-
Notifications
You must be signed in to change notification settings - Fork 36
/
http.go
137 lines (113 loc) · 3.41 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Webserver logic
package main
import (
_ "embed"
"encoding/json"
"html/template"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
)
const (
httpPathAnsibleAdhocTrigger = "/ansible/adhoc-run"
httpPathAnsibleDisable = "/ansible/disable"
httpPathAnsibleEnable = "/ansible/enable"
httpPathAnsibleControl = "/ansible/control"
httpPathStatus = "/ansible/status"
)
var (
disableReason string
//go:embed templates/index.html
indexHtml string
//go:embed templates/ansible_controller.html
ansibleController string
)
// MakeRunOnceHandler returns an http handler that calls runOnce when invoked.
func MakeRunOnceHandler(runOnce func()) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
runOnce()
http.Redirect(w, r, httpPathAnsibleControl, http.StatusFound)
}
}
func HandlerAnsibleEnable(w http.ResponseWriter, r *http.Request) {
disableReason = ""
ansibleEnable()
http.Redirect(w, r, httpPathAnsibleControl, http.StatusFound)
}
func HandlerAnsibleDisable(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if val, ok := r.Form["disable-reason"]; ok {
disableReason = val[0]
}
ansibleDisable()
http.Redirect(w, r, httpPathAnsibleControl, http.StatusFound)
}
func HandlerAnsibleControl(w http.ResponseWriter, r *http.Request) {
data := struct {
AnsibleDisabled bool
AnsibleLastRunSuccess bool
JobRunning bool
Hostname string
DisableReason string
}{
ansibleDisabled, // Callout to the global var in main... inelegant
ansibleLastRunSuccess,
ansibleRunning,
hostname,
disableReason,
}
t, _ := template.New("foo").Parse(ansibleController)
_ = t.Execute(w, data)
}
func HandlerIndex(w http.ResponseWriter, r *http.Request) {
data := struct {
Hostname string
}{
hostname,
}
t, _ := template.New("foo").Parse(indexHtml)
_ = t.Execute(w, data)
}
func HandlerStatus(w http.ResponseWriter, r *http.Request) {
status := map[string]interface{}{
"app_name": appName,
"hostname": hostname,
"ansible_disabled": ansibleDisabled,
"ansible_running": ansibleRunning,
"ansible_last_run_success": ansibleLastRunSuccess,
"version": Version,
}
data, err := json.Marshal(status)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(data)
}
// NewServer creates a new http server
//
// runOnce is a function that we will be called when the adhocTrigger handler is invoked.
func NewServer(runOnce func()) *http.Server {
r := mux.NewRouter()
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/", HandlerIndex).Methods("GET")
r.HandleFunc(httpPathAnsibleAdhocTrigger, MakeRunOnceHandler(runOnce)).Methods("POST")
r.HandleFunc(httpPathAnsibleDisable, HandlerAnsibleDisable).Methods("POST")
r.HandleFunc(httpPathAnsibleEnable, HandlerAnsibleEnable).Methods("POST")
r.HandleFunc(httpPathAnsibleControl, HandlerAnsibleControl).Methods("GET")
r.HandleFunc(httpPathStatus, HandlerStatus).Methods("GET")
srv := &http.Server{
Handler: r,
Addr: viper.GetString("http-listen-string"),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
return srv
}