File: http.go

package info (click to toggle)
golang-github-coreos-discovery-etcd-io 2.0.0%2Bgit2019.04.19.git.78fb45d3c9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 23,460 kB
  • sloc: asm: 452; sh: 94; ansic: 24; makefile: 16
file content (57 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package http

import (
	"context"
	"net/http"
	"os"

	"github.com/coreos/discovery.etcd.io/handlers"

	gorillaHandlers "github.com/gorilla/handlers"
	"github.com/gorilla/mux"
	"github.com/prometheus/client_golang/prometheus"
)

func Setup(ctx context.Context, etcdHost, discHost string) {
	handler := RegisterHandlers(ctx, etcdHost, discHost)
	logH := gorillaHandlers.LoggingHandler(os.Stdout, handler)

	http.Handle("/", logH)
	http.Handle("/metrics", prometheus.Handler())
}

func RegisterHandlers(ctx context.Context, etcdHost, discHost string) http.Handler {
	st := handlers.Setup(etcdHost, discHost)
	r := mux.NewRouter()

	r.HandleFunc("/", handlers.HomeHandler)
	r.Handle("/new", &handlers.ContextAdapter{
		Ctx:     ctx,
		Handler: handlers.With(handlers.ContextHandlerFunc(handlers.NewTokenHandler), st),
	})
	r.Handle("/health", &handlers.ContextAdapter{
		Ctx:     ctx,
		Handler: handlers.With(handlers.ContextHandlerFunc(handlers.HealthHandler), st),
	})
	r.HandleFunc("/robots.txt", handlers.RobotsHandler)

	// Only allow exact tokens with GETs and PUTs
	r.Handle("/{token:[a-f0-9]{32}}", &handlers.ContextAdapter{
		Ctx:     ctx,
		Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st),
	}).Methods("GET", "PUT")
	r.Handle("/{token:[a-f0-9]{32}}/", &handlers.ContextAdapter{
		Ctx:     ctx,
		Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st),
	}).Methods("GET", "PUT")
	r.Handle("/{token:[a-f0-9]{32}}/{machine}", &handlers.ContextAdapter{
		Ctx:     ctx,
		Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st),
	}).Methods("GET", "PUT", "DELETE")
	r.Handle("/{token:[a-f0-9]{32}}/_config/size", &handlers.ContextAdapter{
		Ctx:     ctx,
		Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st),
	}).Methods("GET")

	return r
}