From 9e8ae1c4bce2a5b00acb951dbf5d0da1f0101e34 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Thu, 14 Nov 2024 00:24:14 +0800 Subject: [PATCH] =?UTF-8?q?gin=20=E7=AE=80=E5=8D=95=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/main.go | 16 +++++++++++++++- gin/{ => templates}/favicon.ico | Bin gin/templates/index.html | 13 +++++++++++++ gin/templates/static/css/style.css | 4 ++++ 4 files changed, 32 insertions(+), 1 deletion(-) rename gin/{ => templates}/favicon.ico (100%) create mode 100644 gin/templates/index.html create mode 100644 gin/templates/static/css/style.css diff --git a/gin/main.go b/gin/main.go index 8be0858..26d0385 100644 --- a/gin/main.go +++ b/gin/main.go @@ -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 diff --git a/gin/favicon.ico b/gin/templates/favicon.ico similarity index 100% rename from gin/favicon.ico rename to gin/templates/favicon.ico diff --git a/gin/templates/index.html b/gin/templates/index.html new file mode 100644 index 0000000..8115350 --- /dev/null +++ b/gin/templates/index.html @@ -0,0 +1,13 @@ + + + + + + Gin 框架 + + + + +

{{.msg}}

+ + \ No newline at end of file diff --git a/gin/templates/static/css/style.css b/gin/templates/static/css/style.css new file mode 100644 index 0000000..59c47b5 --- /dev/null +++ b/gin/templates/static/css/style.css @@ -0,0 +1,4 @@ +body { + background-color: #f2f2f2; + display: flex; +} \ No newline at end of file