feat: support RESTful api (#33)

This commit is contained in:
monkeyWie authored and GitHub committed 2022-09-09 18:31:57 +08:00
1 parent e292926697
commit c6aa83194f
22 files changed
+603 -125

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
package util
import (
"encoding/json"
"github.com/monkeyWie/gopeed-core/pkg/rest/model"
"net/http"
)
func ReadJson(w http.ResponseWriter, r *http.Request, v any) bool {
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
WriteJson(w, 500, model.NewResultWithMsg(err.Error()))
return false
}
return true
}
func WriteJsonOk(w http.ResponseWriter, v any) {
WriteJson(w, 200, v)
}
func WriteJson(w http.ResponseWriter, code int, v any) {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(v)
}