enforcer

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2020 License: MIT Imports: 2 Imported by: 0

README

Fiber Route Enforcer

godoc tests sec go-report license

Fiber has a curious behaviour of of allowing non-matched routes to return 200 if they happen to "match" a middleware. This can happen quite often if you use any of the top level middlewares such as logger or requestid.

This packed forces an request that hasn't been matched by a route to return 404 instead.

Usage

Example app. Here, only /ping will return a 200. Anything else will return 404. However, the logger will still log the appropriate status code throughout.

import (
    "github.com/gofiber/fiber"
    "github.com/gofiber/logger"

    enforcer "github.com/hashamali/fiber-route-enforcer"
)

func main() {
    app := fiber.New()
    app.Use(logger.New())
    app.Use(enforcer.New())
    app.Get("/ping", enforcer.RouteHandler(
        func(c *fiber.Ctx) {
            c.Send(requestid.Get(c))
        },
    ))

    err := app.Listen(8080)
    if err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() fiber.Handler

New will create a new route enforcer middleware.

func RouteHandler

func RouteHandler(handler fiber.Handler) fiber.Handler

RouteHandler wraps all routes to mark them as found in a specific context.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL