add version and shutdown

master
shengxiang 9 years ago
parent a7c27a84a2
commit 856fd4780a

@ -1,9 +1,13 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"time"
"github.com/codegangsta/cli"
)
@ -16,6 +20,41 @@ func MkdirIfNoExists(dir string) error {
return nil
}
func chttp(method string, url string, v ...interface{}) (res *JSONResponse, err error) {
var resp *http.Response
switch method {
case "GET":
resp, err = http.Get(url)
case "POST":
resp, err = http.PostForm(url, nil)
}
if err != nil {
return nil, err
}
defer resp.Body.Close()
res = &JSONResponse{}
err = json.NewDecoder(resp.Body).Decode(res)
return
}
func wrapAction(f func(*cli.Context)) func(*cli.Context) {
return func(c *cli.Context) {
// check if serer alive
//host := c.GlobalString("host")
//port := c.GlobalInt("port")
//ServeAddr(host, port)
go exec.Command(os.Args[0], "serv").Run()
time.Sleep(time.Millisecond * 500)
f(c)
}
}
func ServAction(ctx *cli.Context) {
host := ctx.GlobalString("host")
port := ctx.GlobalInt("port")
ServeAddr(host, port)
}
func StatusAction(ctx *cli.Context) {
println("status todo")
}
@ -28,11 +67,30 @@ func AddAction(ctx *cli.Context) {
func StopAction(ctx *cli.Context) {
}
func ShutdownAction(ctx *cli.Context) {
res, err := chttp("POST", fmt.Sprintf("http://%s:%d/api/shutdown",
ctx.GlobalString("host"), ctx.GlobalInt("port")))
if err != nil {
panic(err)
}
fmt.Println(res.Message)
}
func VersionAction(ctx *cli.Context) {
fmt.Printf("Client: %s\n", GOSUV_VERSION)
res, err := chttp("GET", fmt.Sprintf("http://%s:%d/api/version",
ctx.GlobalString("host"), ctx.GlobalInt("port")))
if err != nil {
panic(err)
}
fmt.Printf("Server: %s\n", res.Message)
}
var app *cli.App
func init() {
app = cli.NewApp()
app.Version = "0.0.1"
app.Version = GOSUV_VERSION
app.Name = "gosuv"
app.Usage = "supervisor your program"
app.HideHelp = true
@ -52,6 +110,11 @@ func init() {
}
app.Commands = []cli.Command{
{
Name: "version",
Usage: "Show version",
Action: wrapAction(VersionAction),
},
{
Name: "status",
Usage: "show program status",
@ -70,15 +133,26 @@ func init() {
},
{
Name: "stop",
Usage: "stop running program",
Usage: "Stop running program",
Action: StopAction,
},
{
Name: "shutdown",
Usage: "Shutdown server",
Action: ShutdownAction,
},
{
Name: "serv",
Usage: "This command should only be called by gosuv itself",
Action: ServAction,
},
}
}
const (
GOSUV_HOME = "$HOME/.gosuv"
GOSUV_CONFIG = "gosuv.json"
GOSUV_HOME = "$HOME/.gosuv"
GOSUV_CONFIG = "gosuv.json"
GOSUV_VERSION = "0.0.1"
)
func main() {

@ -0,0 +1,46 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
type JSONResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
func renderJSON(w http.ResponseWriter, v interface{}) {
w.Header().Add("Content-Type", "json")
json.NewEncoder(w).Encode(v)
}
func versionHandler(w http.ResponseWriter, r *http.Request) {
renderJSON(w, &JSONResponse{
Code: 200,
Message: GOSUV_VERSION,
})
}
func shutdownHandler(w http.ResponseWriter, r *http.Request) {
go func() {
time.Sleep(50 * time.Millisecond)
os.Exit(2)
}()
renderJSON(w, &JSONResponse{
Code: 200,
Message: "not implement",
})
}
func ServeAddr(host string, port int) error {
r := mux.NewRouter()
r.HandleFunc("/api/version", versionHandler)
r.Methods("POST").HandleFunc("/api/shutdown", shutdownHandler)
return http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), r)
}
Loading…
Cancel
Save