-
Notifications
You must be signed in to change notification settings - Fork 571
/
rest-rpc.go
157 lines (133 loc) · 5.04 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
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/ochinchina/supervisord/types"
)
// SupervisorRestful the restful interface to control the programs defined in configuration file
type SupervisorRestful struct {
router *mux.Router
supervisor *Supervisor
}
// NewSupervisorRestful create a new SupervisorRestful object
func NewSupervisorRestful(supervisor *Supervisor) *SupervisorRestful {
return &SupervisorRestful{router: mux.NewRouter(), supervisor: supervisor}
}
// CreateProgramHandler create http handler to process program related restful request
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
}
// CreateSupervisorHandler create http rest interface to control supervisor itself
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
}
// ListProgram 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)
}
}
// StartProgram start the given program through restful interface
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
}
// StartPrograms start one or more programs through restful interface
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"))
}
}
// StopProgram stop a program through the restful interface
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
}
// StopPrograms stop programs through the restful interface
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"))
}
}
// ReadStdoutLog read the stdout of given program
func (sr *SupervisorRestful) ReadStdoutLog(w http.ResponseWriter, req *http.Request) {
}
// Shutdown the supervisor itself
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..."))
}
// Reload the supervisor configuration file through rest interface
func (sr *SupervisorRestful) Reload(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
reply := struct{ Ret bool }{false}
sr.supervisor.Reload(false)
r := map[string]bool{"success": reply.Ret}
json.NewEncoder(w).Encode(&r)
}