Router
Router API
Section titled “Router API”The Router is the core component of Fox, handling request routing and middleware.
Creating a Router
Section titled “Creating a Router”fox.New()
Section titled “fox.New()”Create a new router without any middleware:
r := fox.New()fox.Default()
Section titled “fox.Default()”Create a router with default middleware (Logger and Recovery):
r := fox.Default()HTTP Methods
Section titled “HTTP Methods”r.GET("/path", handler)r.POST("/path", handler)r.PUT("/path", handler)DELETE
Section titled “DELETE”r.DELETE("/path", handler)r.PATCH("/path", handler)r.HEAD("/path", handler)OPTIONS
Section titled “OPTIONS”r.OPTIONS("/path", handler)Route Groups
Section titled “Route Groups”Create route groups with shared prefix and middleware:
api := r.Group("/api")api.Use(authMiddleware()){ api.GET("/users", listUsers) api.POST("/users", createUser)}
v1 := api.Group("/v1"){ v1.GET("/products", listProducts)}Domain Routing
Section titled “Domain Routing”Route based on domain names:
// Exact domainr.Domain("api.example.com").GET("/users", handler)
// Wildcardr.Domain("*.example.com").GET("/info", handler)
// Regexr.DomainRegex(`^([a-z]+)\.example\.com$`).GET("/", handler)Middleware
Section titled “Middleware”Use Middleware
Section titled “Use Middleware”// Global middlewarer.Use(middleware1(), middleware2())
// Group middlewareapi := r.Group("/api")api.Use(authMiddleware())Built-in Middleware
Section titled “Built-in Middleware”import "github.com/fox-gonic/fox/middleware"
// Logger middlewarer.Use(middleware.Logger())
// Recovery middlewarer.Use(middleware.Recovery())
// CORS middlewarer.Use(middleware.CORS())Running the Server
Section titled “Running the Server”Run on Port
Section titled “Run on Port”r.Run(":8080")Run with TLS
Section titled “Run with TLS”r.RunTLS(":8443", "cert.pem", "key.pem")Run with Custom Server
Section titled “Run with Custom Server”server := &http.Server{ Addr: ":8080", Handler: r, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,}server.ListenAndServe()Configuration Options
Section titled “Configuration Options”Create Router with Options
Section titled “Create Router with Options”r := fox.New( fox.WithLoggerConfig(loggerConfig), fox.WithMaxMemory(32 << 20), // 32 MB fox.WithTrustedProxies([]string{"127.0.0.1"}),)Next Steps
Section titled “Next Steps”- Context API - Working with request context
- Middleware Guide - Creating custom middleware