Created
October 1, 2025 01:43
-
-
Save index0h/5aca49980364d678fae438dc81600716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type StrictHandlerFunc = strictgin.StrictGinHandlerFunc | |
| type StrictMiddlewareFunc = strictgin.StrictGinMiddlewareFunc | |
| func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface { | |
| return &strictHandler{ssi: ssi, middlewares: middlewares} | |
| } | |
| type strictHandler struct { | |
| ssi StrictServerInterface | |
| middlewares []StrictMiddlewareFunc | |
| } | |
| {{range .}} | |
| {{$opid := .OperationId}} | |
| // {{$opid}} operation middleware | |
| func (sh *strictHandler) {{.OperationId}}(ctx *gin.Context{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params {{.OperationId}}Params{{end}}) { | |
| var request {{$opid | ucFirst}}RequestObject | |
| {{range .PathParams -}} | |
| request.{{.GoName}} = {{.GoVariableName}} | |
| {{end -}} | |
| {{if .RequiresParamObject -}} | |
| request.Params = params | |
| {{end -}} | |
| {{ if .HasMaskedRequestContentTypes -}} | |
| request.ContentType = ctx.ContentType() | |
| {{end -}} | |
| {{$multipleBodies := gt (len .Bodies) 1 -}} | |
| {{range .Bodies -}} | |
| {{if $multipleBodies}}if strings.HasPrefix(ctx.GetHeader("Content-Type"), "{{.ContentType}}") { {{end}} | |
| {{if .IsJSON }} | |
| var body {{$opid}}{{.NameTag}}RequestBody | |
| if err := ctx.ShouldBindJSON(&body); err != nil { | |
| ctx.Status(http.StatusBadRequest) | |
| ctx.Error(err) | |
| return | |
| } | |
| request.{{if $multipleBodies}}{{.NameTag}}{{end}}Body = &body | |
| {{else if eq .NameTag "Formdata" -}} | |
| if err := ctx.Request.ParseForm(); err != nil { | |
| ctx.Error(err) | |
| return | |
| } | |
| var body {{$opid}}{{.NameTag}}RequestBody | |
| if err := runtime.BindForm(&body, ctx.Request.Form, nil, nil); err != nil { | |
| ctx.Error(err) | |
| return | |
| } | |
| request.{{if $multipleBodies}}{{.NameTag}}{{end}}Body = &body | |
| {{else if eq .NameTag "Multipart" -}} | |
| {{if eq .ContentType "multipart/form-data" -}} | |
| if reader, err := ctx.Request.MultipartReader(); err == nil { | |
| request.{{if $multipleBodies}}{{.NameTag}}{{end}}Body = reader | |
| } else { | |
| ctx.Error(err) | |
| return | |
| } | |
| {{else -}} | |
| if _, params, err := mime.ParseMediaType(ctx.Request.Header.Get("Content-Type")); err != nil { | |
| ctx.Error(err) | |
| return | |
| } else if boundary := params["boundary"]; boundary == "" { | |
| ctx.Error(http.ErrMissingBoundary) | |
| return | |
| } else { | |
| request.{{if $multipleBodies}}{{.NameTag}}{{end}}Body = multipart.NewReader(ctx.Request.Body, boundary) | |
| } | |
| {{end -}} | |
| {{else if eq .NameTag "Text" -}} | |
| data, err := io.ReadAll(ctx.Request.Body) | |
| if err != nil { | |
| ctx.Error(err) | |
| return | |
| } | |
| body := {{$opid}}{{.NameTag}}RequestBody(data) | |
| request.{{if $multipleBodies}}{{.NameTag}}{{end}}Body = &body | |
| {{else -}} | |
| request.{{if $multipleBodies}}{{.NameTag}}{{end}}Body = ctx.Request.Body | |
| {{end}}{{/* if eq .NameTag "JSON" */ -}} | |
| {{if $multipleBodies}}}{{end}} | |
| {{end}}{{/* range .Bodies */}} | |
| handler := func(ctx *gin.Context, request interface{}) (interface{}, error) { | |
| return sh.ssi.{{.OperationId}}(ctx, request.({{$opid | ucFirst}}RequestObject)) | |
| } | |
| for _, middleware := range sh.middlewares { | |
| handler = middleware(handler, "{{.OperationId}}") | |
| } | |
| response, err := handler(ctx, request) | |
| // KOKOKO | |
| if err != nil { | |
| ctx.Error(err) | |
| ctx.Status(http.StatusInternalServerError) | |
| } else if validResponse, ok := response.({{$opid | ucFirst}}ResponseObject); ok { | |
| if err := validResponse.Visit{{$opid}}Response(ctx.Writer); err != nil { | |
| ctx.Error(err) | |
| } | |
| } else if response != nil { | |
| ctx.Error(fmt.Errorf("unexpected response type: %T", response)) | |
| } | |
| } | |
| {{end}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment