| 1 |
package http |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/http" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/coreos/discovery.etcd.io/handlers" |
| 9 |
|
| 10 |
gorillaHandlers "github.com/gorilla/handlers" |
| 11 |
"github.com/gorilla/mux" |
| 12 |
"github.com/prometheus/client_golang/prometheus" |
| 13 |
) |
| 14 |
|
| 15 |
func Setup(ctx context.Context, etcdHost, discHost string) { |
| 16 |
handler := RegisterHandlers(ctx, etcdHost, discHost) |
| 17 |
logH := gorillaHandlers.LoggingHandler(os.Stdout, handler) |
| 18 |
|
| 19 |
http.Handle("/", logH) |
| 20 |
http.Handle("/metrics", prometheus.Handler()) |
| 21 |
} |
| 22 |
|
| 23 |
func RegisterHandlers(ctx context.Context, etcdHost, discHost string) http.Handler { |
| 24 |
st := handlers.Setup(etcdHost, discHost) |
| 25 |
r := mux.NewRouter() |
| 26 |
|
| 27 |
r.HandleFunc("/", handlers.HomeHandler) |
| 28 |
r.Handle("/new", &handlers.ContextAdapter{ |
| 29 |
Ctx: ctx, |
| 30 |
Handler: handlers.With(handlers.ContextHandlerFunc(handlers.NewTokenHandler), st), |
| 31 |
}) |
| 32 |
r.Handle("/health", &handlers.ContextAdapter{ |
| 33 |
Ctx: ctx, |
| 34 |
Handler: handlers.With(handlers.ContextHandlerFunc(handlers.HealthHandler), st), |
| 35 |
}) |
| 36 |
r.HandleFunc("/robots.txt", handlers.RobotsHandler) |
| 37 |
|
| 38 |
|
| 39 |
r.Handle("/{token:[a-f0-9]{32}}", &handlers.ContextAdapter{ |
| 40 |
Ctx: ctx, |
| 41 |
Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st), |
| 42 |
}).Methods("GET", "PUT") |
| 43 |
r.Handle("/{token:[a-f0-9]{32}}/", &handlers.ContextAdapter{ |
| 44 |
Ctx: ctx, |
| 45 |
Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st), |
| 46 |
}).Methods("GET", "PUT") |
| 47 |
r.Handle("/{token:[a-f0-9]{32}}/{machine}", &handlers.ContextAdapter{ |
| 48 |
Ctx: ctx, |
| 49 |
Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st), |
| 50 |
}).Methods("GET", "PUT", "DELETE") |
| 51 |
r.Handle("/{token:[a-f0-9]{32}}/_config/size", &handlers.ContextAdapter{ |
| 52 |
Ctx: ctx, |
| 53 |
Handler: handlers.With(handlers.ContextHandlerFunc(handlers.TokenHandler), st), |
| 54 |
}).Methods("GET") |
| 55 |
|
| 56 |
return r |
| 57 |
} |