This commit is contained in:
张超
2024-11-07 10:09:53 +08:00
parent a96dff5804
commit feaff725ce
3 changed files with 45 additions and 11 deletions

View File

@@ -11,6 +11,7 @@ import (
"log" "log"
"time" "time"
"github.com/fsnotify/fsnotify"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@@ -81,6 +82,12 @@ func SetUp1() {
log.Fatalf("Error reading config file, %s", err) log.Fatalf("Error reading config file, %s", err)
} }
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
// 配置文件发生变更之后会调用的回调函数
fmt.Println("Config file changed:", e.Name)
})
// 获取配置值 // 获取配置值
mysqlHost := viper.GetString("mysql.Host") mysqlHost := viper.GetString("mysql.Host")
jwtSecretKey := viper.GetString("jwt.SecretKey") jwtSecretKey := viper.GetString("jwt.SecretKey")

24
main.go
View File

@@ -2,9 +2,11 @@ package main
import ( import (
"flag" "flag"
"fmt"
"go_blog/conf" "go_blog/conf"
"go_blog/controllers" "go_blog/controllers"
"go_blog/models" "go_blog/models"
"log"
"net/http" "net/http"
"strconv" "strconv"
@@ -35,7 +37,7 @@ func init() {
flag.BoolVar(&isOrmDebug, "orm", true, "是否开启gorm的debug信息") flag.BoolVar(&isOrmDebug, "orm", true, "是否开启gorm的debug信息")
flag.Parse() flag.Parse()
conf.SetUp() //conf.SetUp()
conf.SetUp1() conf.SetUp1()
models.SetUp(isOrmDebug) models.SetUp(isOrmDebug)
} }
@@ -46,8 +48,28 @@ func main() {
r.LoadHTMLGlob(templatePath) r.LoadHTMLGlob(templatePath)
// 注册WebSocket路由 // 注册WebSocket路由
registerRoutes(r) registerRoutes(r)
r.GET("/events", esSSE)
r.Run(":8080") r.Run(":8080")
} }
func esSSE(c *gin.Context) {
w := c.Writer
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
_, ok := w.(http.Flusher)
if !ok {
log.Panic("server not support") //浏览器不兼容
}
_, err := fmt.Fprintf(w, "data: %s\n\n", "dsdf")
if err != nil {
return
}
}
func registerRoutes(r *gin.Engine) { func registerRoutes(r *gin.Engine) {

View File

@@ -5,6 +5,9 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css"> <style type="text/css">
body{
background: #f8f8f8;
}
.container { .container {
width: 1170px; width: 1170px;
margin: 0 auto; margin: 0 auto;
@@ -15,7 +18,15 @@
float: left; float: left;
display: block; display: block;
} }
.list-card {
background: #fff;
overflow: hidden;
padding: 20px 20px 10px 20px;
position: relative;
border-radius: 10px;
margin-bottom: 10px;
height: 200px;
}
.sidebar { .sidebar {
float: left; float: left;
width: 25%; width: 25%;
@@ -29,13 +40,7 @@
<div class="header"> <div class="header">
<div class="layui-main"> <div class="layui-main">
<h1>Welcome to my blog!</h1> <a class="logo" href="https://www.hanxiaonuan.cn/">韩小暖的博客</a> <h1>Welcome to my blog!<a class="logo" href="https://www.hanxiaonuan.cn/">韩小暖的博客</a></h1>
</div>
<div>
<p><a href="/page/1">Go语言入门</a>
<a href="/page/2">Gin框架入门</a>
<a href="/page/3">Web开发基础</a></p>
</div> </div>
</div> </div>
@@ -43,6 +48,7 @@
<div class="post-list"> <div class="post-list">
{{ range .Items }} {{ range .Items }}
<div class="list-card">
<div><a href="/post/{{ .Cid }}"> <div><a href="/post/{{ .Cid }}">
<h1>Title: {{ .Title }}</h1> <h1>Title: {{ .Title }}</h1>
</a> </a>
@@ -50,10 +56,10 @@
<p>Text: {{ .Text }}</p> <p>Text: {{ .Text }}</p>
<div>Created: {{ .Created }},</div> <div>Created: {{ .Created }},</div>
</div> </div>
</div>
{{ end }} {{ end }}
</div> </div>
<div class="sidebar"> <div class="sidebar">
<div class="sidebar layui-col-md3 layui-col-lg3">
<div class="column"> <div class="column">
<h3 class="title-sidebar"><i class="layui-icon"></i> 博客信息</h3> <h3 class="title-sidebar"><i class="layui-icon"></i> 博客信息</h3>
<div class="personal-information"> <div class="personal-information">
@@ -120,7 +126,6 @@
</div> </div>
</div> </div>
</div> </div>
</div>
</body> </body>
</html> </html>