github.com/djarvur/go-swagger@v0.18.0/examples/tutorials/custom-server/gen/restapi/operations/greeter_api.go (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 package operations 4 5 // This file was generated by the swagger tool. 6 // Editing this file might prove futile when you re-run the swagger generate command 7 8 import ( 9 "fmt" 10 "net/http" 11 "strings" 12 13 errors "github.com/go-openapi/errors" 14 loads "github.com/go-openapi/loads" 15 runtime "github.com/go-openapi/runtime" 16 middleware "github.com/go-openapi/runtime/middleware" 17 security "github.com/go-openapi/runtime/security" 18 spec "github.com/go-openapi/spec" 19 strfmt "github.com/go-openapi/strfmt" 20 "github.com/go-openapi/swag" 21 ) 22 23 // NewGreeterAPI creates a new Greeter instance 24 func NewGreeterAPI(spec *loads.Document) *GreeterAPI { 25 return &GreeterAPI{ 26 handlers: make(map[string]map[string]http.Handler), 27 formats: strfmt.Default, 28 defaultConsumes: "application/json", 29 defaultProduces: "application/json", 30 customConsumers: make(map[string]runtime.Consumer), 31 customProducers: make(map[string]runtime.Producer), 32 ServerShutdown: func() {}, 33 spec: spec, 34 ServeError: errors.ServeError, 35 BasicAuthenticator: security.BasicAuth, 36 APIKeyAuthenticator: security.APIKeyAuth, 37 BearerAuthenticator: security.BearerAuth, 38 JSONConsumer: runtime.JSONConsumer(), 39 TxtProducer: runtime.TextProducer(), 40 GetGreetingHandler: GetGreetingHandlerFunc(func(params GetGreetingParams) middleware.Responder { 41 return middleware.NotImplemented("operation GetGreeting has not yet been implemented") 42 }), 43 } 44 } 45 46 /*GreeterAPI the greeter API */ 47 type GreeterAPI struct { 48 spec *loads.Document 49 context *middleware.Context 50 handlers map[string]map[string]http.Handler 51 formats strfmt.Registry 52 customConsumers map[string]runtime.Consumer 53 customProducers map[string]runtime.Producer 54 defaultConsumes string 55 defaultProduces string 56 Middleware func(middleware.Builder) http.Handler 57 58 // BasicAuthenticator generates a runtime.Authenticator from the supplied basic auth function. 59 // It has a default implemention in the security package, however you can replace it for your particular usage. 60 BasicAuthenticator func(security.UserPassAuthentication) runtime.Authenticator 61 // APIKeyAuthenticator generates a runtime.Authenticator from the supplied token auth function. 62 // It has a default implemention in the security package, however you can replace it for your particular usage. 63 APIKeyAuthenticator func(string, string, security.TokenAuthentication) runtime.Authenticator 64 // BearerAuthenticator generates a runtime.Authenticator from the supplied bearer token auth function. 65 // It has a default implemention in the security package, however you can replace it for your particular usage. 66 BearerAuthenticator func(string, security.ScopedTokenAuthentication) runtime.Authenticator 67 68 // JSONConsumer registers a consumer for a "application/json" mime type 69 JSONConsumer runtime.Consumer 70 71 // TxtProducer registers a producer for a "text/plain" mime type 72 TxtProducer runtime.Producer 73 74 // GetGreetingHandler sets the operation handler for the get greeting operation 75 GetGreetingHandler GetGreetingHandler 76 77 // ServeError is called when an error is received, there is a default handler 78 // but you can set your own with this 79 ServeError func(http.ResponseWriter, *http.Request, error) 80 81 // ServerShutdown is called when the HTTP(S) server is shut down and done 82 // handling all active connections and does not accept connections any more 83 ServerShutdown func() 84 85 // Custom command line argument groups with their descriptions 86 CommandLineOptionsGroups []swag.CommandLineOptionsGroup 87 88 // User defined logger function. 89 Logger func(string, ...interface{}) 90 } 91 92 // SetDefaultProduces sets the default produces media type 93 func (o *GreeterAPI) SetDefaultProduces(mediaType string) { 94 o.defaultProduces = mediaType 95 } 96 97 // SetDefaultConsumes returns the default consumes media type 98 func (o *GreeterAPI) SetDefaultConsumes(mediaType string) { 99 o.defaultConsumes = mediaType 100 } 101 102 // SetSpec sets a spec that will be served for the clients. 103 func (o *GreeterAPI) SetSpec(spec *loads.Document) { 104 o.spec = spec 105 } 106 107 // DefaultProduces returns the default produces media type 108 func (o *GreeterAPI) DefaultProduces() string { 109 return o.defaultProduces 110 } 111 112 // DefaultConsumes returns the default consumes media type 113 func (o *GreeterAPI) DefaultConsumes() string { 114 return o.defaultConsumes 115 } 116 117 // Formats returns the registered string formats 118 func (o *GreeterAPI) Formats() strfmt.Registry { 119 return o.formats 120 } 121 122 // RegisterFormat registers a custom format validator 123 func (o *GreeterAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) { 124 o.formats.Add(name, format, validator) 125 } 126 127 // Validate validates the registrations in the GreeterAPI 128 func (o *GreeterAPI) Validate() error { 129 var unregistered []string 130 131 if o.JSONConsumer == nil { 132 unregistered = append(unregistered, "JSONConsumer") 133 } 134 135 if o.TxtProducer == nil { 136 unregistered = append(unregistered, "TxtProducer") 137 } 138 139 if o.GetGreetingHandler == nil { 140 unregistered = append(unregistered, "GetGreetingHandler") 141 } 142 143 if len(unregistered) > 0 { 144 return fmt.Errorf("missing registration: %s", strings.Join(unregistered, ", ")) 145 } 146 147 return nil 148 } 149 150 // ServeErrorFor gets a error handler for a given operation id 151 func (o *GreeterAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) { 152 return o.ServeError 153 } 154 155 // AuthenticatorsFor gets the authenticators for the specified security schemes 156 func (o *GreeterAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator { 157 158 return nil 159 160 } 161 162 // Authorizer returns the registered authorizer 163 func (o *GreeterAPI) Authorizer() runtime.Authorizer { 164 165 return nil 166 167 } 168 169 // ConsumersFor gets the consumers for the specified media types 170 func (o *GreeterAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer { 171 172 result := make(map[string]runtime.Consumer) 173 for _, mt := range mediaTypes { 174 switch mt { 175 176 case "application/json": 177 result["application/json"] = o.JSONConsumer 178 179 } 180 181 if c, ok := o.customConsumers[mt]; ok { 182 result[mt] = c 183 } 184 } 185 return result 186 187 } 188 189 // ProducersFor gets the producers for the specified media types 190 func (o *GreeterAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer { 191 192 result := make(map[string]runtime.Producer) 193 for _, mt := range mediaTypes { 194 switch mt { 195 196 case "text/plain": 197 result["text/plain"] = o.TxtProducer 198 199 } 200 201 if p, ok := o.customProducers[mt]; ok { 202 result[mt] = p 203 } 204 } 205 return result 206 207 } 208 209 // HandlerFor gets a http.Handler for the provided operation method and path 210 func (o *GreeterAPI) HandlerFor(method, path string) (http.Handler, bool) { 211 if o.handlers == nil { 212 return nil, false 213 } 214 um := strings.ToUpper(method) 215 if _, ok := o.handlers[um]; !ok { 216 return nil, false 217 } 218 if path == "/" { 219 path = "" 220 } 221 h, ok := o.handlers[um][path] 222 return h, ok 223 } 224 225 // Context returns the middleware context for the greeter API 226 func (o *GreeterAPI) Context() *middleware.Context { 227 if o.context == nil { 228 o.context = middleware.NewRoutableContext(o.spec, o, nil) 229 } 230 231 return o.context 232 } 233 234 func (o *GreeterAPI) initHandlerCache() { 235 o.Context() // don't care about the result, just that the initialization happened 236 237 if o.handlers == nil { 238 o.handlers = make(map[string]map[string]http.Handler) 239 } 240 241 if o.handlers["GET"] == nil { 242 o.handlers["GET"] = make(map[string]http.Handler) 243 } 244 o.handlers["GET"]["/hello"] = NewGetGreeting(o.context, o.GetGreetingHandler) 245 246 } 247 248 // Serve creates a http handler to serve the API over HTTP 249 // can be used directly in http.ListenAndServe(":8000", api.Serve(nil)) 250 func (o *GreeterAPI) Serve(builder middleware.Builder) http.Handler { 251 o.Init() 252 253 if o.Middleware != nil { 254 return o.Middleware(builder) 255 } 256 return o.context.APIHandler(builder) 257 } 258 259 // Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit 260 func (o *GreeterAPI) Init() { 261 if len(o.handlers) == 0 { 262 o.initHandlerCache() 263 } 264 } 265 266 // RegisterConsumer allows you to add (or override) a consumer for a media type. 267 func (o *GreeterAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) { 268 o.customConsumers[mediaType] = consumer 269 } 270 271 // RegisterProducer allows you to add (or override) a producer for a media type. 272 func (o *GreeterAPI) RegisterProducer(mediaType string, producer runtime.Producer) { 273 o.customProducers[mediaType] = producer 274 }