Fiber rest api

From makifbaysal/tasktrooper · View interactive version

Fiber rest api is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when adding or changing a Go HTTP endpoint — Fiber v2 or v3 (detect which), thin handlers, central error mapping, boundary validation; the same rules apply to net/http, chi, echo or gin

How to use this skill

Skill file

Go HTTP Endpoint Patterns

Overview

A handler is a translator: HTTP in → service call → HTTP out. Business logic never lives there. The recurring defects are fat handlers, per-handler error mapping that drifts, endpoints that quietly drop the auth middleware their neighbors have, and copying a value Fiber is about to reuse.

Core principle: handlers parse, validate, delegate, and map. Nothing else.

Detect the repo's router and framework version first

grep -E 'gofiber/fiber/v[23]|go-chi/chi|labstack/echo|gin-gonic/gin' go.mod

Never change the router or the Fiber major version in a feature task; a repo on Fiber v2 stays v2 (security-fix branch only). A brand-new repo with no router yet gets Fiber v3 (needs Go ≥1.25) unless the task says otherwise.

Fiber v2 → v3

| | v2 | v3 |

|---|---|---|

| Handler signature | func(c *fiber.Ctx) error | func(c fiber.Ctx) error |

| Body | c.BodyParser(&req) | c.Bind().Body(&req) |

| Query / params | c.QueryParser / c.ParamsParser | c.Bind().Query / c.Bind().URI |

| Struct tag for path params | params: | uri: |

| Typed query helper | c.QueryInt("limit", 20) | fiber.Query[int](c, "limit", 20) |

| Go-standard request context | c.UserContext() | c.Context() |

| Raw fasthttp context | c.Context() (returns *fasthttp.RequestCtx) | c.RequestCtx() |

| Request id from context | c.Locals("requestid") | requestid.FromContext(c) |

| Static files / sub-app mount | app.Static(...) / app.Mount(...) | static.New(...) / app.Use(...) |

fiber migrate --to v3 exists for a dedicated upgrade task; never fold a framework-major bump into a feature task.

The v2 bug this corrects: on Fiber v2, c.Context() returns the pooled *fasthttp.RequestCtx, not the request-scoped context.Context — passing it where a context.Context is expected either fails to compile or silently drops whatever middleware put into the real context (auth claims, request id, trace span). Use c.UserContext() on v2.

Rules

net/http (Go ≥1.22, when the repo has no router)

mux.HandleFunc("POST /tasks/{id}/children", h.CreateChild)
id := r.PathValue("id")
r.Body = http.MaxBytesReader(w, r.Body, maxBody)

Worked Example

func (h *TaskHandler) Create(c fiber.Ctx) error {
    var req createTaskRequest
    if err := c.Bind().Body(&req); err != nil {
        return fiber.NewError(fiber.StatusBadRequest, "invalid body")
    }
    if err := req.validate(); err != nil {
        return fiber.NewError(fiber.StatusBadRequest, err.Error())
    }
    task, err := h.svc.Create(c.Context(), req.Title)
    if err != nil {
        return err
    }
    return c.Status(fiber.StatusCreated).JSON(taskResponseFrom(task))
}

The title-length rule lives in the service/domain, not the handler. The central error middleware maps domain.ErrInvalidTitle → 400, domain.ErrConflict → 409 — every handler benefits.

Handler test in-process (no network)

req := httptest.NewRequest(http.MethodPost, "/tasks", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)       // v2: app.Test(req, -1)
// v3:  app.Test(req, fiber.TestConfig{Timeout: 0})

Assert status code and decoded body — this is still a unit test; it does not replace running the real service once (backend-runtime-check).

Common Mistakes

Red Flags

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.