chore: initial commit

This commit is contained in:
2026-07-22 17:50:33 +08:00
parent 82936a8987
commit ee9f50b859
107 changed files with 8346 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
package httputil
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"server/internal/config"
"server/internal/model/common"
"server/internal/pkg/errs"
"server/internal/pkg/validator"
"strconv"
"github.com/go-chi/chi/v5"
)
// Pagination 分页请求参数
func Pagination(r *http.Request) *common.Pagination {
q := r.URL.Query()
// page 默认 1
page := int32(1)
if p := q.Get("page"); p != "" {
if v, err := strconv.Atoi(p); err == nil && v >= 1 {
page = int32(v)
}
}
// pageSize 默认 10
pageSize := int32(10)
if ps := q.Get("page_size"); ps != "" {
if v, err := strconv.Atoi(ps); err == nil && v >= 1 && v <= 100 {
pageSize = int32(v)
}
}
return &common.Pagination{
Page: page,
PageSize: pageSize,
}
}
func URLParamInt32(r *http.Request, key string) (int32, error) {
v := chi.URLParam(r, key)
if v == "" {
return 0, errs.ErrIDRequired
}
n, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return 0, errs.ErrInvalidID
}
return int32(n), nil
}
func BindJson(r *http.Request, dest any) error {
if err := json.NewDecoder(r.Body).Decode(dest); err != nil {
if errors.Is(err, io.EOF) {
return errs.ErrEmptyBody
}
return errs.ErrInvalidJSON
}
if err := validator.Struct(dest); err != nil {
return err
}
return nil
}
func BuildFileUrl(path *string) string {
if path == nil || *path == "" {
return ""
}
baseURL := config.GetString("file.base_url")
if baseURL == "" {
return ""
}
result, err := url.JoinPath(baseURL, *path)
if err != nil {
return ""
}
return result
}

View File

@@ -0,0 +1,65 @@
package httputil
import (
"encoding/json"
"errors"
"log/slog"
"net/http"
"server/internal/model/common"
"server/internal/pkg/errs"
validatorI18 "server/internal/pkg/validator"
"strings"
"github.com/go-playground/validator/v10"
)
func respondWithJSON(w http.ResponseWriter, code int, data any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(data); err != nil {
slog.Error("respondWithJSON: failed to encode response", "error", err)
}
}
func Ok(w http.ResponseWriter, data ...any) {
resp := common.Response{
Message: "ok",
}
if len(data) > 0 && data[0] != nil {
resp.Data = data[0]
}
respondWithJSON(w, http.StatusOK, resp)
}
func OkWithPage(w http.ResponseWriter, pageResp *common.PageResponse) {
pageResp.Message = "ok"
respondWithJSON(w, http.StatusOK, pageResp)
}
// Fail 响应失败
func Fail(w http.ResponseWriter, err error) {
var msg string
// 默认code 500
httpStatusCode := http.StatusInternalServerError
var validationErrs validator.ValidationErrors
var appErr *errs.AppError
if errors.As(err, &validationErrs) {
httpStatusCode = http.StatusBadRequest
msgs := validatorI18.Translate(err)
msg = strings.Join(msgs, "; ")
} else if errors.As(err, &appErr) {
httpStatusCode = appErr.HTTPCode
msg = appErr.Msg
} else {
msg = err.Error()
}
resp := common.Response{
Message: msg,
}
respondWithJSON(w, httpStatusCode, resp)
}