gin 简单测试

This commit is contained in:
shikong 2024-11-14 00:24:14 +08:00
parent 94e83cc4a0
commit 9e8ae1c4bc
Signed by: Shikong
GPG Key ID: BD85FF18B373C341
4 changed files with 32 additions and 1 deletions

View File

@ -16,11 +16,24 @@ import (
func main() {
e := gin.Default()
e.Use(favicon.New("./favicon.ico"))
// 注册 favicon
e.Use(favicon.New("./templates/favicon.ico"))
e.Use(gin.Recovery())
controller.Register(e)
// 静态资源文件
e.Static("/static", "./templates/static")
// 加载 HTML 模板
e.LoadHTMLGlob("templates/*.html")
e.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"msg": "Hello Gin",
})
})
// 启动 HTTP 服务
srv := &http.Server{
Addr: ":8848",
Handler: e,
@ -32,6 +45,7 @@ func main() {
}
}()
// 等待中断信号以优雅地关闭服务器
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

13
gin/templates/index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gin 框架</title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<h3 style="text-align: center;">{{.msg}}</h3>
</body>
</html>

View File

@ -0,0 +1,4 @@
body {
background-color: #f2f2f2;
display: flex;
}