Built-inMiddleware
Built-in Middleware
Ready-to-use middleware functions provided by Squirrel Framework for common use cases.
Logger Middleware
Logs HTTP requests with method, path, status code, and response time information.
import (
"github.com/useranonymous001/squirrel"
"github.com/useranonymous001/squirrel/middlewares"
)
func main() {
server := SpawnServer()
// Add logger middleware globally
server.Use(middlewares.Logger)
server.Get("/", func(req *Request, res *Response) {
res.Write("Hello, World!")
res.Send()
})
server.Get("/api/users", func(req *Request, res *Response) {
res.JSON([]map[string]string{
{"id": "1", "name": "John Doe"},
{"id": "2", "name": "Jane Smith"},
})
res.Send()
})
server.Listen(":8080")
}Recover Middleware
Automatically recovers from panics and prevents the server from crashing. Enabled by default.
// Recovery middleware is enabled by default
// No additional setup required
func main() {
server := SpawnServer()
// This handler will panic, but the server won't crash
server.Get("/panic", func(req *Request, res *Response) {
panic("Something went wrong!")
// This line won't be reached
res.Write("This won't be sent")
res.Send()
})
// Other routes continue to work normally
server.Get("/", func(req *Request, res *Response) {
res.Write("Server is still running!")
res.Send()
})
server.Listen(":8080")
}
// When /panic is accessed:
// - The panic is caught
// - A 500 Internal Server Error is returned
// - The server continues running
// - Other routes remain accessibleUsing Built-in Middleware Together
Combining multiple built-in middleware functions for a complete setup.
import (
"github.com/useranonymous001/squirrel"
"github.com/useranonymous001/squirrel/middlewares"
)
func main() {
server := SpawnServer()
// Add built-in middleware
server.Use(middlewares.Logger)
// Recovery is enabled by default
// Add your custom middleware
server.Use(func(next HandlerFunc) HandlerFunc {
return func(req *Request, res *Response) {
// Add security headers
res.SetHeader("X-Content-Type-Options", "nosniff")
res.SetHeader("X-Frame-Options", "DENY")
res.SetHeader("X-XSS-Protection", "1; mode=block")
next(req, res)
}
})
// Routes
server.Get("/", func(req *Request, res *Response) {
res.JSON(map[string]string{
"message": "Server with logging and recovery",
"timestamp": time.Now().Format(time.RFC3339),
})
res.Send()
})
server.Get("/test-panic", func(req *Request, res *Response) {
// This will be caught by the recovery middleware
panic("Test panic - server will not crash")
})
server.Listen(":8080")
}
// Expected behavior:
// 1. All requests are logged with timestamps and durations
// 2. Security headers are added to all responses
// 3. Panics are caught and handled gracefully
// 4. Server remains stable and responsiveMiddleware Best Practices
Recommendations for using built-in middleware effectively.
✅ Do
- • Use Logger middleware for debugging and monitoring
- • Keep the default Recovery middleware enabled in production
- • Add built-in middleware early in the middleware chain
- • Combine with custom middleware for specific needs
- • Test panic scenarios to ensure recovery works
❌ Dont
- • Disable recovery middleware without good reason
- • Add logging middleware multiple times
- • Ignore panic logs in production
- • Rely solely on built-in middleware for security
- • Forget to handle errors in custom recovery handlers
Built-in middleware provides essential functionality out of the box. The Recovery middleware is automatically enabled to prevent server crashes, while the Logger middleware helps with debugging and monitoring. You can customize or disable these as needed for your specific use case.