Fix: patch config API

This commit is contained in:
Dreamacro 2018-11-28 10:38:30 +08:00
parent f6743d4d21
commit a64cea5011
3 changed files with 7 additions and 6 deletions

View File

@ -5,8 +5,8 @@ import (
"github.com/Dreamacro/clash/hub/executor" "github.com/Dreamacro/clash/hub/executor"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
T "github.com/Dreamacro/clash/tunnel"
P "github.com/Dreamacro/clash/proxy" P "github.com/Dreamacro/clash/proxy"
T "github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/render" "github.com/go-chi/render"
@ -15,6 +15,7 @@ import (
func configRouter() http.Handler { func configRouter() http.Handler {
r := chi.NewRouter() r := chi.NewRouter()
r.Get("/", getConfigs) r.Get("/", getConfigs)
r.Patch("/", patchConfigs)
return r return r
} }
@ -32,7 +33,7 @@ func getConfigs(w http.ResponseWriter, r *http.Request) {
render.Respond(w, r, general) render.Respond(w, r, general)
} }
func pointerOrDefault (p *int, def int) int { func pointerOrDefault(p *int, def int) int {
if p != nil { if p != nil {
return *p return *p
} }
@ -40,7 +41,7 @@ func pointerOrDefault (p *int, def int) int {
return def return def
} }
func updateConfigs(w http.ResponseWriter, r *http.Request) { func patchConfigs(w http.ResponseWriter, r *http.Request) {
general := &configSchema{} general := &configSchema{}
if err := render.DecodeJSON(r.Body, general); err != nil { if err := render.DecodeJSON(r.Body, general); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -51,7 +52,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
if general.AllowLan != nil { if general.AllowLan != nil {
P.SetAllowLan(*general.AllowLan) P.SetAllowLan(*general.AllowLan)
} }
ports := P.GetPorts() ports := P.GetPorts()
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port)) P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port))
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort)) P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort))

View File

@ -46,7 +46,7 @@ func (l LogLevel) MarshalYAML() ([]byte, error) {
// UnmarshalJSON unserialize Mode with json // UnmarshalJSON unserialize Mode with json
func (l *LogLevel) UnmarshalJSON(data []byte) error { func (l *LogLevel) UnmarshalJSON(data []byte) error {
var tp string var tp string
json.Unmarshal(data, tp) json.Unmarshal(data, &tp)
level, exist := LogLevelMapping[tp] level, exist := LogLevelMapping[tp]
if !exist { if !exist {
return errors.New("invalid mode") return errors.New("invalid mode")

View File

@ -25,7 +25,7 @@ const (
// UnmarshalJSON unserialize Mode // UnmarshalJSON unserialize Mode
func (m *Mode) UnmarshalJSON(data []byte) error { func (m *Mode) UnmarshalJSON(data []byte) error {
var tp string var tp string
json.Unmarshal(data, tp) json.Unmarshal(data, &tp)
mode, exist := ModeMapping[tp] mode, exist := ModeMapping[tp]
if !exist { if !exist {
return errors.New("invalid mode") return errors.New("invalid mode")