...

Source file src/github.com/go-openapi/runtime/middleware/spec.go

Documentation: github.com/go-openapi/runtime/middleware

     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
    16  
    17  import (
    18  	"net/http"
    19  	"path"
    20  )
    21  
    22  const (
    23  	contentTypeHeader = "Content-Type"
    24  	applicationJSON   = "application/json"
    25  )
    26  
    27  // SpecOption can be applied to the Spec serving middleware
    28  type SpecOption func(*specOptions)
    29  
    30  var defaultSpecOptions = specOptions{
    31  	Path:     "",
    32  	Document: "swagger.json",
    33  }
    34  
    35  type specOptions struct {
    36  	Path     string
    37  	Document string
    38  }
    39  
    40  func specOptionsWithDefaults(opts []SpecOption) specOptions {
    41  	o := defaultSpecOptions
    42  	for _, apply := range opts {
    43  		apply(&o)
    44  	}
    45  
    46  	return o
    47  }
    48  
    49  // Spec creates a middleware to serve a swagger spec as a JSON document.
    50  //
    51  // This allows for altering the spec before starting the http listener.
    52  //
    53  // The basePath argument indicates the path of the spec document (defaults to "/").
    54  // Additional SpecOption can be used to change the name of the document (defaults to "swagger.json").
    55  func Spec(basePath string, b []byte, next http.Handler, opts ...SpecOption) http.Handler {
    56  	if basePath == "" {
    57  		basePath = "/"
    58  	}
    59  	o := specOptionsWithDefaults(opts)
    60  	pth := path.Join(basePath, o.Path, o.Document)
    61  
    62  	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    63  		if path.Clean(r.URL.Path) == pth {
    64  			rw.Header().Set(contentTypeHeader, applicationJSON)
    65  			rw.WriteHeader(http.StatusOK)
    66  			_, _ = rw.Write(b)
    67  
    68  			return
    69  		}
    70  
    71  		if next != nil {
    72  			next.ServeHTTP(rw, r)
    73  
    74  			return
    75  		}
    76  
    77  		rw.Header().Set(contentTypeHeader, applicationJSON)
    78  		rw.WriteHeader(http.StatusNotFound)
    79  	})
    80  }
    81  
    82  // WithSpecPath sets the path to be joined to the base path of the Spec middleware.
    83  //
    84  // This is empty by default.
    85  func WithSpecPath(pth string) SpecOption {
    86  	return func(o *specOptions) {
    87  		o.Path = pth
    88  	}
    89  }
    90  
    91  // WithSpecDocument sets the name of the JSON document served as a spec.
    92  //
    93  // By default, this is "swagger.json"
    94  func WithSpecDocument(doc string) SpecOption {
    95  	return func(o *specOptions) {
    96  		if doc == "" {
    97  			return
    98  		}
    99  
   100  		o.Document = doc
   101  	}
   102  }
   103  

View as plain text