Skip to content

Commit

Permalink
Merge pull request #79 from vortex14/swagger
Browse files Browse the repository at this point in the history
Swagger
  • Loading branch information
vortex14 authored Aug 19, 2023
2 parents f96d769 + cf6eac8 commit 98bed19
Show file tree
Hide file tree
Showing 38 changed files with 1,341 additions and 1,105 deletions.
43 changes: 43 additions & 0 deletions elements/forms/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type Stats struct {
Input int64
}

type BaseModelRequest struct {
RequestModel interface{}
Required bool
Type string
}

type Action struct {
*label.MetaInfo
LOG interfaces.LoggerInterface
Expand All @@ -32,6 +38,11 @@ type Action struct {
AllowedMethods []string
handlerPath string

Service interface{}
Params interface{}
BodyRequestModel BaseModelRequest
ResponseModels map[int]interface{}

//Cn func(ctx context.Context, err error)

Controller interfaces.Controller //Controller of Action
Expand Down Expand Up @@ -212,3 +223,35 @@ func (a *Action) Run(ctx context.Context, logger interfaces.LoggerInterface) {
func (a *Action) SetLogger(logger interfaces.LoggerInterface) {
a.LOG = logger
}

func (a *Action) GetRequestModel() interface{} {
return a.BodyRequestModel.RequestModel
}

func (a *Action) GetRequestType() string {
return a.BodyRequestModel.Type
}

func (a *Action) IsRequiredRequestModel() bool {
return a.BodyRequestModel.Required
}

func (a *Action) IsValidRequestBody() bool {
status := false
if a.GetRequestModel() != nil && a.IsRequiredRequestModel() {
status = true
}
return status
}

func (a *Action) GetResponseModels() map[int]interface{} {
return a.ResponseModels
}

func (a *Action) GetParams() interface{} {
return a.Params
}

func (a *Action) GetService() interface{} {
return a.Service
}
65 changes: 63 additions & 2 deletions elements/forms/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package forms
import (
"context"
"fmt"
"github.com/getkin/kin-openapi/openapi3"
"github.com/vortex14/gotyphoon/elements/models/singleton"
"github.com/vortex14/gotyphoon/integrations/swagger"
"strconv"

// /* ignore for building amd64-linux
// ghvzExt "github.com/vortex14/gotyphoon/extensions/models/graphviz"
Expand Down Expand Up @@ -49,9 +52,13 @@ type TyphoonServer struct {
*label.MetaInfo

Port int
Host string
Schema string
IsDebug bool
IsRunning bool

ActiveSwagger bool

Level string

LOG *logrus.Entry
Expand All @@ -74,14 +81,15 @@ type TyphoonServer struct {
OnExit OnExit

LoggerOptions *log.Options
SwaggerOptions *interfaces.SwaggerOptions
TracingOptions *interfaces.TracingOptions

ArchonChIN ArchonChIN
ArchonChOut ArchonChOut

BuildGraph bool

swagger *swagger.OpenApi

// /* ignore for building amd64-linux
//
// Graph interfaces.GraphInterface
Expand All @@ -90,6 +98,16 @@ type TyphoonServer struct {

}

type ErrorResponse struct {
Message string `json:"message"`
Status bool `json:"status"`
}

func (s *TyphoonServer) GetDocs() []byte {
s.LOG.Error(Errors.ServerMethodNotImplemented.Error())
return nil
}

func (s *TyphoonServer) SetRouterGroup(resource interfaces.ResourceInterface, group interface{}) {
s.LOG.Error(Errors.ServerMethodNotImplemented.Error())
}
Expand All @@ -115,7 +133,13 @@ func (s *TyphoonServer) Init() interfaces.ServerInterface {
}

func (s *TyphoonServer) InitDocs() interfaces.ServerInterface {
s.LOG.Error(Errors.ServerMethodNotImplemented.Error())

s.swagger = swagger.ConstructorNewFromArgs(
s.Name,
s.Description,
s.Version,
[]string{s.Schema, s.Host, strconv.Itoa(s.Port)})
s.swagger.LOG = s.LOG
return s
}

Expand All @@ -139,6 +163,10 @@ func (s *TyphoonServer) InitLogger() interfaces.ServerInterface {
return s
}

//func (s *TyphoonServer) Init() {
//
//}

func (s *TyphoonServer) Run() error {
if !s.IsRunning && len(s.Resources) > 0 {
if s.OnStart == nil {
Expand Down Expand Up @@ -320,6 +348,8 @@ func (s *TyphoonServer) initActions(resource interfaces.ResourceInterface) {
action.SetLogger(actionLogger)
s.LOG.Debug(fmt.Sprintf("need serve path: %s method: %s", handlerPath, method))

s.AddSwaggerOperation(resource, action, method, handlerPath)

s.initHandler(method, handlerPath, resource)
if s.OnInitAction != nil {
s.OnInitAction(resource, action)
Expand All @@ -328,6 +358,31 @@ func (s *TyphoonServer) initActions(resource interfaces.ResourceInterface) {
}
}

func (s *TyphoonServer) AddSwaggerResponses(action interfaces.ActionInterface, operation *openapi3.Operation) {

errorResponseTitle := "Error response"
s.swagger.AddSwaggerResponse(&errorResponseTitle, 422, action, operation, &ErrorResponse{})

for status, model := range action.GetResponseModels() {
responseTitle := "response"

s.swagger.AddSwaggerResponse(&responseTitle, status, action, operation, model)
}

}

func (s *TyphoonServer) AddSwaggerOperation(
resource interfaces.ResourceInterface,
action interfaces.ActionInterface,
method, path string,
) {

operation := s.swagger.AddSwaggerOperation(resource, action, method, path)

s.AddSwaggerResponses(action, operation)

}

func (s *TyphoonServer) buildSubResources(parentPath string, newResource interfaces.ResourceInterface) {
if newResource.GetCountSubResources() > 0 {
for resourceName, subResource := range newResource.GetResources() {
Expand Down Expand Up @@ -361,6 +416,7 @@ func (s *TyphoonServer) buildSubActions(parentPath string, newResource interface
for _, method := range action.GetMethods() {
handlerPath := fmt.Sprintf("%s/%s", parentPath, name)
s.LOG.Debug("init sub action ", handlerPath)
s.AddSwaggerOperation(newResource, action, method, handlerPath)
action.SetHandlerPath(handlerPath)
if s.OnBuildSubAction != nil {
s.OnBuildSubAction(newResource, action)
Expand Down Expand Up @@ -488,3 +544,8 @@ func (s *TyphoonServer) GetServerEngine() interface{} {
func (s *TyphoonServer) SetServerEngine(server interface{}) {
s.LOG.Error(Errors.ServerMethodNotImplemented.Error())
}

func (s *TyphoonServer) GetSwagger() []byte {
return s.swagger.GetDump()

}
31 changes: 23 additions & 8 deletions elements/models/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package label
import "strings"

type MetaInfo struct {
Path string
Name string
Label string
Required bool
Path string
Name string
Label string
Required bool
Version string
Description string
Summary string
Tags []string
}

func (l *MetaInfo) GetName() string {
return l.Name
}

func (l *MetaInfo) SetLabel(label string) {
func (l *MetaInfo) SetLabel(label string) {
l.Label = label
}

Expand All @@ -30,11 +33,11 @@ func (l *MetaInfo) IsRequired() bool {
return l.Required
}

func (l *MetaInfo) SetName(name string) {
func (l *MetaInfo) SetName(name string) {
l.Name = name
}

func (l *MetaInfo) SetDescription(description string) {
func (l *MetaInfo) SetDescription(description string) {
l.Description = description
}

Expand All @@ -46,6 +49,18 @@ func (l *MetaInfo) SetPath(path string) {
l.Path = path
}

func (l *MetaInfo) SetRequired() {
func (l *MetaInfo) SetRequired() {
l.Required = true
}

func (l *MetaInfo) GetTags() []string {
return l.Tags
}

func (l *MetaInfo) SetTag(tag string) {
l.Tags = append(l.Tags, tag)
}

func (l *MetaInfo) GetSummary() string {
return l.Summary
}
5 changes: 3 additions & 2 deletions errors/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ var (

ActionAddMethodNotImplemented = Errors.New("action add method not implemented")

ActionPathNotFound = Errors.New("action path not found in context. need set RoutePath")
ActionFailed = Errors.New("action failed")
ActionPathNotFound = Errors.New("action path not found in context. need set RoutePath")
ActionFailed = Errors.New("action failed")
ActionErrRequestModel = Errors.New("request model isn't correct for action")

ServerOnHandlerMethodNotImplemented = Errors.New("server on handler method not implemented")

Expand Down
2 changes: 2 additions & 0 deletions extensions/servers/gin/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gin

import (
"context"

"github.com/vortex14/gotyphoon/elements/forms"
Errors "github.com/vortex14/gotyphoon/errors"
"github.com/vortex14/gotyphoon/interfaces"
Expand All @@ -13,6 +14,7 @@ type Action struct {

GinController Controller
GinSController ServerController
RequestModel interface{}
}

func (a *Action) AddMethod(name string) {
Expand Down
35 changes: 35 additions & 0 deletions extensions/servers/gin/cmd/params/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

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

type Person struct {
Id string `form:"id" binding:"required,uuid"`
Name string `form:"name" binding:"required" json:"name"`
}

func main() {
route := gin.Default()
route.GET("/data", func(c *gin.Context) {

//println(fmt.Sprintf("%+v", c.Request.URL.Query()))
var person Person

//values := c.Request.URL.Query()
//if err := mapForm(&person, values); err != nil {
// println(">>>>>>", err)
//} else {
// println(">>>> >!> !>>! >! ")
//}

//e := c.ShouldBindWith(&person, binding.Query)
//println("!!!! <<<<<< ", e.Error())
if err := c.BindQuery(&person); err != nil {
c.JSON(400, gin.H{"msg": err.Error()})
return
}
c.JSON(200, gin.H{"name": person.Name})
})
route.Run(":8088")
}
3 changes: 1 addition & 2 deletions extensions/servers/gin/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func ConstructorCreateBaseLocalhostServer(name, description string, port int) *TyphoonGinServer {

loggerOpt, tracingOpt, swaggerOpt := ConstructorLocalhostOptions(name)
loggerOpt, tracingOpt, _ := ConstructorLocalhostOptions(name)

return &TyphoonGinServer{
TyphoonServer: &forms.TyphoonServer{
Expand All @@ -21,7 +21,6 @@ func ConstructorCreateBaseLocalhostServer(name, description string, port int) *T
},
TracingOptions: tracingOpt,
LoggerOptions: loggerOpt,
SwaggerOptions: swaggerOpt,
},
}
}
Loading

0 comments on commit 98bed19

Please sign in to comment.