介绍
本文主要介绍如何自定义HTTP配置, 在gin框架中可以直接使用 http.ListenAndServe()来实现。
案例
源码:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func test01() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
http.ListenAndServe(":8080", r)
}
func test02() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
s := &http.Server{
Addr: ":8080",
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}
func main() {
//test01()
test02()
}
测试:
$ curl http://127.0.0.1:8080/ping
pong