Пример #1
0
func NewHTTPHandler(r *Relay) http.Handler {
	m := httpx.NewRouter()

	m.Handle("POST", "/containers", &PostContainers{r})

	var h httpx.Handler

	// Handle errors
	errorHandler := func(err error, w http.ResponseWriter, r *http.Request) {
		Error(w, err, http.StatusInternalServerError)
	}

	h = middleware.HandleError(m, errorHandler)

	// Recover from panics.
	h = middleware.Recover(h, reporter.NewLogReporter())

	// Add a logger to the context.
	h = middleware.NewLogger(h, os.Stdout)

	// Add the request id to the context.
	h = middleware.ExtractRequestID(h)

	// Wrap the route in middleware to add a context.Context.
	return middleware.BackgroundContext(h)
}
Пример #2
0
// Common wraps the httpx.Handler with some common middleware.
func Common(h httpx.Handler, opts CommonOpts) http.Handler {
	l := &log{opts.Logger}

	// Recover from panics.
	h = middleware.Recover(h, opts.Reporter)

	// Add a logger to the context.
	h = middleware.LogTo(h, func(ctx context.Context, r *http.Request) logger.Logger {
		return l.New("request_id", httpx.RequestID(ctx))
	})

	// Wrap the route in middleware to add a context.Context.
	return middleware.BackgroundContext(h)
}