Skip to content

Server Configuration

Gin offers flexible server configuration options. Because gin.Engine implements the http.Handler interface, you can use it with Go’s standard net/http.Server to control timeouts, TLS, and other settings directly.

Using a custom http.Server

By default, router.Run() starts a basic HTTP server. For production use, create your own http.Server to set timeouts and other options:

func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.String(200, "ok")
})
s := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}

This gives you full access to Go’s server configuration while keeping all of Gin’s routing and middleware capabilities.

In this section