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