podman
62 строки · 1.9 Кб
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/*Package middleware provides the library with helper functions for serving swagger APIs.
16
17Pseudo middleware handler
18
19import (
20"net/http"
21
22"github.com/go-openapi/errors"
23)
24
25func newCompleteMiddleware(ctx *Context) http.Handler {
26return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
27// use context to lookup routes
28if matched, ok := ctx.RouteInfo(r); ok {
29
30if matched.NeedsAuth() {
31if _, err := ctx.Authorize(r, matched); err != nil {
32ctx.Respond(rw, r, matched.Produces, matched, err)
33return
34}
35}
36
37bound, validation := ctx.BindAndValidate(r, matched)
38if validation != nil {
39ctx.Respond(rw, r, matched.Produces, matched, validation)
40return
41}
42
43result, err := matched.Handler.Handle(bound)
44if err != nil {
45ctx.Respond(rw, r, matched.Produces, matched, err)
46return
47}
48
49ctx.Respond(rw, r, matched.Produces, matched, result)
50return
51}
52
53// Not found, check if it exists in the other methods first
54if others := ctx.AllowedMethods(r); len(others) > 0 {
55ctx.Respond(rw, r, ctx.spec.RequiredProduces(), nil, errors.MethodNotAllowed(r.Method, others))
56return
57}
58ctx.Respond(rw, r, ctx.spec.RequiredProduces(), nil, errors.NotFound("path %s was not found", r.URL.Path))
59})
60}
61*/
62package middleware
63