Example #1
0
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)
		})
	}
}
Example #2
0
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)
		})
	}
}
Example #3
0
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)
		})
	}
}
Example #4
0
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)
		})
	}
}
Example #5
0
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)
		})
	}
}
Example #6
0
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)
		})
	}
}