test sse
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/go-ini/ini"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -81,6 +82,12 @@ func SetUp1() {
|
||||
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")
|
||||
jwtSecretKey := viper.GetString("jwt.SecretKey")
|
||||
|
||||
24
main.go
24
main.go
@@ -2,9 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"go_blog/conf"
|
||||
"go_blog/controllers"
|
||||
"go_blog/models"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -35,7 +37,7 @@ func init() {
|
||||
flag.BoolVar(&isOrmDebug, "orm", true, "是否开启gorm的debug信息")
|
||||
flag.Parse()
|
||||
|
||||
conf.SetUp()
|
||||
//conf.SetUp()
|
||||
conf.SetUp1()
|
||||
models.SetUp(isOrmDebug)
|
||||
}
|
||||
@@ -46,8 +48,28 @@ func main() {
|
||||
r.LoadHTMLGlob(templatePath)
|
||||
// 注册WebSocket路由
|
||||
registerRoutes(r)
|
||||
r.GET("/events", esSSE)
|
||||
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) {
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style type="text/css">
|
||||
body{
|
||||
background: #f8f8f8;
|
||||
}
|
||||
.container {
|
||||
width: 1170px;
|
||||
margin: 0 auto;
|
||||
@@ -15,7 +18,15 @@
|
||||
float: left;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.list-card {
|
||||
background: #fff;
|
||||
overflow: hidden;
|
||||
padding: 20px 20px 10px 20px;
|
||||
position: relative;
|
||||
border-radius: 10px;
|
||||
margin-bottom: 10px;
|
||||
height: 200px;
|
||||
}
|
||||
.sidebar {
|
||||
float: left;
|
||||
width: 25%;
|
||||
@@ -29,13 +40,7 @@
|
||||
|
||||
<div class="header">
|
||||
<div class="layui-main">
|
||||
<h1>Welcome to my blog!</h1> <a class="logo" href="https://www.hanxiaonuan.cn/">韩小暖的博客</a>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p><a href="/page/1">Go语言入门</a>
|
||||
<a href="/page/2">Gin框架入门</a>
|
||||
<a href="/page/3">Web开发基础</a></p>
|
||||
<h1>Welcome to my blog!<a class="logo" href="https://www.hanxiaonuan.cn/">韩小暖的博客</a></h1>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -43,6 +48,7 @@
|
||||
|
||||
<div class="post-list">
|
||||
{{ range .Items }}
|
||||
<div class="list-card">
|
||||
<div><a href="/post/{{ .Cid }}">
|
||||
<h1>Title: {{ .Title }}</h1>
|
||||
</a>
|
||||
@@ -50,10 +56,10 @@
|
||||
<p>Text: {{ .Text }}</p>
|
||||
<div>Created: {{ .Created }},</div>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
<div class="sidebar">
|
||||
<div class="sidebar layui-col-md3 layui-col-lg3">
|
||||
<div class="column">
|
||||
<h3 class="title-sidebar"><i class="layui-icon"></i> 博客信息</h3>
|
||||
<div class="personal-information">
|
||||
@@ -118,7 +124,6 @@
|
||||
</li>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user