github.com/djarvur/go-swagger@v0.18.0/generator/templates/server/configureapi.gotmpl (about) 1 // This file is safe to edit. Once it exists it will not be overwritten 2 3 4 {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} 5 6 7 package {{ .APIPackage }} 8 9 import ( 10 "context" 11 "crypto/tls" 12 "net/http" 13 "log" 14 15 errors "github.com/go-openapi/errors" 16 runtime "github.com/go-openapi/runtime" 17 middleware "github.com/go-openapi/runtime/middleware" 18 security "github.com/go-openapi/runtime/security" 19 20 {{range .DefaultImports}}{{printf "%q" .}} 21 {{end}} 22 {{range $key, $value := .Imports}}{{$key}} {{ printf "%q" $value}} 23 {{end}} 24 ) 25 {{ $package := .Package }} 26 27 {{ with .GenOpts }} 28 //go:generate swagger generate server --target {{ .TargetPath }} --name {{ .Name }} --spec {{ .SpecPath }} 29 {{- if .APIPackage }}{{ if ne .APIPackage "operations" }} --api-package {{ .APIPackage }}{{ end }}{{ end }} 30 {{- if .ModelPackage }}{{ if ne .ModelPackage "models" }} --model-package {{ .ModelPackage }}{{ end }}{{ end }} 31 {{- if .ServerPackage }}{{ if ne .ServerPackage "restapi"}} --server-package {{ .ServerPackage }}{{ end }}{{ end }} 32 {{- if .ClientPackage }}{{ if ne .ClientPackage "client" }} --client-package {{ .ClientPackage }}{{ end }}{{ end }} 33 {{- if .TemplateDir }} --template-dir {{ .TemplateDir }}{{ end }} 34 {{- range .Operations }} --operation {{ . }}{{ end }} 35 {{- range .Tags }} --tags {{ . }}{{ end }} 36 {{- if .Principal }} --principal {{ .Principal }}{{ end }} 37 {{- if .DefaultScheme }}{{ if ne .DefaultScheme "http" }} --default-scheme {{ .DefaultScheme }}{{ end }}{{ end }} 38 {{- range .Models }} --model {{ . }}{{ end }} 39 {{- if or (not .IncludeModel) (not .IncludeValidator) }} --skip-models{{ end }} 40 {{- if or (not .IncludeHandler) (not .IncludeParameters ) (not .IncludeResponses) }} --skip-operations{{ end }} 41 {{- if not .IncludeSupport }} --skip-support{{ end }} 42 {{- if not .IncludeMain }} --exclude-main{{ end }} 43 {{- if .ExcludeSpec }} --exclude-spec{{ end }} 44 {{- if .DumpData }} --dump-data{{ end }} 45 {{ end }} 46 func configureFlags(api *{{.Package}}.{{ pascalize .Name }}API) { 47 // api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... } 48 } 49 50 func configureAPI(api *{{.Package}}.{{ pascalize .Name }}API) http.Handler { 51 // configure the api here 52 api.ServeError = errors.ServeError 53 54 // Set your custom logger if needed. Default one is log.Printf 55 // Expected interface func(string, ...interface{}) 56 // 57 // Example: 58 // api.Logger = log.Printf 59 60 {{ range .Consumes }}{{ if .Implementation }}api.{{ pascalize .Name }}Consumer = {{ .Implementation }} 61 {{else}}api.{{ pascalize .Name }}Consumer = runtime.ConsumerFunc(func(r io.Reader, target interface{}) error { 62 return errors.NotImplemented("{{.Name}} consumer has not yet been implemented") 63 }){{end}} 64 {{end}} 65 {{ range .Produces }}{{ if .Implementation }}api.{{ pascalize .Name }}Producer = {{ .Implementation }} 66 {{else}}api.{{ pascalize .Name }}Producer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error { 67 return errors.NotImplemented("{{.Name}} producer has not yet been implemented") 68 }){{end}} 69 {{end}} 70 {{ range .SecurityDefinitions }} 71 {{- if .IsBasicAuth }} 72 // Applies when the Authorization header is set with the Basic scheme 73 api.{{ pascalize .ID }}Auth = func(user string, pass string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { 74 return nil, errors.NotImplemented("basic auth ({{ .ID }}) has not yet been implemented") 75 } 76 {{- else if .IsAPIKeyAuth }} 77 // Applies when the "{{ .Name }}" {{ .Source }} is set 78 api.{{ pascalize .ID }}Auth = func(token string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { 79 return nil, errors.NotImplemented("api key auth ({{ .ID }}) {{.Name}} from {{.Source}} param [{{ .Name }}] has not yet been implemented") 80 } 81 {{- else if .IsOAuth2 }} 82 api.{{ pascalize .ID }}Auth = func(token string, scopes []string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { 83 return nil, errors.NotImplemented("oauth2 bearer auth ({{ .ID }}) has not yet been implemented") 84 } 85 {{- end }} 86 {{- end }} 87 {{- if .SecurityDefinitions }} 88 89 // Set your custom authorizer if needed. Default one is security.Authorized() 90 // Expected interface runtime.Authorizer 91 // 92 // Example: 93 // api.APIAuthorizer = security.Authorized() 94 {{- end }} 95 {{range .Operations}}api.{{if ne .Package $package}}{{pascalize .Package}}{{end}}{{ pascalize .Name }}Handler = {{.Package}}.{{ pascalize .Name }}HandlerFunc(func(params {{.Package}}.{{ pascalize .Name }}Params{{if .Authorized}}, principal {{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}{{end}}) middleware.Responder { 96 return middleware.NotImplemented("operation {{if ne .Package $package}}{{ .Package}}{{end}}.{{pascalize .Name}} has not yet been implemented") 97 }) 98 {{end}} 99 100 api.ServerShutdown = func() { } 101 102 return setupGlobalMiddleware(api.Serve(setupMiddlewares)) 103 } 104 105 // The TLS configuration before HTTPS server starts. 106 func configureTLS(tlsConfig *tls.Config) { 107 // Make all necessary changes to the TLS configuration here. 108 } 109 110 // As soon as server is initialized but not run yet, this function will be called. 111 // If you need to modify a config, store server instance to stop it individually later, this is the place. 112 // This function can be called multiple times, depending on the number of serving schemes. 113 // scheme value will be set accordingly: "http", "https" or "unix" 114 func configureServer(s *http.Server, scheme, addr string) { 115 } 116 117 // The middleware configuration is for the handler executors. These do not apply to the swagger.json document. 118 // The middleware executes after routing but before authentication, binding and validation 119 func setupMiddlewares(handler http.Handler) http.Handler { 120 return handler 121 } 122 123 // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document. 124 // So this is a good place to plug in a panic handling middleware, logging and metrics 125 func setupGlobalMiddleware(handler http.Handler) http.Handler { 126 return handler 127 }