mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-05-23 10:28:13 +08:00
gin 简单测试
This commit is contained in:
parent
94e83cc4a0
commit
9e8ae1c4bc
16
gin/main.go
16
gin/main.go
@ -16,11 +16,24 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
e := gin.Default()
|
e := gin.Default()
|
||||||
e.Use(favicon.New("./favicon.ico"))
|
// 注册 favicon
|
||||||
|
e.Use(favicon.New("./templates/favicon.ico"))
|
||||||
e.Use(gin.Recovery())
|
e.Use(gin.Recovery())
|
||||||
|
|
||||||
controller.Register(e)
|
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{
|
srv := &http.Server{
|
||||||
Addr: ":8848",
|
Addr: ":8848",
|
||||||
Handler: e,
|
Handler: e,
|
||||||
@ -32,6 +45,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// 等待中断信号以优雅地关闭服务器
|
||||||
quit := make(chan os.Signal, 1)
|
quit := make(chan os.Signal, 1)
|
||||||
signal.Notify(quit, os.Interrupt)
|
signal.Notify(quit, os.Interrupt)
|
||||||
<-quit
|
<-quit
|
||||||
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
13
gin/templates/index.html
Normal file
13
gin/templates/index.html
Normal 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>
|
4
gin/templates/static/css/style.css
Normal file
4
gin/templates/static/css/style.css
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
body {
|
||||||
|
background-color: #f2f2f2;
|
||||||
|
display: flex;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user