-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (96 loc) · 2.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cgi"
"github.com/harness/artifacts-cgi/artifacts/docker"
"github.com/harness/artifacts-cgi/common"
"github.com/harness/artifacts-cgi/logger"
"github.com/sirupsen/logrus"
)
func main() {
logger.SetLogrus()
http.HandleFunc("/", handle)
logrus.Info("artifacts-cgi server is running")
cgi.Serve(http.DefaultServeMux)
}
type ArtifactType string
type ArtifactOperation string
var (
Docker ArtifactType = "DockerRegistry"
Validate ArtifactOperation = "VALIDATE"
)
type Params struct {
ArtifactType ArtifactType `json:"artifact_type"`
ArtifactOperation ArtifactOperation `json:"artifact_operation"`
ArtifactParams json.RawMessage `json:"artifact_params"`
}
func handle(w http.ResponseWriter, r *http.Request) {
// unmarshal the input
logrus.Info("handling new request")
params, err := parseParams(r)
if err != nil {
sendErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// get the artifact handler
handler, err := getHandler(params)
if err != nil {
sendErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := applyOperation(handler, params)
if err != nil {
sendErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
sendSuccessResponse(w, res)
}
func applyOperation(handler common.ArtifactHandler, params *Params) (interface{}, error) {
var res interface{}
var err error
switch operation := params.ArtifactOperation; operation {
case Validate:
res, err = handler.Validate()
default:
err = fmt.Errorf("unsupported artifact operation [%s]", operation)
}
return res, err
}
func getHandler(params *Params) (common.ArtifactHandler, error) {
var handler common.ArtifactHandler
var err error
switch artifactType := params.ArtifactType; artifactType {
case Docker:
handler, err = docker.New(params.ArtifactParams)
default:
err = fmt.Errorf("unsupported artifact type [%s]", artifactType)
}
return handler, err
}
func parseParams(r *http.Request) (*Params, error) {
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("unable to read request body")
}
defer r.Body.Close()
params := new(Params)
if err := json.Unmarshal(body, params); err != nil {
return nil, fmt.Errorf("invalid payload")
}
return params, nil
}
func sendSuccessResponse(w http.ResponseWriter, response interface{}) {
logrus.Infof("sending success response")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
func sendErrorResponse(w http.ResponseWriter, status int, errMsg string) {
logrus.Errorf("sending error response with status [%s] and msg [%s]", status, errMsg)
w.WriteHeader(status)
json.NewEncoder(w).Encode(map[string]string{
"error": errMsg,
})
}