Esempio n. 1
0
// NewHandler returns a new instance of handler with routes.
func NewHandler(
	requireAuthentication,
	loggingEnabled,
	writeTrace,
	allowGzip bool,
	statMap *expvar.Map,
	l *log.Logger,
	li logging.Interface,
	sharedSecret string,
) *Handler {
	h := &Handler{
		methodMux:             make(map[string]*ServeMux),
		requireAuthentication: requireAuthentication,
		sharedSecret:          sharedSecret,
		allowGzip:             allowGzip,
		logger:                l,
		writeTrace:            writeTrace,
		clfLogger:             li.NewRawLogger("[httpd] ", 0),
		loggingEnabled:        loggingEnabled,
		statMap:               statMap,
	}

	allowedMethods := []string{
		"GET",
		"POST",
		"PATCH",
		"DELETE",
		"HEAD",
		"OPTIONS",
	}

	for _, method := range allowedMethods {
		h.methodMux[method] = NewServeMux()
		route := Route{
			// Catch all 404
			Name:        "404",
			Method:      method,
			Pattern:     "/",
			HandlerFunc: h.serve404,
		}
		h.addRawRoute(route)
	}

	h.addRawRoutes([]Route{
		{
			// Ping
			Name:        "ping",
			Method:      "GET",
			Pattern:     BasePath + "/ping",
			HandlerFunc: h.servePing,
		},
		{
			// Ping
			Name:        "ping-head",
			Method:      "HEAD",
			Pattern:     BasePath + "/ping",
			HandlerFunc: h.servePing,
		},
		{
			// Data-ingest route.
			Name:        "write",
			Method:      "POST",
			Pattern:     BasePath + "/write",
			HandlerFunc: h.serveWrite,
		},
		{
			// Satisfy CORS checks.
			Name:        "write",
			Method:      "OPTIONS",
			Pattern:     BasePath + "/write",
			HandlerFunc: ServeOptions,
		},
		{
			// Data-ingest route for /write endpoint without base path
			Name:        "write-raw",
			Method:      "POST",
			Pattern:     "/write",
			HandlerFunc: h.serveWrite,
		},
		{
			// Satisfy CORS checks.
			Name:        "write-raw",
			Method:      "OPTIONS",
			Pattern:     "/write",
			HandlerFunc: ServeOptions,
		},
		{
			// Display current API routes
			Name:        "routes",
			Method:      "GET",
			Pattern:     BasePath + "/:routes",
			HandlerFunc: h.serveRoutes,
		},
		{
			// Change current log level
			Name:        "log-level",
			Method:      "POST",
			Pattern:     BasePath + "/loglevel",
			HandlerFunc: h.serveLogLevel,
		},
		{
			Name:        "pprof",
			Method:      "GET",
			Pattern:     BasePath + "/debug/pprof/",
			HandlerFunc: pprof.Index,
			noJSON:      true,
		},
		{
			Name:        "pprof/cmdline",
			Method:      "GET",
			Pattern:     BasePath + "/debug/pprof/cmdline",
			HandlerFunc: pprof.Cmdline,
			noJSON:      true,
		},
		{
			Name:        "pprof/profile",
			Method:      "GET",
			Pattern:     BasePath + "/debug/pprof/profile",
			HandlerFunc: pprof.Profile,
			noJSON:      true,
		},
		{
			Name:        "pprof/symbol",
			Method:      "GET",
			Pattern:     BasePath + "/debug/pprof/symbol",
			HandlerFunc: pprof.Symbol,
			noJSON:      true,
		},
		{
			Name:        "pprof/trace",
			Method:      "GET",
			Pattern:     BasePath + "/debug/pprof/trace",
			HandlerFunc: pprof.Trace,
			noJSON:      true,
		},
		{
			Name:        "debug/vars",
			Method:      "GET",
			Pattern:     BasePath + "/debug/vars",
			HandlerFunc: serveExpvar,
		},
	})

	return h
}