پیکربندی HTTP سفارشی
بهطور پیشفرض، router.Run() یک سرور HTTP ساده راهاندازی میکند. برای استفاده در محیط production، ممکن است نیاز به سفارشیسازی timeoutها، محدودیتهای هدر، یا تنظیمات TLS داشته باشید. میتوانید این کار را با ایجاد http.Server خودتان و ارسال روتر Gin بهعنوان Handler انجام دهید.
استفاده پایه
Section titled “استفاده پایه”روتر Gin را مستقیماً به 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)}با تنظیمات سفارشی سرور
Section titled “با تنظیمات سفارشی سرور”یک struct http.Server ایجاد کنید تا timeoutهای خواندن/نوشتن و گزینههای دیگر را پیکربندی کنید:
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()}curl http://localhost:8080/ping# Output: pong