Exemple #1
0
func (e Endpoint) handlerFunc(globalBeforeWM, globalAfterWM DriveMiddleware) func(w http.ResponseWriter, r *http.Request, params map[string]string) {
	action := WrapForAction(e.Action)

	var localWM DriveMiddleware
	var afterWM DriveMiddleware

	if e.LocalMW != nil {
		localWM = WrapForMW(e.LocalMW)
	}

	if e.AfterWM != nil {
		afterWM = WrapForMW(e.AfterWM)
	}

	return func(w http.ResponseWriter, r *http.Request, params map[string]string) {
		ctx := context.New()
		rw := &Request{
			Params: Param(params),
			Res:    NewResponseWriter(w),
			Req:    r,
		}

		// Run the global middleware first and recieve its returned values.
		if globalBeforeWM != nil {
			_, err := globalBeforeWM(ctx, rw)
			if err != nil && !rw.Res.DataWritten() {
				RenderResponseError(err, rw)
				return
			}
		}

		// Run local middleware second and receive its return values.
		if localWM != nil {
			_, err := localWM(ctx, rw)
			if err != nil && !rw.Res.DataWritten() {
				RenderResponseError(err, rw)
				return
			}
		}

		if err := action(ctx, rw); err != nil && !rw.Res.DataWritten() {
			RenderResponseError(err, rw)
			return
		}

		if afterWM != nil {
			_, err := afterWM(ctx, rw)
			if err != nil && !rw.Res.DataWritten() {
				RenderResponseError(err, rw)
				return
			}
		}

		if globalAfterWM != nil {
			_, err := globalAfterWM(ctx, rw)
			if err != nil && !rw.Res.DataWritten() {
				RenderResponseError(err, rw)
				return
			}
		}

	}
}
Exemple #2
0
// WrapFractalHandler returns a new http.HandlerFunc for recieving http request.
func WrapFractalHandler(handler fractals.Handler) func(http.ResponseWriter, *http.Request, map[string]string) {
	return WrapFractalHandlerWith(context.New(), handler)
}