Skip to content

Commit

Permalink
add restful interface
Browse files Browse the repository at this point in the history
  • Loading branch information
test committed Apr 19, 2018
1 parent c54750c commit 978bb0a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
59 changes: 59 additions & 0 deletions rest-rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/ochinchina/supervisord/types"
"net/http"
)

type SupervisorRestful struct {
router *mux.Router
supervisor *Supervisor
}

func NewSupervisorRestful(supervisor *Supervisor) *SupervisorRestful {
router := mux.NewRouter()
return &SupervisorRestful{router: router, supervisor: supervisor}
}

func (sr *SupervisorRestful) CreateHandler() 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")
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 {

}
}

func (sr *SupervisorRestful) StartProgram(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
startArgs := StartProcessArgs{Name: params["name"], Wait: true}
result := struct{ Success bool }{false}
err := sr.supervisor.StartProcess(nil, &startArgs, &result)
r := map[string]bool{"success": err == nil && result.Success}
json.NewEncoder(w).Encode(&r)
}

func (sr *SupervisorRestful) StopProgram(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
stopArgs := StartProcessArgs{Name: params["name"], Wait: true}
result := struct{ Success bool }{false}
err := sr.supervisor.StopProcess(nil, &stopArgs, &result)
r := map[string]bool{"success": err == nil && result.Success}
json.NewEncoder(w).Encode(&r)
}

func (sr *SupervisorRestful) ReadStdoutLog(w http.ResponseWriter, req *http.Request) {
}
6 changes: 6 additions & 0 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ func (s *Supervisor) GetState(r *http.Request, args *struct{}, reply *struct{ St
return nil
}

// Get all the name of prorams
//
// Return the name of all the programs
func (s *Supervisor) GetPrograms() []string {
return s.config.GetProgramNames()
}
func (s *Supervisor) GetPID(r *http.Request, args *struct{}, reply *struct{ Pid int }) error {
reply.Pid = os.Getpid()
return nil
Expand Down
28 changes: 14 additions & 14 deletions types/comm-types.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package types

type ProcessInfo struct {
Name string `xml:"name"`
Group string `xml:"group"`
Description string `xml:"description"`
Start int `xml:"start"`
Stop int `xml:"stop"`
Now int `xml:"now"`
State int `xml:"state"`
Statename string `xml:"statename"`
Spawnerr string `xml:"spawnerr"`
Exitstatus int `xml:"exitstatus"`
Logfile string `xml:"logfile"`
Stdout_logfile string `xml:"stdout_logfile"`
Stderr_logfile string `xml:"stderr_logfile"`
Pid int `xml:"pid"`
Name string `xml:"name" json:"name"`
Group string `xml:"group" json:"group"`
Description string `xml:"description" json:"description"`
Start int `xml:"start" json:"start"`
Stop int `xml:"stop" json:"stop"`
Now int `xml:"now" json:"now"`
State int `xml:"state" json:"state"`
Statename string `xml:"statename" json:"statename"`
Spawnerr string `xml:"spawnerr" json:"spawnerr"`
Exitstatus int `xml:"exitstatus" json:"exitstatus"`
Logfile string `xml:"logfile" json:"logfile"`
Stdout_logfile string `xml:"stdout_logfile" json:"stdout_logfile"`
Stderr_logfile string `xml:"stderr_logfile" json:"stderr_logfile"`
Pid int `xml:"pid" json:"pid"`
}

type ReloadConfigResult struct {
Expand Down
2 changes: 2 additions & 0 deletions xmlrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (p *XmlRPC) startHttpServer(user string, password string, protocol string,
p.started = true
mux := http.NewServeMux()
mux.Handle("/RPC2", NewHttpBasicAuth(user, password, p.createRPCServer(s)))
rest_handler := NewSupervisorRestful(s).CreateHandler()
mux.Handle("/", NewHttpBasicAuth(user, password, rest_handler))
listener, err := net.Listen(protocol, listenAddr)
if err == nil {
p.listeners[protocol] = listener
Expand Down

0 comments on commit 978bb0a

Please sign in to comment.