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