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

24
main.go
View File

@@ -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) {