1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 /* 16 Package middleware provides the library with helper functions for serving swagger APIs. 17 18 Pseudo middleware handler 19 20 import ( 21 "net/http" 22 23 "github.com/go-openapi/errors" 24 ) 25 26 func newCompleteMiddleware(ctx *Context) http.Handler { 27 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { 28 // use context to lookup routes 29 if matched, ok := ctx.RouteInfo(r); ok { 30 31 if matched.NeedsAuth() { 32 if _, err := ctx.Authorize(r, matched); err != nil { 33 ctx.Respond(rw, r, matched.Produces, matched, err) 34 return 35 } 36 } 37 38 bound, validation := ctx.BindAndValidate(r, matched) 39 if validation != nil { 40 ctx.Respond(rw, r, matched.Produces, matched, validation) 41 return 42 } 43 44 result, err := matched.Handler.Handle(bound) 45 if err != nil { 46 ctx.Respond(rw, r, matched.Produces, matched, err) 47 return 48 } 49 50 ctx.Respond(rw, r, matched.Produces, matched, result) 51 return 52 } 53 54 // Not found, check if it exists in the other methods first 55 if others := ctx.AllowedMethods(r); len(others) > 0 { 56 ctx.Respond(rw, r, ctx.spec.RequiredProduces(), nil, errors.MethodNotAllowed(r.Method, others)) 57 return 58 } 59 ctx.Respond(rw, r, ctx.spec.RequiredProduces(), nil, errors.NotFound("path %s was not found", r.URL.Path)) 60 }) 61 } 62 */ 63 package middleware 64