“Golang Gin 部署上线”的版本间差异
本页内容
(创建页面,内容为“分类:Golang Gin Web 框架教程 == 自定义 HTTP 配置 == 直接使用 http.ListenAndServe(),如下所示: <sample title="" desc="" > func main() { router := gin.Default() http.ListenAndServe(":8080", router) } </sample> 配置更多参数。 <sample title="" desc=""> func main() { router := gin.Default() s := &http.Server{ Addr: ":8080", Handler: router, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.S…”) |
|||
第89行: | 第89行: | ||
} | } | ||
log.Println("Server exiting") | log.Println("Server exiting") | ||
} | |||
</sample> | |||
== 支持SSL基于Let's Encrypt == | |||
一行代码支持 LetsEncrypt HTTPS servers 示例。 | |||
<sample title="" desc=""> | |||
package main | |||
import ( | |||
"log" | |||
"github.com/gin-gonic/autotls" | |||
"github.com/gin-gonic/gin" | |||
) | |||
func main() { | |||
r := gin.Default() | |||
// Ping handler | |||
r.GET("/ping", func(c *gin.Context) { | |||
c.String(200, "pong") | |||
}) | |||
log.Fatal(autotls.Run(r, "example1.com", "example2.com")) | |||
} | |||
</sample> | |||
自定义 autocert manager 示例。 | |||
<sample title="" desc=""> | |||
package main | |||
import ( | |||
"log" | |||
"github.com/gin-gonic/autotls" | |||
"github.com/gin-gonic/gin" | |||
"golang.org/x/crypto/acme/autocert" | |||
) | |||
func main() { | |||
r := gin.Default() | |||
// Ping handler | |||
r.GET("/ping", func(c *gin.Context) { | |||
c.String(200, "pong") | |||
}) | |||
m := autocert.Manager{ | |||
Prompt: autocert.AcceptTOS, | |||
HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"), | |||
Cache: autocert.DirCache("/var/www/.cache"), | |||
} | |||
log.Fatal(autotls.RunWithManager(r, &m)) | |||
} | } | ||
</sample> | </sample> |
2022年9月28日 (三) 11:53的最新版本
自定义 HTTP 配置
直接使用 http.ListenAndServe(),如下所示:
示例
func main() {
router := gin.Default()
http.ListenAndServe(":8080", router)
}
配置更多参数。
示例
func main() {
router := gin.Default()
s := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}
优雅地重启或停止
你想优雅地重启或停止 web 服务器吗?有一些方法可以做到这一点。这样可以减少对用户的影响。
请使用的是 Go 1.8,可以使用 http.Server 内置的 Shutdown() 方法优雅地停止服务。
Shutdown 优雅地关闭服务器而不中断任何活动连接。关闭首先关闭所有打开的侦听器,然后关闭所有空闲连接,然后无限期地等待连接返回空闲状态,然后关闭。如果提供的上下文在关闭完成之前过期,则 Shutdown 返回上下文的错误,否则返回关闭服务器的底层侦听器返回的任何错误。
当调用 Shutdown 时,Serve、ListenAndServe 和 ListenAndServeTLS 立即返回 ErrServerClosed。确保程序不会退出,而是等待 Shutdown 返回。
Shutdown 不会尝试关闭或等待被劫持的连接,例如 WebSockets。Shutdown 的调用者应单独通知此类长期连接的关闭并等待它们关闭(如果需要)。有关注册关闭通知功能的方法,请参阅 RegisterOnShutdown。
一旦在服务器上调用了 Shutdown,它就不能被重用;将来对 Serve 等方法的调用将返回 ErrServerClosed。
示例
// +build go1.8
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
time.Sleep(5 * time.Second)
c.String(http.StatusOK, "Welcome Gin Server")
})
srv := &http.Server{
Addr: ":8080",
Handler: router,
}
go func() {
// 服务连接
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}
支持SSL基于Let's Encrypt
一行代码支持 LetsEncrypt HTTPS servers 示例。
示例
package main
import (
"log"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// Ping handler
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
log.Fatal(autotls.Run(r, "example1.com", "example2.com"))
}
自定义 autocert manager 示例。
示例
package main
import (
"log"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/acme/autocert"
)
func main() {
r := gin.Default()
// Ping handler
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("example1.com", "example2.com"),
Cache: autocert.DirCache("/var/www/.cache"),
}
log.Fatal(autotls.RunWithManager(r, &m))
}