Middleware
Trailing Slash
Remove or add the trailing slash on request paths, or redirect clients to one form.
Trailing Slash makes /users/ and /users reach the same route, or sends clients to one canonical form of each URL. Zinc already matches both forms unless you turn on StrictRouting, so you’d add this to a strict app, or when you want one URL per page for caches and search engines.
import "github.com/0mjs/zinc/middleware/trailingslash"
app := zinc.New(zinc.Config{StrictRouting: true})app.Use(trailingslash.New())
app.Get("/users", func(c *zinc.Context) error { return c.String("users at " + c.Path())})curl http://localhost:8080/users/ # users at /userscurl http://localhost:8080/users// # users at /usersWithout the middleware, the strict app answers /users/ with a 404. The slash is removed before routing, and the client isn’t told.
Register it with app.Use, so it runs before routing.
Defaults
Section titled “Defaults”| Setting | Default |
|---|---|
| Direction | remove trailing slashes |
| Client sees | nothing: the path is changed before routing |
| Redirect status | 301 Moved Permanently (when Redirect is on) |
Configuration
Section titled “Configuration”Redirect clients to the form without the slash, keeping the method and body:
app.Use(trailingslash.New(trailingslash.Config{ Redirect: true, StatusCode: zinc.StatusPermanentRedirect,}))curl -i "http://localhost:8080/users/?page=2"# HTTP/1.1 308 Permanent Redirect# Location: /users?page=2| Field | Default | Meaning |
|---|---|---|
Add |
false |
Adds a trailing slash instead of removing it |
Redirect |
false |
Redirects the client to the changed path instead of routing it directly. The query string is kept, the path keeps its escaping, and the target always stays on this site. |
StatusCode |
301 |
Redirect status when Redirect is on. A value outside 3xx sends 302 Found. |
Always end with a slash
Section titled “Always end with a slash”For sites whose URLs end in /, add the slash and redirect to it:
app := zinc.New(zinc.Config{StrictRouting: true})app.Use(trailingslash.New(trailingslash.Config{Add: true, Redirect: true}))
app.Get("/docs/", func(c *zinc.Context) error { return c.String("docs")})curl -i http://localhost:8080/docs# HTTP/1.1 301 Moved PermanentlyThe root path / is always left alone.
Static folders
Section titled “Static folders”A folder served by Static or StaticFS lives at its URL with a trailing slash, and redirects there without one. When removing slashes, Trailing Slash leaves a folder’s URL alone, so the two never redirect back and forth. Files under a static mount are treated like any other path.
Errors
Section titled “Errors”trailingslash never fails a request. If the changed path has no route, the client gets the usual 404.
trailingslash.New panics at startup when it’s given more than one Config.