-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (72 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"embed"
_ "embed"
"github.com/cute-angelia/go-utils/conf"
"github.com/cute-angelia/go-utils/db/igorm"
"github.com/cute-angelia/go-utils/logger"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"html/template"
"log"
"mime"
"net/http"
"note/controller/notepad"
"note/model"
"path"
"path/filepath"
"strings"
"time"
)
//go:embed static/* templates/*
var staticFS embed.FS
func main() {
// 加载配置
conf.MustLoadConfigFile("config.toml")
// 日志
logger.NewLogger("notepad", viper.GetBool("note.wirteLogFile"))
// 初始化数据库
igorm.Load("notepad").Build(
igorm.WithDsn(viper.GetString("note.mysqDsn")),
igorm.WithMaxIdleConns(0),
igorm.WithMaxOpenConnss(10),
).MustInitMysql()
// 初始化表结构
orm, _ := igorm.GetGormMysql("notepad")
orm.AutoMigrate(&model.NotepadModel{})
// Gin
r := gin.Default()
// gin.SetMode(gin.ReleaseMode)
// router
r.GET("/", notepad.Index)
r.GET("/robots.txt", notepad.RobotsHandle)
r.POST("/:id", notepad.ProcessHandle)
r.GET("/:id", notepad.ProcessHandle)
r.GET("/:id/md", notepad.MarkdownHandle)
r.GET("/:id/raw", notepad.RawHandle)
// 设置模板
templ := template.Must(template.New("").ParseFS(staticFS, "templates/*.html"))
r.SetHTMLTemplate(templ)
// r.LoadHTMLGlob("./static/*.html")
// 静态文件
// r.StaticFS("/static", http.FS(staticFS))
r.GET("/static/*filepath", func(c *gin.Context) {
contentType := mime.TypeByExtension(filepath.Ext(c.Request.URL.Path))
c.Header("Cache-Type", contentType)
c.Header("Cache-Control", "public, max-age=31536000")
etag := time.Now().Format("20060102150405")
c.Header("ETag", etag)
if match := c.GetHeader("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
c.Status(http.StatusNotModified)
return
}
}
c.FileFromFS(path.Join("/", c.Request.URL.Path), http.FS(staticFS))
})
port := viper.GetString("note.serverPort")
log.Println("run: http://127.0.0.1:" + port)
if err := r.Run(":" + port); err != nil {
panic(err)
}
}