github.com/arthur-befumo/witchcraft-go-server@v1.12.0/wrouter/wgorillamux/routerimpl.go (about)

     1  // Copyright (c) 2018 Palantir Technologies. All rights reserved.
     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 wgorillamux
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"strings"
    21  
    22  	"github.com/gorilla/mux"
    23  	"github.com/palantir/witchcraft-go-server/wrouter"
    24  )
    25  
    26  // New returns a wrouter.RouterImpl backed by a new mux.Router configured using the provided parameters.
    27  func New(params ...Param) wrouter.RouterImpl {
    28  	r := mux.NewRouter()
    29  	for _, p := range params {
    30  		p.apply(r)
    31  	}
    32  	return (*router)(r)
    33  }
    34  
    35  type Param interface {
    36  	apply(*mux.Router)
    37  }
    38  
    39  type paramFunc func(*mux.Router)
    40  
    41  func (f paramFunc) apply(r *mux.Router) {
    42  	f(r)
    43  }
    44  
    45  func NotFoundHandler(h http.Handler) Param {
    46  	return paramFunc(func(r *mux.Router) {
    47  		r.NotFoundHandler = h
    48  	})
    49  }
    50  
    51  func StrictSlash(value bool) Param {
    52  	return paramFunc(func(r *mux.Router) {
    53  		r.StrictSlash(value)
    54  	})
    55  }
    56  
    57  func SkipClean(value bool) Param {
    58  	return paramFunc(func(r *mux.Router) {
    59  		r.SkipClean(value)
    60  	})
    61  }
    62  
    63  func UseEncodedPath() Param {
    64  	return paramFunc(func(r *mux.Router) {
    65  		r.UseEncodedPath()
    66  	})
    67  }
    68  
    69  type router mux.Router
    70  
    71  func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    72  	(*mux.Router)(r).ServeHTTP(w, req)
    73  }
    74  
    75  func (r *router) Register(method string, pathSegments []wrouter.PathSegment, handler http.Handler) {
    76  	(*mux.Router)(r).Path(r.convertPathParams(pathSegments)).Methods(method).Handler(handler)
    77  }
    78  
    79  func (r *router) PathParams(req *http.Request, pathVarNames []string) map[string]string {
    80  	vars := mux.Vars(req)
    81  	if len(vars) == 0 {
    82  		return nil
    83  	}
    84  	params := make(map[string]string)
    85  	for _, currVarName := range pathVarNames {
    86  		params[currVarName] = vars[currVarName]
    87  	}
    88  	return params
    89  }
    90  
    91  func (r *router) convertPathParams(path []wrouter.PathSegment) string {
    92  	pathParts := make([]string, len(path))
    93  	for i, segment := range path {
    94  		switch segment.Type {
    95  		case wrouter.TrailingPathParamSegment:
    96  			pathParts[i] = fmt.Sprintf("{%s:.+}", segment.Value)
    97  		case wrouter.PathParamSegment:
    98  			pathParts[i] = fmt.Sprintf("{%s}", segment.Value)
    99  		default:
   100  			pathParts[i] = segment.Value
   101  		}
   102  	}
   103  	return "/" + strings.Join(pathParts, "/")
   104  }