parent
a7c27a84a2
commit
856fd4780a
@ -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…
Reference in new issue