This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
91 lines (81 loc) · 2.66 KB
/
server.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
package main
import (
"encoding/json"
"github.com/serverlessworkflow/sdk-go/parser"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
)
const (
envKeyServerPort = "SERVER_PORT"
defaultServerPort = "9000"
)
type defaultHandler func(http.ResponseWriter, *http.Request) *serverError
// ServeHTTP http.ServeHTTP implementation that can handle errors
func (fn defaultHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if err := fn(writer, request); err != nil {
handleServerError(err, writer)
}
}
func getServerPort() string {
serverPort := os.Getenv(envKeyServerPort)
if len(serverPort) == 0 {
serverPort = defaultServerPort
}
return serverPort
}
func startServingRequests() {
http.HandleFunc("/", defaultHandler(handleSWDeployment).ServeHTTP)
http.HandleFunc("/live", defaultHandler(handleLiveness).ServeHTTP)
http.HandleFunc("/ready", defaultHandler(handleReady).ServeHTTP)
serverPort := getServerPort()
log.Printf("Listening at port %s", serverPort)
log.Fatal(http.ListenAndServe(":"+serverPort, nil))
}
type successDeployment struct {
WorkflowName string `json:"workflowName,omitempty"`
}
func handleSWDeployment(writer http.ResponseWriter, request *http.Request) *serverError {
body, err := ioutil.ReadAll(request.Body)
if err != nil {
return newServerError("Failed to read request body", err)
}
if len(body) == 0 {
return newServerError("Body contains no data", nil)
}
workflow, err := parser.FromJSONSource(body)
if err != nil {
return newServerError("Failed to parse request body into a workflow definition", err)
}
if err := deploy(*workflow, body); err != nil {
return newServerError("Failed to deploy Kogito service", err)
}
if err := json.NewEncoder(writer).Encode(&successDeployment{WorkflowName: workflow.Name}); err != nil {
return newServerError("Failed to encode success response", err)
}
return nil
}
func handleLiveness(writer http.ResponseWriter, request *http.Request) *serverError {
writer.WriteHeader(http.StatusOK)
if _, err := writer.Write([]byte("ok")); err != nil {
return newServerError("Failed to write in the response stream", err)
}
return nil
}
func handleReady(writer http.ResponseWriter, request *http.Request) *serverError {
// verify namespace env var
if len(os.Getenv(envKeyNamespace)) == 0 {
return newServerError("Environment variable "+envKeyNamespace+" not defined", nil)
}
// verify kubectl
if err := execCmd("oc", "version"); err != nil {
return newServerError("Failed to run kubectl", err)
}
// verify kogito
if err := execCmd("kogito", "--version"); err != nil {
return newServerError("Failed to run kogito CLI", err)
}
writer.WriteHeader(http.StatusOK)
return nil
}