Skip to content

Commit

Permalink
Merge pull request #24 from Jecosine/main
Browse files Browse the repository at this point in the history
feat(http): 增加模式设置功能和请求头常量定义
  • Loading branch information
sunist-c authored Jul 30, 2024
2 parents 6e3b02a + 3945cbb commit 35d0e70
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
33 changes: 33 additions & 0 deletions network/http/define.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package http

import (
"os"

"github.com/gin-gonic/gin"
)

const (
ModeRelease = "RELEASE"
)

var mode = os.Getenv("AC_MODE")

func SetMode(m string) {
mode = m
gin.SetMode(gin.ReleaseMode)
}

const (
defaultTraceHeaderKey = "Ac-Request-Id"
defaultErrorContextKey = "ac-error"
Expand Down Expand Up @@ -207,3 +224,19 @@ type FrameworkResponse struct {
RequestID string `json:"request_id"`
Data any `json:"data,omitempty"`
}

type HeaderEnum = string

const (
HeaderAuthorization HeaderEnum = "Authorization"
HeaderContentType HeaderEnum = "Content-Type"
HeaderUserAgent HeaderEnum = "User-Agent"
HeaderAccept HeaderEnum = "Accept"
HeaderEncoding HeaderEnum = "Accept-Encoding"
HeaderAcceptLanguage HeaderEnum = "Accept-Language"
HeaderContentLength HeaderEnum = "Content-Length"
)

type NoBody = struct{}

type NoResponse = struct{}
7 changes: 6 additions & 1 deletion network/http/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -257,7 +258,11 @@ func (ep *EndPoint[request, response]) Serve(ctx *gin.Context) {
RequestID: tid,
}
ctx.AbortWithStatusJSON(http.StatusInternalServerError, errResponse)
return

if mode != ModeRelease {
// recovered error should be panic with stack trace when not in release mode
panic(fmt.Sprintf("error: %v\nwith stack:\n%s", recovered.Error(), trace.Stack(1)))
}
}
}()

Expand Down
25 changes: 15 additions & 10 deletions network/http/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,21 @@ func CheckRequestBodyPreprocessor[request any, response any](_ *EndPoint[request
}

// unmarshal request body
requestBody, unmarshalErr := defaultPayloadProcessor[request](origin.ContentType(), payload, nil)
if unmarshalErr != nil {
errMsg := values.BuildStringsWithJoin(" ", "invalid request body:", unmarshalErr.Error())
origin.AbortWithStatusJSON(StatusBadRequest, &FrameworkResponse{
ErrorCode: ErrorCodeInvalidRequestBody,
ErrorMessage: errMsg,
RequestID: trace.GetTid(dest),
})
origin.Set(ErrorContextKey(), errMsg)
return
requestBody := values.Nil[request]()
if len(payload) > 0 {
tryRequestBody, unmarshalErr := defaultPayloadProcessor[request](origin.ContentType(), payload, nil)
if unmarshalErr != nil {
errMsg := values.BuildStringsWithJoin(" ", "invalid request body:", unmarshalErr.Error())
origin.AbortWithStatusJSON(StatusBadRequest, &FrameworkResponse{
ErrorCode: ErrorCodeInvalidRequestBody,
ErrorMessage: errMsg,
RequestID: trace.GetTid(dest),
})
origin.Set(ErrorContextKey(), errMsg)
return
}

requestBody = tryRequestBody
}

// check request body
Expand Down

0 comments on commit 35d0e70

Please sign in to comment.