github.com/kaisawind/go-swagger@v0.19.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 "context" 14 "net/http" 15 "strings" 16 "fmt" 17 18 errors "github.com/go-openapi/errors" 19 middleware "github.com/go-openapi/runtime/middleware" 20 security "github.com/go-openapi/runtime/security" 21 strfmt "github.com/go-openapi/strfmt" 22 validate "github.com/go-openapi/validate" 23 swag "github.com/go-openapi/swag" 24 25 {{ range .DefaultImports }}{{ printf "%q" . }} 26 {{ end }} 27 {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }} 28 {{ end }} 29 ) 30 31 // {{ pascalize .Name }}HandlerFunc turns a function with the right signature into a {{ humanize .Name }} handler 32 type {{ pascalize .Name }}HandlerFunc func({{ pascalize .Name }}Params{{ if .Authorized }}, {{ if not ( eq .Principal "interface{}" ) }}*{{ end }}{{ .Principal }}{{ end }}) middleware.Responder 33 34 // Handle executing the request and returning a response 35 func (fn {{ pascalize .Name }}HandlerFunc) Handle(params {{ pascalize .Name }}Params{{ if .Authorized }}, principal {{ if not ( eq .Principal "interface{}" ) }}*{{ end }}{{ .Principal }}{{ end }}) middleware.Responder { 36 return fn(params{{ if .Authorized }}, principal{{ end }}) 37 } 38 39 // {{ pascalize .Name }}Handler interface for that can handle valid {{ humanize .Name }} params 40 type {{ pascalize .Name }}Handler interface { 41 Handle({{ pascalize .Name }}Params{{ if .Authorized }}, {{ if not ( eq .Principal "interface{}" ) }}*{{ end }}{{ .Principal }}{{ end }}) middleware.Responder 42 } 43 44 // New{{ pascalize .Name }} creates a new http.Handler for the {{ humanize .Name }} operation 45 func New{{ pascalize .Name }}(ctx *middleware.Context, handler {{ pascalize .Name }}Handler) *{{ pascalize .Name }} { 46 return &{{ pascalize .Name }}{Context: ctx, Handler: handler} 47 } 48 49 /*{{ pascalize .Name }} swagger:route {{ .Method }} {{ .Path }}{{ range .Tags }} {{ . }}{{ end }} {{ camelize .Name }} 50 51 {{ if .Summary }}{{ .Summary }}{{ if .Description }} 52 53 {{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ pascalize .Name }} {{ humanize .Name }} API{{ end }} 54 55 */ 56 type {{ pascalize .Name }} struct { 57 Context *middleware.Context 58 Handler {{ pascalize .Name }}Handler 59 } 60 61 func ({{ .ReceiverName }} *{{ pascalize .Name }}) ServeHTTP(rw http.ResponseWriter, r *http.Request) { 62 route, rCtx, _ := {{ .ReceiverName }}.Context.RouteInfo(r) 63 if rCtx != nil { 64 r = rCtx 65 } 66 var Params = New{{ pascalize .Name }}Params() 67 68 {{ if .Authorized }}uprinc, aCtx, err := {{ .ReceiverName }}.Context.Authorize(r, route) 69 if err != nil { 70 {{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, err) 71 return 72 } 73 if aCtx != nil { 74 r = aCtx 75 } 76 var principal {{ if not (eq .Principal "interface{}") }}*{{ end }}{{ .Principal }} 77 if uprinc != nil { 78 principal = {{ if eq .Principal "interface{}" }}uprinc{{ else }}uprinc.(*{{ .Principal }}) // this is really a {{ .Principal }}, I promise{{ end }} 79 } 80 81 {{ end }} 82 if err := {{ .ReceiverName }}.Context.BindValidRequest(r, route, &Params); err != nil { // bind params 83 {{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, err) 84 return 85 } 86 87 {{ if .Authorized }} 88 res := {{ .ReceiverName }}.Handler.Handle(Params, principal) // actually handle the request 89 {{else}} 90 res := {{ .ReceiverName }}.Handler.Handle(Params) // actually handle the request 91 {{ end }} 92 {{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, res) 93 94 } 95 96 {{ range .ExtraSchemas }} 97 // {{ .Name }} {{ template "docstring" . }} 98 // swagger:model {{ .Name }} 99 {{ template "schema" . }} 100 {{ end }}