Home / Function/ handlerFunc() — fiber Function Reference

handlerFunc() — fiber Function Reference

Architecture documentation for the handlerFunc() function in adaptor.go from the fiber codebase.

Function go FiberCore Adapters calls 1 called by 2

Entity Profile

Dependency Diagram

graph TD
  40aa2f1e_c86b_c607_5f05_d8382ca4e8ce["handlerFunc()"]
  69780622_2588_e0fa_ab5a_13dcfd3559e7["adaptor.go"]
  40aa2f1e_c86b_c607_5f05_d8382ca4e8ce -->|defined in| 69780622_2588_e0fa_ab5a_13dcfd3559e7
  8a3a0e5b_34f3_b904_67a3_684b57950412["FiberHandlerFunc()"]
  8a3a0e5b_34f3_b904_67a3_684b57950412 -->|calls| 40aa2f1e_c86b_c607_5f05_d8382ca4e8ce
  b7839fd5_d98a_2a41_e9ef_b08bdec7b1b1["FiberApp()"]
  b7839fd5_d98a_2a41_e9ef_b08bdec7b1b1 -->|calls| 40aa2f1e_c86b_c607_5f05_d8382ca4e8ce
  86d4a824_2a2e_9467_1106_5313691a9e3c["resolveRemoteAddr()"]
  40aa2f1e_c86b_c607_5f05_d8382ca4e8ce -->|calls| 86d4a824_2a2e_9467_1106_5313691a9e3c
  style 40aa2f1e_c86b_c607_5f05_d8382ca4e8ce fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/adaptor/adaptor.go lines 234–330

func handlerFunc(app *fiber.App, h ...fiber.Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		req := fasthttp.AcquireRequest()
		defer fasthttp.ReleaseRequest(req)

		// Convert net/http -> fasthttp request with size limit
		maxBodySize := int64(app.Config().BodyLimit)
		if r.Body != nil {
			if r.ContentLength > maxBodySize {
				http.Error(w, utils.StatusMessage(fiber.StatusRequestEntityTooLarge), fiber.StatusRequestEntityTooLarge)
				return
			}
			limitedReader := io.LimitReader(r.Body, maxBodySize)
			n, err := io.Copy(req.BodyWriter(), limitedReader)
			req.Header.SetContentLength(int(n))

			if err != nil {
				http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
				return
			}
		}
		req.Header.SetMethod(r.Method)
		req.SetRequestURI(r.RequestURI)
		req.SetHost(r.Host)
		req.Header.SetHost(r.Host)

		for key, val := range r.Header {
			for _, v := range val {
				req.Header.Set(key, v)
			}
		}

		remoteAddr, err := resolveRemoteAddr(r.RemoteAddr, r.Context().Value(http.LocalAddrContextKey))
		if err != nil {
			remoteAddr = nil // Fallback to nil
		}

		// New fasthttp Ctx from pool
		fctx := ctxPool.Get().(*fasthttp.RequestCtx) //nolint:forcetypeassert,errcheck // not needed
		fctx.Response.Reset()
		fctx.Request.Reset()
		defer ctxPool.Put(fctx)
		fctx.Init(req, remoteAddr, &disableLogger{})

		if len(h) > 0 {
			// New fiber Ctx
			ctx := app.AcquireCtx(fctx)
			defer app.ReleaseCtx(ctx)

			// Execute fiber Ctx
			err := h[0](ctx)
			if err != nil {
				_ = app.Config().ErrorHandler(ctx, err) //nolint:errcheck // not needed
			}
		} else {
			// Execute fasthttp Ctx though app.Handler
			app.Handler()(fctx)
		}

		// Convert fasthttp Ctx -> net/http
		for k, v := range fctx.Response.Header.All() {
			w.Header().Add(string(k), string(v))
		}
		w.WriteHeader(fctx.Response.StatusCode())

		// Check if streaming is not possible or unnecessary.
		bodyStream := fctx.Response.BodyStream()
		flusher, ok := w.(http.Flusher)
		if !ok || bodyStream == nil {
			_, _ = w.Write(fctx.Response.Body()) //nolint:errcheck // not needed
			return
		}

		// Stream fctx.Response.BodyStream() -> w
		// in chunks.
		bufPtr, ok := bufferPool.Get().(*[bufferSize]byte)
		if !ok {
			panic(fmt.Errorf("failed to type-assert to *[%d]byte", bufferSize))
		}
		defer bufferPool.Put(bufPtr)

Domain

Subdomains

Frequently Asked Questions

What does handlerFunc() do?
handlerFunc() is a function in the fiber codebase, defined in middleware/adaptor/adaptor.go.
Where is handlerFunc() defined?
handlerFunc() is defined in middleware/adaptor/adaptor.go at line 234.
What does handlerFunc() call?
handlerFunc() calls 1 function(s): resolveRemoteAddr.
What calls handlerFunc()?
handlerFunc() is called by 2 function(s): FiberApp, FiberHandlerFunc.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free