github.com/6543-forks/go-swagger@v0.26.0/generator/templates/contrib/stratoscale/server/configureapi.gotmpl (about) 1 // Code generated by go-swagger; DO NOT EDIT. 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 "fmt" 15 16 "github.com/go-openapi/errors" 17 "github.com/go-openapi/loads" 18 "github.com/go-openapi/runtime" 19 "github.com/go-openapi/runtime/middleware" 20 "github.com/go-openapi/runtime/security" 21 22 {{ imports .DefaultImports }} 23 {{ imports .Imports }} 24 ) 25 {{ $package := .Package }} 26 27 type contextKey string 28 29 const AuthKey contextKey = "Auth" 30 31 {{ range .OperationGroups -}} 32 //go:generate mockery -name {{ pascalize .Name}}API -inpkg 33 34 /* {{ pascalize .Name }}API {{ .Description }} */ 35 type {{ pascalize .Name }}API interface { 36 {{ range .Operations -}} 37 {{ if .Summary -}} 38 /* {{ pascalize .Name }} {{ .Summary }} */ 39 {{ else if .Description -}} 40 /* {{ pascalize .Name }} {{ .Description }} */ 41 {{ end -}} 42 {{ pascalize .Name }}(ctx context.Context, params {{.Package}}.{{ pascalize .Name }}Params) middleware.Responder 43 44 {{ end -}} 45 } 46 {{ end }} 47 48 // Config is configuration for Handler 49 type Config struct { 50 {{ range .OperationGroups -}} 51 {{ pascalize .Name }}API 52 {{ end -}} 53 Logger func(string, ...interface{}) 54 // InnerMiddleware is for the handler executors. These do not apply to the swagger.json document. 55 // The middleware executes after routing but before authentication, binding and validation 56 InnerMiddleware func(http.Handler) http.Handler 57 58 // Authorizer is used to authorize a request after the Auth function was called using the "Auth*" functions 59 // and the principal was stored in the context in the "AuthKey" context value. 60 Authorizer func(*http.Request) error 61 62 {{ range .SecurityDefinitions -}} 63 {{ if .IsBasicAuth -}} 64 // Auth{{ pascalize .ID }} for basic authentication 65 Auth{{ pascalize .ID }} func(user string, pass string) (interface{}, error) 66 {{ end -}} 67 {{ if .IsAPIKeyAuth -}} 68 // Auth{{ pascalize .ID }} Applies when the "{{ .Name }}" {{ .Source }} is set 69 Auth{{ pascalize .ID }} func(token string) (interface{}, error) 70 {{ end }} 71 {{ if .IsOAuth2 -}} 72 // Auth{{ pascalize .ID }} For OAuth2 authentication 73 Auth{{ pascalize .ID }} func(token string, scopes []string) (interface{}, error) 74 {{ end -}} 75 {{ end -}} 76 77 // Authenticator to use for all APIKey authentication 78 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 79 // Authenticator to use for all Bearer authentication 80 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 81 // Authenticator to use for all Basic authentication 82 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 83 } 84 85 // Handler returns an http.Handler given the handler configuration 86 // It mounts all the business logic implementers in the right routing. 87 func Handler(c Config) (http.Handler, error) { 88 h, _, err := HandlerAPI(c) 89 return h, err 90 } 91 92 // HandlerAPI returns an http.Handler given the handler configuration 93 // and the corresponding *{{ pascalize .Name }} instance. 94 // It mounts all the business logic implementers in the right routing. 95 func HandlerAPI(c Config) (http.Handler, *{{.Package}}.{{ pascalize .Name }}API, error) { 96 spec, err := loads.Analyzed(swaggerCopy(SwaggerJSON), "") 97 if err != nil { 98 return nil, nil, fmt.Errorf("analyze swagger: %v", err) 99 } 100 api := {{.Package}}.New{{ pascalize .Name }}API(spec) 101 api.ServeError = errors.ServeError 102 api.Logger = c.Logger 103 104 if c.APIKeyAuthenticator != nil { 105 api.APIKeyAuthenticator = c.APIKeyAuthenticator 106 } 107 if c.BasicAuthenticator != nil { 108 api.BasicAuthenticator = c.BasicAuthenticator 109 } 110 if c.BearerAuthenticator != nil { 111 api.BearerAuthenticator = c.BearerAuthenticator 112 } 113 114 {{ range .Consumes -}} 115 {{ if .Implementation -}} 116 api.{{ pascalize .Name }}Consumer = {{ .Implementation }} 117 {{ else }} 118 api.{{ pascalize .Name }}Consumer = runtime.ConsumerFunc(func(r io.Reader, target interface{}) error { 119 return errors.NotImplemented("{{.Name}} consumer has not yet been implemented") 120 }) 121 {{ end -}} 122 {{ end -}} 123 {{ range .Produces -}} 124 {{ if .Implementation -}} 125 api.{{ pascalize .Name }}Producer = {{ .Implementation }} 126 {{ else -}} 127 api.{{ pascalize .Name }}Producer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error { 128 return errors.NotImplemented("{{.Name}} producer has not yet been implemented") 129 }) 130 {{ end -}} 131 {{ end -}} 132 133 {{ range .SecurityDefinitions -}} 134 {{ if .IsBasicAuth -}} 135 api.{{ pascalize .ID }}Auth = func(user string, pass string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { 136 if c.Auth{{ pascalize .ID }} == nil { 137 return "", nil 138 } 139 return c.Auth{{ pascalize .ID }}(user, pass) 140 } 141 {{ end -}} 142 {{ if .IsAPIKeyAuth -}} 143 api.{{ pascalize .ID }}Auth = func(token string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { 144 if c.Auth{{ pascalize .ID }} == nil { 145 return token, nil 146 } 147 return c.Auth{{ pascalize .ID }}(token) 148 } 149 {{ end }} 150 {{ if .IsOAuth2 -}} 151 api.{{ pascalize .ID }}Auth = func(token string, scopes []string) ({{if not ( eq .Principal "interface{}" )}}*{{ end }}{{.Principal}}, error) { 152 if c.Auth{{ pascalize .ID }} == nil { 153 return token, nil 154 } 155 return c.Auth{{ pascalize .ID }}(token, scopes) 156 } 157 {{ end -}} 158 {{ end -}} 159 160 {{ if .SecurityDefinitions -}} 161 api.APIAuthorizer = authorizer(c.Authorizer) 162 {{ end -}} 163 164 {{ range .Operations -}} 165 api.{{if ne .Package $package}}{{pascalize .Package}}{{end}}{{ pascalize .Name }}Handler = 166 {{- .PackageAlias }}.{{ pascalize .Name }}HandlerFunc(func(params {{.PackageAlias}}.{{ pascalize .Name }}Params{{if .Authorized}}, principal interface{}{{end}}) middleware.Responder { 167 ctx := params.HTTPRequest.Context() 168 {{ if .Authorized -}} 169 ctx = storeAuth(ctx, principal) 170 {{ end -}} 171 return c.{{pascalize .Package}}API.{{pascalize .Name}}(ctx, params) 172 }) 173 {{ end -}} 174 175 api.ServerShutdown = func() { } 176 return api.Serve(c.InnerMiddleware), api, nil 177 } 178 179 // swaggerCopy copies the swagger json to prevent data races in runtime 180 func swaggerCopy(orig json.RawMessage) json.RawMessage { 181 c := make(json.RawMessage, len(orig)) 182 copy(c, orig) 183 return c 184 } 185 186 // authorizer is a helper function to implement the runtime.Authorizer interface. 187 type authorizer func(*http.Request) error 188 189 func (a authorizer) Authorize(req *http.Request, principal interface{}) error { 190 if a == nil { 191 return nil 192 } 193 ctx := storeAuth(req.Context(), principal) 194 return a(req.WithContext(ctx)) 195 } 196 197 func storeAuth(ctx context.Context, principal interface{}) context.Context { 198 return context.WithValue(ctx, AuthKey, principal) 199 }