1// Copyright 2015 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package route
15
16import (
17	"context"
18	"net/http"
19
20	"github.com/julienschmidt/httprouter"
21)
22
23type param string
24
25// Param returns param p for the context, or the empty string when
26// param does not exist in context.
27func Param(ctx context.Context, p string) string {
28	if v := ctx.Value(param(p)); v != nil {
29		return v.(string)
30	}
31	return ""
32}
33
34// WithParam returns a new context with param p set to v.
35func WithParam(ctx context.Context, p, v string) context.Context {
36	return context.WithValue(ctx, param(p), v)
37}
38
39// Router wraps httprouter.Router and adds support for prefixed sub-routers,
40// per-request context injections and instrumentation.
41type Router struct {
42	rtr    *httprouter.Router
43	prefix string
44	instrh func(handlerName string, handler http.HandlerFunc) http.HandlerFunc
45}
46
47// New returns a new Router.
48func New() *Router {
49	return &Router{
50		rtr: httprouter.New(),
51	}
52}
53
54// WithInstrumentation returns a router with instrumentation support.
55func (r *Router) WithInstrumentation(instrh func(handlerName string, handler http.HandlerFunc) http.HandlerFunc) *Router {
56	return &Router{rtr: r.rtr, prefix: r.prefix, instrh: instrh}
57}
58
59// WithPrefix returns a router that prefixes all registered routes with prefix.
60func (r *Router) WithPrefix(prefix string) *Router {
61	return &Router{rtr: r.rtr, prefix: r.prefix + prefix, instrh: r.instrh}
62}
63
64// handle turns a HandlerFunc into an httprouter.Handle.
65func (r *Router) handle(handlerName string, h http.HandlerFunc) httprouter.Handle {
66	if r.instrh != nil {
67		// This needs to be outside the closure to avoid data race when reading and writing to 'h'.
68		h = r.instrh(handlerName, h)
69	}
70	return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
71		ctx, cancel := context.WithCancel(req.Context())
72		defer cancel()
73
74		for _, p := range params {
75			ctx = context.WithValue(ctx, param(p.Key), p.Value)
76		}
77		h(w, req.WithContext(ctx))
78	}
79}
80
81// Get registers a new GET route.
82func (r *Router) Get(path string, h http.HandlerFunc) {
83	r.rtr.GET(r.prefix+path, r.handle(path, h))
84}
85
86// Options registers a new OPTIONS route.
87func (r *Router) Options(path string, h http.HandlerFunc) {
88	r.rtr.OPTIONS(r.prefix+path, r.handle(path, h))
89}
90
91// Del registers a new DELETE route.
92func (r *Router) Del(path string, h http.HandlerFunc) {
93	r.rtr.DELETE(r.prefix+path, r.handle(path, h))
94}
95
96// Put registers a new PUT route.
97func (r *Router) Put(path string, h http.HandlerFunc) {
98	r.rtr.PUT(r.prefix+path, r.handle(path, h))
99}
100
101// Post registers a new POST route.
102func (r *Router) Post(path string, h http.HandlerFunc) {
103	r.rtr.POST(r.prefix+path, r.handle(path, h))
104}
105
106// Redirect takes an absolute path and sends an internal HTTP redirect for it,
107// prefixed by the router's path prefix. Note that this method does not include
108// functionality for handling relative paths or full URL redirects.
109func (r *Router) Redirect(w http.ResponseWriter, req *http.Request, path string, code int) {
110	http.Redirect(w, req, r.prefix+path, code)
111}
112
113// ServeHTTP implements http.Handler.
114func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
115	r.rtr.ServeHTTP(w, req)
116}
117
118// FileServe returns a new http.HandlerFunc that serves files from dir.
119// Using routes must provide the *filepath parameter.
120func FileServe(dir string) http.HandlerFunc {
121	fs := http.FileServer(http.Dir(dir))
122
123	return func(w http.ResponseWriter, r *http.Request) {
124		r.URL.Path = Param(r.Context(), "filepath")
125		fs.ServeHTTP(w, r)
126	}
127}
128