diff --git a/templates/server/configureapi.gotmpl b/templates/server/configureapi.gotmpl new file mode 100644 index 0000000000000000000000000000000000000000..a05e099f7b300257697d4a56eb02b75d3753f623_dGVtcGxhdGVzL3NlcnZlci9jb25maWd1cmVhcGkuZ290bXBs --- /dev/null +++ b/templates/server/configureapi.gotmpl @@ -0,0 +1,156 @@ +// This file is safe to edit. Once it exists it will not be overwritten + +{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} + + +package {{ .APIPackage }} + +import ( + "context" + "crypto/tls" + "io" + "log" + "net/http" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" + "github.com/go-openapi/runtime/middleware" + "github.com/go-openapi/runtime/security" + + {{ imports .DefaultImports }} + {{ imports .Imports }} +) + +{{ with .GenOpts }} +//go:generate swagger generate server --target {{ .TargetPath }} --name {{ .Name }} --spec {{ .SpecPath }} +{{- if .APIPackage }}{{ if ne .APIPackage "operations" }} --api-package {{ .APIPackage }}{{ end }}{{ end }} +{{- if .ModelPackage }}{{ if ne .ModelPackage "models" }} --model-package {{ .ModelPackage }}{{ end }}{{ end }} +{{- if .ServerPackage }}{{ if ne .ServerPackage "restapi"}} --server-package {{ .ServerPackage }}{{ end }}{{ end }} +{{- if .ClientPackage }}{{ if ne .ClientPackage "client" }} --client-package {{ .ClientPackage }}{{ end }}{{ end }} +{{- if .TemplateDir }} --template-dir {{ .TemplateDir }}{{ end }} +{{- range .Operations }} --operation {{ . }}{{ end }} +{{- range .Tags }} --tags {{ . }}{{ end }} +{{- if .Principal }} --principal {{ .Principal }}{{ end }} +{{- if .DefaultScheme }}{{ if ne .DefaultScheme "http" }} --default-scheme {{ .DefaultScheme }}{{ end }}{{ end }} +{{- range .Models }} --model {{ . }}{{ end }} +{{- if or (not .IncludeModel) (not .IncludeValidator) }} --skip-models{{ end }} +{{- if or (not .IncludeHandler) (not .IncludeParameters ) (not .IncludeResponses) }} --skip-operations{{ end }} +{{- if not .IncludeSupport }} --skip-support{{ end }} +{{- if not .IncludeMain }} --exclude-main{{ end }} +{{- if .ExcludeSpec }} --exclude-spec{{ end }} +{{- if .DumpData }} --dump-data{{ end }} +{{- if .StrictResponders }} --strict-responders{{ end }} +{{ end }} +func configureFlags(api *{{.Package}}.{{ pascalize .Name }}API) { + // api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... } +} + +func configureAPI(api *{{.Package}}.{{ pascalize .Name }}API) http.Handler { + // configure the api here + api.ServeError = errors.ServeError + + // Set your custom logger if needed. Default one is log.Printf + // Expected interface func(string, ...interface{}) + // + // Example: + // api.Logger = log.Printf + + api.UseSwaggerUI() + // To continue using redoc as your UI, uncomment the following line + // api.UseRedoc() + + {{ range .Consumes }} + {{- if .Implementation }} + api.{{ pascalize .Name }}Consumer = {{ .Implementation }} + {{- else }} + api.{{ pascalize .Name }}Consumer = runtime.ConsumerFunc(func(r io.Reader, target interface{}) error { + return errors.NotImplemented("{{.Name}} consumer has not yet been implemented") + }) + {{- end }} + {{- end }} + {{ range .Produces }} + {{- if .Implementation }} + api.{{ pascalize .Name }}Producer = {{ .Implementation }} + {{- else }} + api.{{ pascalize .Name }}Producer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error { + return errors.NotImplemented("{{.Name}} producer has not yet been implemented") + }) + {{- end }} + {{- end}} + {{ range .SecurityDefinitions }} + {{- if .IsBasicAuth }} + // Applies when the Authorization header is set with the Basic scheme + if api.{{ pascalize .ID }}Auth == nil { + api.{{ pascalize .ID }}Auth = func(user string, pass string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { + return nil, errors.NotImplemented("basic auth ({{ .ID }}) has not yet been implemented") + } + } + {{- else if .IsAPIKeyAuth }} + // Applies when the "{{ .Name }}" {{ .Source }} is set + if api.{{ pascalize .ID }}Auth == nil { + api.{{ pascalize .ID }}Auth = func(token string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { + return nil, errors.NotImplemented("api key auth ({{ .ID }}) {{.Name}} from {{.Source}} param [{{ .Name }}] has not yet been implemented") + } + } + {{- else if .IsOAuth2 }} + if api.{{ pascalize .ID }}Auth == nil { + api.{{ pascalize .ID }}Auth = func(token string, scopes []string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { + return nil, errors.NotImplemented("oauth2 bearer auth ({{ .ID }}) has not yet been implemented") + } + } + {{- end }} + {{- end }} + {{- if .SecurityDefinitions }} + + // Set your custom authorizer if needed. Default one is security.Authorized() + // Expected interface runtime.Authorizer + // + // Example: + // api.APIAuthorizer = security.Authorized() + {{- end }} + {{- $package := .Package }} + {{- range .Operations }} + if api.{{ if ne .Package $package }}{{ pascalize .Package }}{{ end }}{{ pascalize .Name }}Handler == nil { + api.{{ if ne .Package $package }}{{pascalize .Package}}{{ end }}{{ pascalize .Name }}Handler = + {{- .PackageAlias }}.{{- pascalize .Name }}HandlerFunc(func(params {{ .PackageAlias }}.{{- pascalize .Name }}Params + {{- if $.GenOpts.StrictResponders }} + {{- if .Authorized}}, principal {{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}{{end}}) {{.Package}}.{{ pascalize .Name }}Responder { + return {{.Package}}.{{ pascalize .Name }}NotImplemented() + {{ else }} + {{- if .Authorized}}, principal {{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}{{end}}) middleware.Responder { + return middleware.NotImplemented("operation {{ .Package}}.{{pascalize .Name}} has not yet been implemented") + {{ end -}} + }) + } + {{- end }} + + api.PreServerShutdown = func() { } + + api.ServerShutdown = func() { } + + return setupGlobalMiddleware(api.Serve(setupMiddlewares)) +} + +// The TLS configuration before HTTPS server starts. +func configureTLS(tlsConfig *tls.Config) { + // Make all necessary changes to the TLS configuration here. +} + +// As soon as server is initialized but not run yet, this function will be called. +// If you need to modify a config, store server instance to stop it individually later, this is the place. +// This function can be called multiple times, depending on the number of serving schemes. +// scheme value will be set accordingly: "http", "https" or "unix" +func configureServer(s *http.Server, scheme, addr string) { +} + +// The middleware configuration is for the handler executors. These do not apply to the swagger.json document. +// The middleware executes after routing but before authentication, binding and validation +func setupMiddlewares(handler http.Handler) http.Handler { + return handler +} + +// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document. +// So this is a good place to plug in a panic handling middleware, logging and metrics +func setupGlobalMiddleware(handler http.Handler) http.Handler { + return handler +}