forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-rpc.go
145 lines (122 loc) · 4.3 KB
/
rest-rpc.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
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/ochinchina/supervisord/types"
"io/ioutil"
"net/http"
)
type SupervisorRestful struct {
router *mux.Router
supervisor *Supervisor
}
func NewSupervisorRestful(supervisor *Supervisor) *SupervisorRestful {
return &SupervisorRestful{router: mux.NewRouter(), supervisor: supervisor}
}
func (sr *SupervisorRestful) CreateProgramHandler() http.Handler {
sr.router.HandleFunc("/program/list", sr.ListProgram).Methods("GET")
sr.router.HandleFunc("/program/start/{name}", sr.StartProgram).Methods("POST", "PUT")
sr.router.HandleFunc("/program/stop/{name}", sr.StopProgram).Methods("POST", "PUT")
sr.router.HandleFunc("/program/log/{name}/stdout", sr.ReadStdoutLog).Methods("GET")
sr.router.HandleFunc("/program/startPrograms", sr.StartPrograms).Methods("POST", "PUT")
sr.router.HandleFunc("/program/stopPrograms", sr.StopPrograms).Methods("POST", "PUT")
return sr.router
}
func (sr *SupervisorRestful) CreateSupervisorHandler() http.Handler {
sr.router.HandleFunc("/supervisor/shutdown", sr.Shutdown).Methods("PUT", "POST")
sr.router.HandleFunc("/supervisor/reload", sr.Reload).Methods("PUT", "POST")
return sr.router
}
// list the status of all the programs
//
// json array to present the status of all programs
func (sr *SupervisorRestful) ListProgram(w http.ResponseWriter, req *http.Request) {
result := struct{ AllProcessInfo []types.ProcessInfo }{make([]types.ProcessInfo, 0)}
if sr.supervisor.GetAllProcessInfo(nil, nil, &result) == nil {
json.NewEncoder(w).Encode(result.AllProcessInfo)
} else {
r := map[string]bool{"success": false}
json.NewEncoder(w).Encode(r)
}
}
func (sr *SupervisorRestful) StartProgram(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
params := mux.Vars(req)
success, err := sr._startProgram(params["name"])
r := map[string]bool{"success": err == nil && success}
json.NewEncoder(w).Encode(&r)
}
func (sr *SupervisorRestful) _startProgram(program string) (bool, error) {
startArgs := StartProcessArgs{Name: program, Wait: true}
result := struct{ Success bool }{false}
err := sr.supervisor.StartProcess(nil, &startArgs, &result)
return result.Success, err
}
func (sr *SupervisorRestful) StartPrograms(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
var b []byte
var err error
if b, err = ioutil.ReadAll(req.Body); err != nil {
w.WriteHeader(400)
w.Write([]byte("not a valid request"))
return
}
var programs []string
if err = json.Unmarshal(b, &programs); err != nil {
w.WriteHeader(400)
w.Write([]byte("not a valid request"))
} else {
for _, program := range programs {
sr._startProgram(program)
}
w.Write([]byte("Success to start the programs"))
}
}
func (sr *SupervisorRestful) StopProgram(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
params := mux.Vars(req)
success, err := sr._stopProgram(params["name"])
r := map[string]bool{"success": err == nil && success}
json.NewEncoder(w).Encode(&r)
}
func (sr *SupervisorRestful) _stopProgram(programName string) (bool, error) {
stopArgs := StartProcessArgs{Name: programName, Wait: true}
result := struct{ Success bool }{false}
err := sr.supervisor.StopProcess(nil, &stopArgs, &result)
return result.Success, err
}
func (sr *SupervisorRestful) StopPrograms(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
var programs []string
var b []byte
var err error
if b, err = ioutil.ReadAll(req.Body); err != nil {
w.WriteHeader(400)
w.Write([]byte("not a valid request"))
return
}
if err := json.Unmarshal(b, &programs); err != nil {
w.WriteHeader(400)
w.Write([]byte("not a valid request"))
} else {
for _, program := range programs {
sr._stopProgram(program)
}
w.Write([]byte("Success to stop the programs"))
}
}
func (sr *SupervisorRestful) ReadStdoutLog(w http.ResponseWriter, req *http.Request) {
}
func (sr *SupervisorRestful) Shutdown(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
reply := struct{ Ret bool }{false}
sr.supervisor.Shutdown(nil, nil, &reply)
w.Write([]byte("Shutdown..."))
}
func (sr *SupervisorRestful) Reload(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
reply := struct{ Ret bool }{false}
sr.supervisor.Reload()
r := map[string]bool{"success": reply.Ret}
json.NewEncoder(w).Encode(&r)
}