rpc_safe_guard_01/pkg/handler/handler.go
2023-07-13 17:39:26 +08:00

37 lines
779 B
Go

package handler
import (
"net/http"
"rpc_safe_guard_01/pkg/okapi"
"strings"
)
func Handler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
contentType := r.Header.Get("Content-Type")
if !strings.Contains(contentType, "application/json") {
http.Error(w, "Unsupported media type", http.StatusUnsupportedMediaType)
return
}
var req okapi.Request
err := req.Decode(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := Execute(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write([]byte(data))
}