Skip to content

Commit

Permalink
pass a service from define the action
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy committed Aug 19, 2023
1 parent 51e3f7d commit cf6eac8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
5 changes: 5 additions & 0 deletions elements/forms/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Action struct {
AllowedMethods []string
handlerPath string

Service interface{}
Params interface{}
BodyRequestModel BaseModelRequest
ResponseModels map[int]interface{}
Expand Down Expand Up @@ -250,3 +251,7 @@ func (a *Action) GetResponseModels() map[int]interface{} {
func (a *Action) GetParams() interface{} {
return a.Params
}

func (a *Action) GetService() interface{} {
return a.Service
}
32 changes: 23 additions & 9 deletions extensions/servers/gin/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

const (
CTX = "GIN_CTX"
TYPHOONServer = "TYPHOON_SERVER"
CTX = "GIN_CTX"
TYPHOONServer = "TYPHOON_SERVER"
TYPHOONActionService = "ACTION_SERVICE"
)


func GetTyphoonGinServer(server interfaces.ServerInterface) (bool, *TyphoonGinServer){
func GetTyphoonGinServer(server interfaces.ServerInterface) (bool, *TyphoonGinServer) {
ginServer, ok := server.(*TyphoonGinServer)
return ok, ginServer
}
Expand All @@ -30,20 +30,34 @@ func GetGinGroup(group interface{}) *gin.RouterGroup {
return group.(*gin.RouterGroup)
}

func NewRequestCtx(context Context.Context, ginCtx *gin.Context) Context.Context{
func NewRequestCtx(context Context.Context, ginCtx *gin.Context) Context.Context {
return ctx.Update(context, CTX, ginCtx)
}

func GetRequestCtx(context Context.Context) (bool, *gin.Context){
func GetRequestCtx(context Context.Context) (bool, *gin.Context) {
request, ok := ctx.Get(context, CTX).(*gin.Context)
return ok, request
}

func GetServerCtx(context Context.Context) (bool, interfaces.ServerInterface){
func GetServerCtx(context Context.Context) (bool, interfaces.ServerInterface) {
request, ok := ctx.Get(context, TYPHOONServer).(interfaces.ServerInterface)
return ok, request
}

func NewServerCtx(context Context.Context, server interfaces.ServerInterface) Context.Context{
func NewServerCtx(context Context.Context, server interfaces.ServerInterface) Context.Context {
return ctx.Update(context, TYPHOONServer, server)
}
}

func PackActionServiceIntoContext(service interface{}, context Context.Context) Context.Context {
if service != nil {
return ctx.Update(context, TYPHOONActionService, service)
}
return context
}

func GetActionService(context Context.Context) (bool, interface{}) {

service, ok := ctx.Get(context, TYPHOONActionService).(interface{})
return ok, service

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/vortex14/gotyphoon/elements/forms"
"github.com/vortex14/gotyphoon/elements/models/label"
Expand All @@ -27,21 +28,34 @@ type TokenResponse struct {
Token string `json:"token"`
}

// handler
// @Tags Auth
// @Accept json
// @Produce plain
// @Summary Discovery login controller
// @Description Typhoon Discovery login controller
// @Success 200 {string}
// @Router /api/v1/login [post]
func handler(ctx *gin.Context, logger interfaces.LoggerInterface) {

service, ok := ctx.Get(GinExtension.TYPHOONActionService)

if ok {
_service := service.(*Service)
logger.Warning(fmt.Sprintf("OK ! %s", _service.Test()))
}

ctx.String(200, JWTAUTHDefault)
}

type Service struct {
Repository interface{}
}

func (s *Service) Login() {

}

func (s *Service) Test() string {
return "123"
}

var LoginController = &GinExtension.Action{
Action: &forms.Action{
BodyRequestModel: forms.BaseModelRequest{RequestModel: &UserPayload{}, Required: true},
Service: &Service{},
MetaInfo: &label.MetaInfo{
Tags: []string{"Auth"},
Path: NAME,
Expand Down
4 changes: 3 additions & 1 deletion extensions/servers/gin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (s *TyphoonGinServer) onRequestHandler(ginCtx *Gin.Context) {

action.OnRequest(ginCtx.Request.Method, reservedRequestPath)

ginCtx.Set(TYPHOONActionService, action.GetService())

requestLogger = log.Patch(requestLogger, log.D{"controller": action.GetName()})
requestContext = NewServerCtx(requestContext, s)

Expand Down Expand Up @@ -154,7 +156,7 @@ func (s *TyphoonGinServer) Init() interfaces.ServerInterface {
ginSwagger.DefaultModelsExpandDepth(-1)))

s.server.GET("/docs", func(c *Gin.Context) {
c.Writer.Write(s.GetDocs())
_, _ = c.Writer.Write(s.GetDocs())
})
}

Expand Down
2 changes: 2 additions & 0 deletions interfaces/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type ActionInterface interface {
GetResponseModels() map[int]interface{}
GetParams() interface{}

GetService() interface{}

Run(
ctx context.Context,
logger LoggerInterface,
Expand Down

0 comments on commit cf6eac8

Please sign in to comment.