Skip to content

Commit

Permalink
Update cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghui0779 committed Dec 7, 2024
1 parent 86d2544 commit 96df046
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
7 changes: 3 additions & 4 deletions cmd/internal/http/pkg_lib_binding.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/shenghui0779/yiigo"
"github.com/shenghui0779/yiigo/validator"
"github.com/shenghui0779/yiigo/xhttp"
)

func BindJSON(r *http.Request, obj any) error {
Expand All @@ -23,12 +22,12 @@ func BindJSON(r *http.Request, obj any) error {
// BindForm 解析Form表单并校验
func BindForm(r *http.Request, obj any) error {
switch yiigo.ContentType(r) {
case xhttp.ContentForm:
case yiigo.ContentForm:
if err := r.ParseForm(); err != nil {
return err
}
case xhttp.ContentFormMultipart:
if err := r.ParseMultipartForm(xhttp.MaxFormMemory); err != nil {
case yiigo.ContentFormMultipart:
if err := r.ParseMultipartForm(yiigo.MaxFormMemory); err != nil {
if err != http.ErrNotMultipart {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/internal/http/pkg_lib_middleware_log.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/pkg/errors"
"github.com/shenghui0779/yiigo"
"github.com/shenghui0779/yiigo/xhttp"
"github.com/tidwall/pretty"
"go.uber.org/zap"

Expand All @@ -28,14 +27,14 @@ func Log(next http.Handler) http.Handler {
// 请求包含body
if r.Body != nil && r.Body != http.NoBody {
switch yiigo.ContentType(r) {
case xhttp.ContentForm:
case yiigo.ContentForm:
if err := r.ParseForm(); err != nil {
result.ErrSystem(result.E(errors.WithMessage(err, "表单解析失败"))).JSON(w, r)
return
}
body = r.Form.Encode()
case xhttp.ContentFormMultipart:
if err := r.ParseMultipartForm(xhttp.MaxFormMemory); err != nil {
case yiigo.ContentFormMultipart:
if err := r.ParseMultipartForm(yiigo.MaxFormMemory); err != nil {
if err != http.ErrNotMultipart {
result.ErrSystem(result.E(errors.WithMessage(err, "表单解析失败"))).JSON(w, r)
return
Expand Down
3 changes: 1 addition & 2 deletions cmd/internal/http/pkg_lib_result_result.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/shenghui0779/yiigo"
"github.com/shenghui0779/yiigo/xhttp"
"go.uber.org/zap"

"{{.Module}}/pkg/lib/log"
Expand All @@ -23,7 +22,7 @@ type response struct {
}

func (resp *response) JSON(w http.ResponseWriter, r *http.Request) {
w.Header().Set(xhttp.HeaderContentType, xhttp.ContentJSON)
w.Header().Set(yiigo.HeaderContentType, yiigo.ContentJSON)
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(resp.x); err != nil {
log.Error(r.Context(), "Error write response", zap.Error(err))
Expand Down

0 comments on commit 96df046

Please sign in to comment.