gPanel/pkg/api/server/shutdown.go
George Shaw b9f7c5d065 Fix import paths
* github.com/Ennovar/gPanel -> github.com/kentonh/gPanel
2018-09-21 20:13:49 -05:00

43 lines
1.3 KiB
Go

// Package server is a child of package api to handle api calls concerning the server
package server
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/kentonh/gPanel/pkg/public"
)
// Shutdown function is called from /api/server/shutdown and will attempt to shutdown, either gracefully
// or not gracefully (contingent on request data).
func Shutdown(res http.ResponseWriter, req *http.Request, logger *log.Logger, publicServer *public.Controller) bool {
if req.Method != "UPDATE" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
var shutdownRequestData struct {
Graceful bool `json:"graceful"`
}
err := json.NewDecoder(req.Body).Decode(&shutdownRequestData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
err = publicServer.Stop(shutdownRequestData.Graceful)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
res.WriteHeader(http.StatusNoContent)
return true
}