func NewRequestMiddleare() synapse.Constructor { return func(next synapse.Handler) synapse.Handler { return synapse.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) { next.ServeHTTPC(c, &statusResponseWriter{w, http.StatusOK}, r) }) } }
func newReport() synapse.Constructor { return func(next synapse.Handler) synapse.Handler { return synapse.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) { c = context.WithValue(c, "report", map[string]interface{}{}) next.ServeHTTPC(c, w, r) }) } }
func AuthMiddleware() synapse.Constructor { return func(next synapse.Handler) synapse.Handler { return synapse.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) { // Some auth logic here c = context.WithValue(c, CtxRequestUser, "John Doe") next.ServeHTTPC(c, w, r) }) } }
func TracingMiddleware(getTraceID NewTraceID) synapse.Constructor { return func(next synapse.Handler) synapse.Handler { return synapse.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) { traceID := getTraceID() w.Header().Set(HeaderTraceID, fmt.Sprintf("%d", traceID)) c = context.WithValue(c, CtxRequestTraceID, traceID) next.ServeHTTPC(c, w, r) }) } }
func defaultReport() synapse.Constructor { return func(next synapse.Handler) synapse.Handler { return synapse.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) { report := c.Value("report").(map[string]interface{}) report["name"] = "Special Analysis" report["date"] = time.Now() report["author"] = c.Value(CtxRequestUser).(string) c = context.WithValue(c, "report", report) next.ServeHTTPC(c, w, r) }) } }
func LoggingMiddleware(logs io.Writer) synapse.Constructor { return func(next synapse.Handler) synapse.Handler { return synapse.HandlerFunc(func(c context.Context, w http.ResponseWriter, r *http.Request) { logger := log.New(logs, "", log.LstdFlags) defer func(begin time.Time) { cw := w.(*statusResponseWriter) traceID := c.Value(CtxRequestTraceID) logger.Printf("method=%s route=%s trace_id=%d status=%d took=%s\n", r.Method, r.RequestURI, traceID, cw.Status, time.Now().Sub(begin)) }(time.Now()) next.ServeHTTPC(c, w, r) }) } }