Skip to content

Custom HTTP configuration

By default, router.Run() starts a basic HTTP server. For production use, you may need to customize timeouts, header limits, or TLS settings. You can do this by creating your own http.Server and passing the Gin router as the Handler.

Basic usage

Pass the Gin router directly to http.ListenAndServe:

package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
http.ListenAndServe(":8080", router)
}

With custom server settings

Create an http.Server struct to configure read/write timeouts and other options:

package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
s := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}

Test it

Terminal window
curl http://localhost:8080/ping
# Output: pong

See also