Fox Framework Examples
This directory contains comprehensive examples demonstrating various features of the Fox web framework.
Examples Overview
| Example |
Description |
Key Features |
| 01-basic |
Basic usage |
Simple routes, path parameters, JSON responses |
| 02-binding |
Parameter binding |
JSON/URI/Query binding, validation |
| 03-middleware |
Middleware usage |
Custom middleware, authentication, rate limiting |
| 04-domain-routing |
Domain-based routing |
Multi-domain, regex patterns, multi-tenant |
| 05-custom-validator |
Custom validation |
IsValider interface, complex validation rules |
| 06-error-handling |
Error handling |
HTTP errors, custom errors, error codes |
| 07-logger-config |
Logger configuration |
Console/file logging, rotation, structured logs |
Quick Start
Each example has its own directory with:
main.go - The example code
README.md - Detailed documentation and testing instructions
Running an Example
# Navigate to an example directory
cd examples/01-basic
# Run the example
go run main.go
# Test the endpoints (see each example's README for specific commands)
curl http://localhost:8080/ping
Example Categories
🚀 Getting Started
Perfect for beginners:
- 01-basic - Start here to learn the basics
- 02-binding - Learn parameter binding and validation
- 06-error-handling - Understand error handling
🔧 Advanced Features
For more complex scenarios:
4. 03-middleware - Custom middleware and request processing
5. 04-domain-routing - Multi-domain and multi-tenant applications
6. 05-custom-validator - Complex validation rules
7. 07-logger-config - Production-ready logging
Common Patterns
1. Basic Route
router.GET("/hello", func(ctx *fox.Context) string {
return "Hello, World!"
})
2. Parameter Binding
type CreateUserRequest struct {
Username string `json:"username" binding:"required"`
Email string `json:"email" binding:"required,email"`
}
router.POST("/users", func(ctx *fox.Context, req *CreateUserRequest) (*User, error) {
// req is automatically bound and validated
return createUser(req), nil
})
3. Error Handling
var ErrNotFound = &httperrors.Error{
HTTPCode: http.StatusNotFound,
Code: "NOT_FOUND",
Message: "Resource not found",
}
router.GET("/user/:id", func(ctx *fox.Context) (*User, error) {
user, err := findUser(ctx.Param("id"))
if err != nil {
return nil, ErrNotFound
}
return user, nil
})
4. Middleware
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if !isAuthenticated(c) {
c.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"})
return
}
c.Next()
}
}
router.Use(AuthMiddleware())
5. Custom Validation
type SignupRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
func (sr *SignupRequest) IsValid() error {
if len(sr.Password) < 8 {
return &httperrors.Error{
HTTPCode: http.StatusBadRequest,
Code: "WEAK_PASSWORD",
Message: "Password must be at least 8 characters",
}
}
return nil
}
Learning Path
Beginner
- Start with 01-basic to understand basic routing
- Move to 02-binding for parameter handling
- Learn 06-error-handling for proper error responses
- Study 03-middleware for request processing
- Explore 05-custom-validator for complex validation
- Configure 07-logger-config for production logging
Advanced
- Implement 04-domain-routing for multi-tenant apps
- Combine patterns from multiple examples
- Build your own production application
Prerequisites
- Go 1.24 or higher
- Basic understanding of HTTP and REST APIs
- Familiarity with Go syntax
Installation
go get github.com/fox-gonic/fox
Additional Resources
Tips
- Read the README: Each example has detailed documentation
- Modify and Experiment: Try changing the code to understand how it works
- Check Error Messages: Fox provides detailed validation errors
- Use Structured Logging: Add context to your logs for better debugging
- Handle Errors Properly: Always return meaningful error messages
Contributing
Found an issue or have a suggestion for a new example? Please open an issue or pull request in the main repository.
License
These examples are part of the Fox framework and are released under the same license as Fox.