feat(api): require bearer authentication and unify response contracts
This commit is contained in:
1 parent
30e361cd10
commit
1b2b2b12ff
19 files changed
+1799
-314
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
CodeCaptchaInvalid = 40001
|
||||
CodeCaptchaExpired = 40002
|
||||
CodeCurrentPassword = 40003
|
||||
CodeBadCredentials = 40101
|
||||
CodeConflict = 41200
|
||||
)
|
||||
|
||||
type response struct {
|
||||
Code int `json:"code"`
|
||||
Data any `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, v any) {
|
||||
writeResponse(w, status, response{Code: 0, Data: v, Message: "成功"})
|
||||
}
|
||||
func writeFailure(w http.ResponseWriter, status, code int, message string, data any) {
|
||||
writeResponse(w, status, response{Code: code, Data: data, Message: message})
|
||||
}
|
||||
func writeError(w http.ResponseWriter, status int, message string) {
|
||||
writeFailure(w, status, status*100, message, nil)
|
||||
}
|
||||
func writeResponse(w http.ResponseWriter, status int, value response) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
func logPath(value string) string {
|
||||
const prefix = "/api/v1/resources/"
|
||||
if strings.HasPrefix(value, prefix) {
|
||||
_, rest, _ := strings.Cut(strings.TrimPrefix(value, prefix), "/")
|
||||
return prefix + "[redacted]/" + rest
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in new issue
Block a user