Skip to content

Commit

Permalink
Merge pull request #17 from golang-acexy/develop
Browse files Browse the repository at this point in the history
bugfix: commonResp未能按照预期返回指定的[]byte数据
  • Loading branch information
acexy authored Jul 5, 2024
2 parents 264324c + c7e4e5e commit 669e98b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 20 deletions.
12 changes: 4 additions & 8 deletions ginstarter/ginloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
var server *http.Server
var ginEngine *gin.Engine

const (
defaultListenAddress = ":8080"
)

var (
disabledDefaultIgnoreHttpStatusCode bool
ignoreHttpStatusCode []int
Expand Down Expand Up @@ -127,25 +123,25 @@ func (g *GinStarter) Start() (interface{}, error) {
}

if g.ListenAddress == "" {
g.ListenAddress = defaultListenAddress
g.ListenAddress = ":8080"
}

server = &http.Server{
Addr: g.ListenAddress,
Handler: ginEngine,
}

status := make(chan error)
errChn := make(chan error)
go func() {
if err = server.ListenAndServe(); err != nil {
status <- err
errChn <- err
}
}()

select {
case <-time.After(time.Second):
return ginEngine, nil
case err = <-status:
case err = <-errChn:
return ginEngine, err
}
}
Expand Down
16 changes: 13 additions & 3 deletions ginstarter/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type Request struct {
ctx *gin.Context
}

// RawGinContext 获取原始Gin上下文
func (r *Request) RawGinContext() *gin.Context {
return r.ctx
}

// HttpMethod 获取请求方法
func (r *Request) HttpMethod() string {
return r.ctx.Request.Method
Expand All @@ -23,9 +28,14 @@ func (r *Request) FullPath() string {
return r.ctx.FullPath()
}

// RawGinContext 获取原始Gin上下文
func (r *Request) RawGinContext() *gin.Context {
return r.ctx
// Host 获取Host信息
func (r *Request) Host() string {
return r.ctx.Request.Host
}

// Proto 获取请求协议
func (r *Request) Proto() string {
return r.ctx.Request.Proto
}

// RequestIP 尝试获取请求方客户端IP
Expand Down
6 changes: 3 additions & 3 deletions ginstarter/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ type commonResp struct {
}

func (c *commonResp) Data() *ResponseData {
return nil
return c.responseData
}

// NewCommonResp 创建一个普通响应
Expand All @@ -157,8 +157,8 @@ func NewCommonResp() *commonResp {
}

// DataBuilder 响应数据构造器
func (c *commonResp) DataBuilder(fn func(data *ResponseData)) Response {
fn(c.responseData)
func (c *commonResp) DataBuilder(fn func() *ResponseData) Response {
c.responseData = fn()
return c
}

Expand Down
9 changes: 8 additions & 1 deletion ginstarter/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,17 @@ type ResponseCookie struct {
httpOnly bool
}

func NewResponseData() *ResponseData {
func NewEmptyResponseData() *ResponseData {
return &ResponseData{}
}

func NewResponseData(contentType string, body []byte) *ResponseData {
return &ResponseData{
contentType: contentType,
data: body,
}
}

func NewHeader(name, value string) *ResponseHeader {
return &ResponseHeader{name: name, value: value}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/golang-acexy/starter-gin
go 1.20

require (
github.com/acexy/golang-toolkit v0.0.11
github.com/acexy/golang-toolkit v0.0.14
github.com/gin-gonic/gin v1.10.0
github.com/golang-acexy/starter-parent v0.1.1
github.com/sirupsen/logrus v1.9.3
Expand Down Expand Up @@ -32,7 +32,7 @@ require (
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/acexy/golang-toolkit v0.0.11 h1:RxZ6rY9GSvH7JqfbSNoUd2IEBGJjJ7XWdcovFDxSmj0=
github.com/acexy/golang-toolkit v0.0.11/go.mod h1:lZn+XmM/H0Y3AQ3G4BiDwkiKbBXCgGVeTCz02ILhc0s=
github.com/acexy/golang-toolkit v0.0.14 h1:Aoz/PatuIDQ8j2lN4/vqcJNOE8mv85RcPsbhYsP9o5o=
github.com/acexy/golang-toolkit v0.0.14/go.mod h1:AHH70PoF5X9zXpAoBSBb4+kfeYPO/1tXOtONEu/QNlc=
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
Expand Down Expand Up @@ -79,6 +79,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
9 changes: 9 additions & 0 deletions test/router/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (d *DemoRouter) Handlers(router *ginstarter.RouterWrapper) {
router.GET("empty", d.empty())

router.GET("redirect", d.redirect())
router.GET("common", d.common())
}

func (d *DemoRouter) more() ginstarter.HandlerWrapper {
Expand Down Expand Up @@ -76,3 +77,11 @@ func (d *DemoRouter) redirect() ginstarter.HandlerWrapper {
return ginstarter.RespRedirect("https://google.com"), nil
}
}

func (d *DemoRouter) common() ginstarter.HandlerWrapper {
return func(request *ginstarter.Request) (ginstarter.Response, error) {
return ginstarter.NewCommonResp().DataBuilder(func() *ginstarter.ResponseData {
return ginstarter.NewEmptyResponseData().SetData([]byte("success"))
}), nil
}
}
2 changes: 1 addition & 1 deletion test/router/myrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (m *MyRestResponse) Data() *ginstarter.ResponseData {
}

func (m *MyRestResponse) setData(data any) {
m.responseData = ginstarter.NewResponseData()
m.responseData = ginstarter.NewEmptyResponseData()
m.responseData.SetData(json.ToJsonBytes(&RestStruct{
Code: 200,
Msg: "success",
Expand Down

0 comments on commit 669e98b

Please sign in to comment.