Skip to content

Commit

Permalink
feat(openapi): timeout with context
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix-X committed Sep 25, 2024
1 parent ab31569 commit 9e46e3e
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 80 deletions.
12 changes: 12 additions & 0 deletions api/openapi/provider/brainx/demo.api
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ service PowerX {
@doc "hello world api for provider demo"
@handler HelloWorld
get /hello-world returns (HelloWorldResponse)

@doc "timeout api for provider demo"
@handler EchoLongTime
post /echo-long-time (EchoLongTimeRequest) returns (EchoLongTimeResponse)
}



type (
HelloWorldResponse {
Message string `json:"message"`
}

EchoLongTimeRequest {
Timeout int `json:"timeout"`
}
EchoLongTimeResponse {
Message string `json:"message"`
}
)
29 changes: 29 additions & 0 deletions internal/handler/openapi/provider/brainx/echolongtimehandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package brainx

import (
"net/http"

"PowerX/internal/logic/openapi/provider/brainx"
"PowerX/internal/svc"
"PowerX/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)

// timeout api for provider demo
func EchoLongTimeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EchoLongTimeRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}

l := brainx.NewEchoLongTimeLogic(r.Context(), svcCtx)
resp, err := l.EchoLongTime(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
136 changes: 71 additions & 65 deletions internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions internal/logic/openapi/provider/brainx/echolongtimelogic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package brainx

import (
"PowerX/internal/provider/brainx"
"context"
"time"

"PowerX/internal/svc"
"PowerX/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

type EchoLongTimeLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

// timeout api for provider demo
func NewEchoLongTimeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EchoLongTimeLogic {
return &EchoLongTimeLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *EchoLongTimeLogic) EchoLongTime(req *types.EchoLongTimeRequest) (resp *types.EchoLongTimeResponse, err error) {

time.Sleep(60 * time.Second)

return nil, nil
brainXService := brainx.NewBrainXServiceProvider(l.svcCtx)
message, err := brainXService.EchoLongTime(l.ctx, req.Timeout)

return &types.EchoLongTimeResponse{
Message: message,
}, err
}
2 changes: 1 addition & 1 deletion internal/logic/openapi/provider/brainx/helloworldlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewHelloWorldLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HelloW

func (l *HelloWorldLogic) HelloWorld() (resp *types.HelloWorldResponse, err error) {
brainXService := brainx.NewBrainXServiceProvider(l.svcCtx)
message, err := brainXService.HelloWorld()
message, err := brainXService.HelloWorld(l.ctx)

return &types.HelloWorldResponse{
Message: message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package schema
type ResponseHelloWorld struct {
Message string `json:"message"`
}

type ResponseEchoLongTime struct {
Message string `json:"message"`
}
Loading

0 comments on commit 9e46e3e

Please sign in to comment.