Files
go_blog/main.go

123 lines
2.5 KiB
Go
Raw Normal View History

2023-12-20 16:50:05 +08:00
package main
2024-01-10 16:05:15 +08:00
import (
"flag"
2024-11-07 10:09:53 +08:00
"fmt"
2024-01-10 16:05:15 +08:00
"go_blog/conf"
2024-07-04 19:15:44 +08:00
"go_blog/controllers"
2024-01-10 16:05:15 +08:00
"go_blog/models"
2024-11-07 10:09:53 +08:00
"log"
2024-01-22 17:55:16 +08:00
"net/http"
2024-08-05 17:19:23 +08:00
"strconv"
2024-01-10 16:05:15 +08:00
"github.com/gin-gonic/gin"
)
2024-07-04 19:15:44 +08:00
type User struct {
Name string
Gender string
Age int
Password string
PasswordHash []byte
2024-01-10 16:05:15 +08:00
}
var host string
var port string
var isDebugMode bool
var isErrMsg bool
var isOrmDebug bool
2024-07-04 19:15:44 +08:00
const templatePath = "./templates/*"
2024-01-10 16:05:15 +08:00
func init() {
flag.StringVar(&host, "h", "127.0.0.1", "主机")
flag.StringVar(&port, "p", "", "监听端口")
flag.BoolVar(&isDebugMode, "debug", true, "是否开启debug")
flag.BoolVar(&isErrMsg, "err", true, "是否返回错误信息")
flag.BoolVar(&isOrmDebug, "orm", true, "是否开启gorm的debug信息")
flag.Parse()
<<<<<<< HEAD
2024-11-07 10:09:53 +08:00
//conf.SetUp()
2024-07-24 04:19:01 +08:00
conf.SetUp1()
=======
2024-01-10 16:05:15 +08:00
conf.SetUp()
>>>>>>> c628419559368f0270c2573d91a06a1a9531d53a
2024-01-10 16:05:15 +08:00
models.SetUp(isOrmDebug)
}
2023-12-20 16:50:05 +08:00
func main() {
r := gin.Default()
2024-07-04 19:15:44 +08:00
r.LoadHTMLGlob(templatePath)
// 注册WebSocket路由
registerRoutes(r)
2024-11-07 10:09:53 +08:00
r.GET("/events", esSSE)
2024-07-04 19:15:44 +08:00
r.Run(":8080")
}
2024-11-07 10:09:53 +08:00
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
}
}
2024-07-04 19:15:44 +08:00
func registerRoutes(r *gin.Engine) {
2024-08-01 02:44:06 +08:00
var items []models.Content
2024-08-02 21:47:22 +08:00
models.DB.Select("*").Limit(5).Find(&items, "type = ?", "post")
2023-12-20 16:50:05 +08:00
r.GET("/", func(c *gin.Context) {
2024-08-01 02:44:06 +08:00
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Items": items,
})
})
r.GET("/createcontent", func(c *gin.Context) {
c.HTML(http.StatusOK, "content.tmpl", nil)
2023-12-20 16:50:05 +08:00
})
2024-01-10 16:05:15 +08:00
2024-08-05 17:19:23 +08:00
r.GET("/post/:id", func(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 32)
if err != nil {
return
}
var content = models.Content{Cid: int32(id)}
models.DB.First(&content)
c.HTML(http.StatusOK, "post.tmpl", content)
})
user := getUserInfo()
2024-01-10 16:05:15 +08:00
r.GET("/login", func(c *gin.Context) {
c.HTML(200, "login.tmpl", map[string]interface{}{
"title": "这个是titile,传入templates中的",
"user": user,
})
})
2024-07-04 19:15:44 +08:00
r.GET("/ws", controllers.WebSocketHandler)
2024-08-01 02:44:06 +08:00
r.POST("/content", controllers.CreateContentHandler)
2024-07-04 19:15:44 +08:00
r.POST("/login", controllers.UsersLoginHandler)
2023-12-20 16:50:05 +08:00
}
2024-01-22 17:55:16 +08:00
2024-07-04 19:15:44 +08:00
func getUserInfo() User {
user := User{
Name: "user",
Gender: "male",
Age: 18,
Password: "nothings",
PasswordHash: []byte("nothings"),
2024-01-22 17:55:16 +08:00
}
2024-07-04 19:15:44 +08:00
return user
2024-01-22 17:55:16 +08:00
}