Back to all reviewers

Use context for configuration

gofiber/fiber
Based on 4 comments
Markdown

When accessing application state or configuration within request handlers, always use the context method `c.App().State()` instead of direct app instance references. This pattern ensures proper context isolation, supports dependency injection, and works correctly in multi-file applications where the app instance may not be directly accessible.

Configurations Markdown

Reviewer Prompt

When accessing application state or configuration within request handlers, always use the context method c.App().State() instead of direct app instance references. This pattern ensures proper context isolation, supports dependency injection, and works correctly in multi-file applications where the app instance may not be directly accessible.

Direct app references create tight coupling and break when handlers are moved to separate files or packages. The context-aware approach maintains loose coupling and follows proper architectural patterns.

Example:

// ❌ Avoid: Direct app reference
app.Get("/config", func(c fiber.Ctx) error {
    config := map[string]any{
        "apiUrl": fiber.GetStateWithDefault(app.State(), "apiUrl", ""),
        "debug":  fiber.GetStateWithDefault(app.State(), "debug", false),
    }
    return c.JSON(config)
})

// ✅ Prefer: Context-aware access
app.Get("/config", func(c fiber.Ctx) error {
    config := map[string]any{
        "apiUrl": fiber.GetStateWithDefault(c.App().State(), "apiUrl", ""),
        "debug":  fiber.GetStateWithDefault(c.App().State(), "debug", false),
    }
    return c.JSON(config)
})

This pattern is especially important when handlers are defined in separate packages or when using dependency injection, as it ensures configuration access works regardless of the application’s structure.

4
Comments Analyzed
Markdown
Primary Language
Configurations
Category

Source Discussions