This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
90 lines (81 loc) · 2.3 KB
/
http.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
package deployer
import (
"io"
"net/http"
"strings"
)
func readSecret(c *DeploymentConfig, r *http.Request) (*string, *ConfigError) {
if c.Secret == nil {
return nil, nil
}
if c.SecretHeader != nil {
var secret = r.Header.Get(*c.SecretHeader)
return &secret, nil
} else {
var bodyReader = r.Body
if bodyReader == nil {
bmsg := "Body is empty"
return nil, &ConfigError{PreconditionsError, &bmsg}
}
var secret = make([]byte, MaxSecretSize)
var read, rerr = bodyReader.Read(secret)
if rerr != nil {
/* rmsg := rerr.Error()
return deploymentConf, &ConfigError{PreconditionsError, &rmsg}
*/
}
var secretStr = string(secret[:read])
return &secretStr, nil
}
}
func runConfigFromRequest(r *http.Request) (string, *ConfigError) {
var path = strings.Split(strings.Trim(r.URL.Path, "/"), "/")
var deploymentConf = path[len(path)-1]
var err = runConfig(deploymentConf, r)
return deploymentConf, err
}
func handleDeployment(w http.ResponseWriter, r *http.Request) {
var deploymentConf, err = runConfigFromRequest(r)
if err != nil {
if err.IsMissingConfig() {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, "Config "+deploymentConf+" not found: "+(*err.Error()))
io.WriteString(w, "\n")
return
}
if err.IsReadError() {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "Config "+deploymentConf+" error: "+(*err.Error()))
io.WriteString(w, "\n")
return
}
if err.IsExecutionError() {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "Config "+deploymentConf+" execution error: "+(*err.Error()))
io.WriteString(w, "\n")
return
}
if err.IsPreconditionsError() {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Config "+deploymentConf+" preconditions error: "+(*err.Error()))
io.WriteString(w, "\n")
return
}
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "Config "+deploymentConf+" unknown error: "+(*err.Error()))
io.WriteString(w, "\n")
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, "OK")
return
}
// path to read configs
var configurationBase string
// MakeMux creates new ServerMux object
func MakeMux(directory string) *http.ServeMux {
configurationBase = directory
mux := http.NewServeMux()
mux.HandleFunc("/incoming/", handleDeployment)
return mux
}