تخطَّ إلى المحتوى

تكوين HTTP مخصص

بشكل افتراضي، router.Run() يبدأ خادم HTTP أساسي. للاستخدام في الإنتاج، قد تحتاج لتخصيص المهلات الزمنية أو حدود الترويسات أو إعدادات TLS. يمكنك القيام بذلك بإنشاء http.Server خاص بك وتمرير موجّه Gin كـ Handler.

مرر موجّه 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)
}

أنشئ هيكل http.Server لتكوين مهلات القراءة/الكتابة وخيارات أخرى:

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()
}
Terminal window
curl http://localhost:8080/ping
# Output: pong