github.com/6543-forks/go-swagger@v0.26.0/generator/templates/server/operation.gotmpl (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 4 {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} 5 6 7 package {{ .Package }} 8 9 // This file was generated by the swagger tool. 10 // Editing this file might prove futile when you re-run the generate command 11 12 import ( 13 "net/http" 14 15 "github.com/go-openapi/errors" 16 "github.com/go-openapi/runtime/middleware" 17 "github.com/go-openapi/runtime/security" 18 "github.com/go-openapi/strfmt" 19 "github.com/go-openapi/swag" 20 "github.com/go-openapi/validate" 21 22 {{ imports .DefaultImports }} 23 {{ imports .Imports }} 24 ) 25 26 // {{ pascalize .Name }}HandlerFunc turns a function with the right signature into a {{ humanize .Name }} handler 27 type {{ pascalize .Name }}HandlerFunc func({{ pascalize .Name }}Params{{ if .Authorized }}, {{ if not ( eq .Principal "interface{}" ) }}*{{ end }}{{ .Principal }}{{ end }}) {{ if $.StrictResponders }} {{ pascalize .Name }}Responder {{else}} middleware.Responder {{end}} 28 29 // Handle executing the request and returning a response 30 func (fn {{ pascalize .Name }}HandlerFunc) Handle(params {{ pascalize .Name }}Params{{ if .Authorized }}, principal {{ if not ( eq .Principal "interface{}" ) }}*{{ end }}{{ .Principal }}{{ end }}) {{ if $.StrictResponders }} {{ pascalize .Name }}Responder {{else}} middleware.Responder {{end}} { 31 return fn(params{{ if .Authorized }}, principal{{ end }}) 32 } 33 34 // {{ pascalize .Name }}Handler interface for that can handle valid {{ humanize .Name }} params 35 type {{ pascalize .Name }}Handler interface { 36 Handle({{ pascalize .Name }}Params{{ if .Authorized }}, {{ if not ( eq .Principal "interface{}" ) }}*{{ end }}{{ .Principal }}{{ end }}) {{ if $.StrictResponders }} {{ pascalize .Name }}Responder {{else}} middleware.Responder {{end}} 37 } 38 39 // New{{ pascalize .Name }} creates a new http.Handler for the {{ humanize .Name }} operation 40 func New{{ pascalize .Name }}(ctx *middleware.Context, handler {{ pascalize .Name }}Handler) *{{ pascalize .Name }} { 41 return &{{ pascalize .Name }}{Context: ctx, Handler: handler} 42 } 43 44 /*{{ pascalize .Name }} swagger:route {{ .Method }} {{ .Path }}{{ range .Tags }} {{ . }}{{ end }} {{ camelize .Name }} 45 46 {{ if .Summary }}{{ .Summary }}{{ if .Description }} 47 48 {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ pascalize .Name }} {{ humanize .Name }} API{{ end }} 49 50 */ 51 type {{ pascalize .Name }} struct { 52 Context *middleware.Context 53 Handler {{ pascalize .Name }}Handler 54 } 55 56 func ({{ .ReceiverName }} *{{ pascalize .Name }}) ServeHTTP(rw http.ResponseWriter, r *http.Request) { 57 route, rCtx, _ := {{ .ReceiverName }}.Context.RouteInfo(r) 58 if rCtx != nil { 59 r = rCtx 60 } 61 var Params = New{{ pascalize .Name }}Params() 62 63 {{ if .Authorized }}uprinc, aCtx, err := {{ .ReceiverName }}.Context.Authorize(r, route) 64 if err != nil { 65 {{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, err) 66 return 67 } 68 if aCtx != nil { 69 r = aCtx 70 } 71 var principal {{ if not (eq .Principal "interface{}") }}*{{ end }}{{ .Principal }} 72 if uprinc != nil { 73 principal = {{ if eq .Principal "interface{}" }}uprinc{{ else }}uprinc.(*{{ .Principal }}) // this is really a {{ .Principal }}, I promise{{ end }} 74 } 75 76 {{ end }} 77 if err := {{ .ReceiverName }}.Context.BindValidRequest(r, route, &Params); err != nil { // bind params 78 {{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, err) 79 return 80 } 81 82 {{ if .Authorized }} 83 res := {{ .ReceiverName }}.Handler.Handle(Params, principal) // actually handle the request 84 {{else}} 85 res := {{ .ReceiverName }}.Handler.Handle(Params) // actually handle the request 86 {{ end }} 87 {{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, res) 88 89 } 90 91 {{ range .ExtraSchemas }} 92 // {{ .Name }} {{ template "docstring" . }} 93 // 94 // swagger:model {{ .Name }} 95 {{- template "schema" . }} 96 {{- end }}