46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func handleTransportSingBoxProfileRender(w http.ResponseWriter, r *http.Request, id string) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
body, checkBinary, persist, err := decodeTransportSingBoxProfileRenderRequest(r)
|
|
if err != nil {
|
|
http.Error(w, "bad json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
singBoxProfilesMu.Lock()
|
|
defer singBoxProfilesMu.Unlock()
|
|
|
|
status, resp := executeTransportSingBoxProfileRenderLocked(id, body, checkBinary, persist)
|
|
writeJSON(w, status, resp)
|
|
}
|
|
|
|
func decodeTransportSingBoxProfileRenderRequest(r *http.Request) (SingBoxProfileRenderRequest, bool, bool, error) {
|
|
var body SingBoxProfileRenderRequest
|
|
if r.Body != nil {
|
|
defer r.Body.Close()
|
|
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil && err != io.EOF {
|
|
return SingBoxProfileRenderRequest{}, false, false, err
|
|
}
|
|
}
|
|
checkBinary := true
|
|
if body.CheckBinary != nil {
|
|
checkBinary = *body.CheckBinary
|
|
}
|
|
persist := true
|
|
if body.Persist != nil {
|
|
persist = *body.Persist
|
|
}
|
|
return body, checkBinary, persist, nil
|
|
}
|