chore: initial commit
This commit is contained in:
90
internal/pkg/httputil/request.go
Normal file
90
internal/pkg/httputil/request.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user