requireAPIKey checks for a valid X-API-Key header. Attach it to a group to gate an entire sub-tree without touching each handler.
()
| 63 | // requireAPIKey checks for a valid X-API-Key header. |
| 64 | // Attach it to a group to gate an entire sub-tree without touching each handler. |
| 65 | func requireAPIKey() pine.Middleware { |
| 66 | const validKey = "super-secret-key" |
| 67 | return func(next pine.Handler) pine.Handler { |
| 68 | return func(c *pine.Ctx) error { |
| 69 | if c.Header("X-API-Key") != validKey { |
| 70 | return c.Status(401).JSON(map[string]string{"error": "unauthorized"}) |
| 71 | } |
| 72 | return next(c) |
| 73 | } |
| 74 | } |
| 75 | } |