github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/generator/templates/server/autoconfigureapi.gotmpl (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  // Auto configures api handlers Implementations.
     3  
     4  {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
     5  
     6  
     7  package {{ .APIPackage }}
     8  
     9  import (
    10    "context"
    11    "crypto/tls"
    12    "io"
    13    "log"
    14    "net/http"
    15  
    16    "github.com/go-openapi/errors"
    17    "github.com/go-openapi/runtime"
    18    "github.com/go-openapi/runtime/middleware"
    19    "github.com/go-openapi/runtime/security"
    20  
    21    {{ imports .DefaultImports }}
    22    {{ imports .Imports }}
    23  )
    24  
    25  {{ with .GenOpts }}
    26  //go:generate swagger generate server --target {{ .TargetPath }} --name {{ .Name }} --spec {{ .SpecPath }}
    27  {{- if .APIPackage }}{{ if ne .APIPackage "operations" }} --api-package {{ .APIPackage }}{{ end }}{{ end }}
    28  {{- if .ModelPackage }}{{ if ne .ModelPackage "models" }} --model-package {{ .ModelPackage }}{{ end }}{{ end }}
    29  {{- if .ServerPackage }}{{ if ne .ServerPackage "restapi"}} --server-package {{ .ServerPackage }}{{ end }}{{ end }}
    30  {{- if .ClientPackage }}{{ if ne .ClientPackage "client" }} --client-package {{ .ClientPackage }}{{ end }}{{ end }}
    31  {{- if .ImplementationPackage }} --implementation-package {{ .ImplementationPackage }}{{ end }}
    32  {{- if .TemplateDir }} --template-dir {{ .TemplateDir }}{{ end }}
    33  {{- range .Operations }} --operation {{ . }}{{ end }}
    34  {{- range .Tags }} --tags {{ . }}{{ end }}
    35  {{- if .Principal }} --principal {{ .Principal }}{{ end }}
    36  {{- if .DefaultScheme }}{{ if ne .DefaultScheme "http" }} --default-scheme {{ .DefaultScheme }}{{ end }}{{ end }}
    37  {{- range .Models }} --model {{ . }}{{ end }}
    38  {{- if or (not .IncludeModel) (not .IncludeValidator) }} --skip-models{{ end }}
    39  {{- if or (not .IncludeHandler) (not .IncludeParameters ) (not .IncludeResponses) }} --skip-operations{{ end }}
    40  {{- if not .IncludeSupport }} --skip-support{{ end }}
    41  {{- if not .IncludeMain }} --exclude-main{{ end }}
    42  {{- if .ExcludeSpec }} --exclude-spec{{ end }}
    43  {{- if .DumpData }} --dump-data{{ end }}
    44  {{- if .StrictResponders }} --strict-responders{{ end }}
    45  {{ end }}
    46  
    47  // This file auto configures the api backend implementation.
    48  // {{.ImplementationPackageAlias}} package must already exist.
    49  // {{.ImplementationPackageAlias}}.New() is implemented by user, and must return an object
    50  // or interface that implements Handler interface defined below. 
    51  var Impl Handler = {{.ImplementationPackageAlias}}.New()
    52  
    53  // Handler handles all api server backend configurations and requests
    54  type Handler interface{
    55  {{- if .SecurityDefinitions }}
    56    Authable
    57  {{- end }}
    58    Configurable
    59  {{ range .OperationGroups -}}
    60    {{ pascalize .Name }}Handler
    61  {{ end -}}
    62  }
    63  
    64  // Configurable handles all server configurations
    65  type Configurable interface {
    66    ConfigureFlags(api *{{.APIPackageAlias}}.{{ pascalize .Name }}API)
    67    ConfigureTLS(tlsConfig *tls.Config)
    68    ConfigureServer(s *http.Server, scheme, addr string)
    69    CustomConfigure(api *{{.APIPackageAlias}}.{{ pascalize .Name }}API)
    70    SetupMiddlewares(handler http.Handler) http.Handler
    71    SetupGlobalMiddleware(handler http.Handler) http.Handler
    72  }
    73  
    74  {{- if .SecurityDefinitions }}
    75  // Authable handles server authentication
    76  type Authable interface{
    77    {{- range .SecurityDefinitions }}
    78      {{- if .IsBasicAuth }}
    79    // Applies when the Authorization header is set with the Basic scheme
    80    {{ pascalize .ID }}Auth(user string, pass string) ({{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}, error)
    81      {{- else if .IsAPIKeyAuth }}
    82    // Applies when the "{{ .Name }}" {{ .Source }} is set
    83    {{ pascalize .ID }}Auth(token string) ({{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}, error)
    84      {{- else if .IsOAuth2 }}
    85    {{ pascalize .ID }}Auth(token string, scopes []string) ({{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}, error)
    86      {{- end }}
    87    {{- end }}
    88  }
    89  {{- end }}
    90  
    91  {{- $package := .Package }}
    92  {{- $apipackagealias := .APIPackageAlias }}
    93  {{ range .OperationGroups -}}
    94  /* {{ pascalize .Name }}Handler {{ .Description }} */
    95  type {{ pascalize .Name }}Handler interface {
    96  {{ range .Operations -}}
    97  	{{ if .Summary -}}
    98  	/* {{ pascalize .Name }} {{ .Summary }} */
    99  	{{ else if .Description -}}
   100  	/* {{ pascalize .Name }} {{ .Description }} */
   101  	{{ end -}}
   102  	{{ pascalize .Name }}(params {{ if ne .Package $package }}{{ .PackageAlias }}{{ else }}{{- $apipackagealias }}{{ end }}.
   103      {{- pascalize .Name }}Params {{- if .Authorized}}, principal {{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}{{end}})
   104      {{- if $.GenOpts.StrictResponders }} {{.Package}}.{{ pascalize .Name }}Responder {{ else }} middleware.Responder {{ end }}
   105  {{ end -}}
   106  }
   107  {{ end }}
   108  
   109  func configureFlags(api *{{.APIPackageAlias}}.{{ pascalize .Name }}API) {
   110    Impl.ConfigureFlags(api)
   111  }
   112  
   113  func configureAPI(api *{{.APIPackageAlias}}.{{ pascalize .Name }}API) http.Handler {
   114  
   115    api.ServeError = errors.ServeError
   116  
   117    api.UseSwaggerUI()
   118  
   119    {{ range .Consumes }}
   120      {{- if .Implementation }}
   121    api.{{ pascalize .Name }}Consumer = {{ .Implementation }}
   122      {{- else }}
   123    api.{{ pascalize .Name }}Consumer = runtime.ConsumerFunc(func(r io.Reader, target interface{}) error {
   124      return Impl.{{ pascalize .Name }}Consume(r, target)
   125    })
   126      {{- end }}
   127    {{- end }}
   128    {{ range .Produces }}
   129      {{- if .Implementation }}
   130    api.{{ pascalize .Name }}Producer = {{ .Implementation }}
   131      {{- else }}
   132    api.{{ pascalize .Name }}Producer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
   133      return Impl.{{ pascalize .Name }}Produce(w, target)
   134    })
   135      {{- end }}
   136    {{- end}}
   137    {{ range .SecurityDefinitions }}
   138      {{- if .IsBasicAuth }}
   139    // Applies when the Authorization header is set with the Basic scheme
   140    api.{{ pascalize .ID }}Auth = func(user string, pass string) ({{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}, error) {
   141      return Impl.{{ pascalize .ID }}Auth(user, pass)
   142    }
   143    
   144      {{- else if .IsAPIKeyAuth }}
   145    // Applies when the "{{ .Name }}" {{ .Source }} is set
   146    api.{{ pascalize .ID }}Auth = func(token string) ({{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}, error) {
   147        return Impl.{{ pascalize .ID }}Auth(token)
   148    }
   149      {{- else if .IsOAuth2 }}
   150    api.{{ pascalize .ID }}Auth = func(token string, scopes []string) ({{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}, error) {
   151      return Impl.{{ pascalize .ID }}Auth(token, scopes)
   152    }
   153      {{- end }}
   154    {{- end }}
   155  
   156    {{- $package := .Package }}
   157    {{- $apipackagealias := .APIPackageAlias }}
   158    {{ range .Operations }}
   159      api.{{ if ne .Package $package }}{{pascalize .Package}}{{ end }}{{ pascalize .Name }}Handler =
   160      {{- if ne .Package $package }}
   161        {{- .PackageAlias }}.{{- pascalize .Name }}HandlerFunc(func(params {{ .PackageAlias }}.{{- pascalize .Name }}Params
   162      {{- else }}
   163        {{- $apipackagealias }}.{{- pascalize .Name }}HandlerFunc(func(params {{ $apipackagealias }}.{{- pascalize .Name }}Params
   164      {{- end }}
   165      {{- if .Authorized}}, principal {{ if .PrincipalIsNullable }}*{{ end }}{{.Principal}}{{end}}) 
   166        {{- if $.GenOpts.StrictResponders }} {{.Package}}.{{ pascalize .Name }}Responder { {{ else }} middleware.Responder { {{ end }}
   167        return Impl.{{ pascalize .Name }}(params {{- if .Authorized}}, principal {{ end }})
   168      })
   169    {{- end }}
   170  
   171    api.PreServerShutdown = func() {  }
   172  
   173    api.ServerShutdown = func() {  }
   174  
   175    // CustomConfigure can override or add to configurations set above
   176    Impl.CustomConfigure(api)
   177  
   178    return setupGlobalMiddleware(api.Serve(setupMiddlewares))
   179  }
   180  
   181  // The TLS configuration before HTTPS server starts.
   182  func configureTLS(tlsConfig *tls.Config) {
   183    // Make all necessary changes to the TLS configuration here.
   184    Impl.ConfigureTLS(tlsConfig)
   185  }
   186  
   187  // As soon as server is initialized but not run yet, this function will be called.
   188  // If you need to modify a config, store server instance to stop it individually later, this is the place.
   189  // This function can be called multiple times, depending on the number of serving schemes.
   190  // scheme value will be set accordingly: "http", "https" or "unix".
   191  func configureServer(s *http.Server, scheme, addr string) {
   192  	Impl.ConfigureServer(s, scheme, addr)
   193  }
   194  
   195  // The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
   196  // The middleware executes after routing but before authentication, binding and validation.
   197  func setupMiddlewares(handler http.Handler) http.Handler {
   198  	return Impl.SetupMiddlewares(handler)
   199  }
   200  
   201  // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
   202  // So this is a good place to plug in a panic handling middleware, logging and metrics.
   203  func setupGlobalMiddleware(handler http.Handler) http.Handler {
   204  	return Impl.SetupGlobalMiddleware(handler)
   205  }