github.com/palantir/witchcraft-go-server/v2@v2.76.0/wrouter/route_params.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 wrouter
    16  
    17  import (
    18  	"github.com/palantir/pkg/metrics"
    19  )
    20  
    21  type routeParamBuilder struct {
    22  	middleware       []RouteHandlerMiddleware
    23  	paramPerms       RouteParamPerms
    24  	metricTags       metrics.Tags
    25  	disableTelemetry bool
    26  }
    27  
    28  func (b *routeParamBuilder) toRequestParamPerms() RouteParamPerms {
    29  	if b.paramPerms != nil {
    30  		return b.paramPerms
    31  	}
    32  	return &requestParamPermsImpl{}
    33  }
    34  
    35  func (b *routeParamBuilder) toMetricTags() metrics.Tags {
    36  	var tags metrics.Tags
    37  	if b.metricTags != nil {
    38  		tags = append(tags, b.metricTags...)
    39  	}
    40  	return tags
    41  }
    42  
    43  type RouteParam interface {
    44  	apply(*routeParamBuilder) error
    45  }
    46  
    47  type routeParamFunc func(*routeParamBuilder) error
    48  
    49  func (f routeParamFunc) apply(b *routeParamBuilder) error {
    50  	return f(b)
    51  }
    52  
    53  func RouteParamPermsParam(perms RouteParamPerms) RouteParam {
    54  	return routeParamFunc(func(b *routeParamBuilder) error {
    55  		var pathParamPerms []ParamPerms
    56  		var queryParamPerms []ParamPerms
    57  		var headerParamPerms []ParamPerms
    58  		if b.paramPerms != nil {
    59  			pathParamPerms = append(pathParamPerms, b.paramPerms.PathParamPerms())
    60  			queryParamPerms = append(queryParamPerms, b.paramPerms.QueryParamPerms())
    61  			headerParamPerms = append(headerParamPerms, b.paramPerms.HeaderParamPerms())
    62  		}
    63  
    64  		pathParamPerms = append(pathParamPerms, perms.PathParamPerms())
    65  		queryParamPerms = append(queryParamPerms, perms.QueryParamPerms())
    66  		headerParamPerms = append(headerParamPerms, perms.HeaderParamPerms())
    67  
    68  		b.paramPerms = &requestParamPermsImpl{
    69  			pathParamPerms:   NewCombinedParamPerms(pathParamPerms...),
    70  			queryParamPerms:  NewCombinedParamPerms(queryParamPerms...),
    71  			headerParamPerms: NewCombinedParamPerms(headerParamPerms...),
    72  		}
    73  		return nil
    74  	})
    75  }
    76  
    77  func SafePathParams(safeParams ...string) RouteParam {
    78  	return RouteParamPermsParam(&requestParamPermsImpl{
    79  		pathParamPerms: newSafeParamPerms(safeParams...),
    80  	})
    81  }
    82  
    83  func ForbiddenPathParams(forbiddenParams ...string) RouteParam {
    84  	return RouteParamPermsParam(&requestParamPermsImpl{
    85  		pathParamPerms: newForbiddenParamPerms(forbiddenParams...),
    86  	})
    87  }
    88  
    89  func SafeQueryParams(safeParams ...string) RouteParam {
    90  	return RouteParamPermsParam(&requestParamPermsImpl{
    91  		queryParamPerms: newSafeParamPerms(safeParams...),
    92  	})
    93  }
    94  
    95  func ForbiddenQueryParams(forbiddenParams ...string) RouteParam {
    96  	return RouteParamPermsParam(&requestParamPermsImpl{
    97  		queryParamPerms: newForbiddenParamPerms(forbiddenParams...),
    98  	})
    99  }
   100  
   101  func SafeHeaderParams(safeParams ...string) RouteParam {
   102  	return RouteParamPermsParam(&requestParamPermsImpl{
   103  		headerParamPerms: newSafeParamPerms(safeParams...),
   104  	})
   105  }
   106  
   107  func ForbiddenHeaderParams(forbiddenParams ...string) RouteParam {
   108  	return RouteParamPermsParam(&requestParamPermsImpl{
   109  		headerParamPerms: newForbiddenParamPerms(forbiddenParams...),
   110  	})
   111  }
   112  
   113  func MetricTags(tags metrics.Tags) RouteParam {
   114  	return routeParamFunc(func(b *routeParamBuilder) error {
   115  		b.metricTags = tags
   116  		return nil
   117  	})
   118  }
   119  
   120  func DisableTelemetry() RouteParam {
   121  	return routeParamFunc(func(b *routeParamBuilder) error {
   122  		b.disableTelemetry = true
   123  		return nil
   124  	})
   125  }
   126  
   127  // RouteMiddleware configures the provided middleware to run on requests matching this specific route.
   128  func RouteMiddleware(middleware RouteHandlerMiddleware) RouteParam {
   129  	return routeParamFunc(func(b *routeParamBuilder) error {
   130  		if middleware != nil {
   131  			b.middleware = append(b.middleware, middleware)
   132  		}
   133  		return nil
   134  	})
   135  }