go-fastdfs/server/http_reload.go
2023-04-09 17:11:17 +08:00

86 lines
2.0 KiB
Go

package server
import (
"io/ioutil"
"net/http"
log "github.com/sjqzhang/seelog"
)
func (c *Server) Reload(w http.ResponseWriter, r *http.Request) {
var (
err error
data []byte
cfg GlobalConfig
action string
cfgjson string
result JsonResult
key string
)
result.Status = "fail"
r.ParseForm()
if !c.IsPeer(r) {
w.Write([]byte(c.GetClusterNotPermitMessage(r)))
return
}
cfgjson = r.FormValue("cfg")
action = r.FormValue("action")
key = r.FormValue("key")
if key == "" {
result.Message = "(error)parameter key require"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if key != Config().AdminKey {
result.Message = "(error)key is not correct"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
_ = cfgjson
if action == "get" {
result.Data = Config()
result.Status = "ok"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if action == "set" {
if cfgjson == "" {
result.Message = "(error)parameter cfg(json) require"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if err = json.Unmarshal([]byte(cfgjson), &cfg); err != nil {
log.Error(err)
result.Message = err.Error()
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
result.Status = "ok"
cfgjson = c.util.JsonEncodePretty(cfg)
c.util.WriteFile(CONST_CONF_FILE_NAME, cfgjson)
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if action == "reload" {
if data, err = ioutil.ReadFile(CONST_CONF_FILE_NAME); err != nil {
result.Message = err.Error()
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if err = json.Unmarshal(data, &cfg); err != nil {
result.Message = err.Error()
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
ParseConfig(CONST_CONF_FILE_NAME)
c.initComponent(true)
result.Status = "ok"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if action == "" {
w.Write([]byte("(error)action support set(json) get reload"))
}
}