forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
165 lines (127 loc) · 3.91 KB
/
config.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package utils
import (
"chat/globals"
"fmt"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"strings"
)
var configFile = "config/config.yaml"
var configExampleFile = "config.example.yaml"
var redirectRoutes = []string{
"/v1",
"/mj",
}
func ReadConf() {
viper.SetConfigFile(configFile)
if !IsFileExist(configFile) {
fmt.Println(fmt.Sprintf("[service] config.yaml not found, creating one from template: %s", configExampleFile))
if err := CopyFile(configExampleFile, configFile); err != nil {
fmt.Println(err)
}
}
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}
func NewEngine() *gin.Engine {
if viper.GetBool("debug") {
return gin.Default()
}
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
engine.Use(gin.Recovery())
return engine
}
func ApplySeo(title, icon string) {
// seo optimization
if !viper.GetBool("serve_static") {
return
}
content, err := ReadFile("./app/dist/index.html")
if err != nil {
globals.Warn(fmt.Sprintf("[service] failed to read index.html: %s", err.Error()))
return
}
if len(title) > 0 {
content = strings.ReplaceAll(content, "Chat Nio", title)
content = strings.ReplaceAll(content, "chatnio", strings.ToLower(title))
}
if len(icon) > 0 {
content = strings.ReplaceAll(content, "/favicon.ico", icon)
}
if err := WriteFile("./app/dist/index.cache.html", content, true); err != nil {
globals.Warn(fmt.Sprintf("[service] failed to write index.cache.html: %s", err.Error()))
}
globals.Info("[service] seo optimization applied to index.cache.html")
}
func ApplyPWAManifest(content string) {
// pwa manifest rewrite (site.webmanifest -> site.cache.webmanifest)
if !viper.GetBool("serve_static") {
return
}
if len(content) == 0 {
// read from site.webmanifest if not provided
var err error
content, err = ReadFile("./app/dist/site.webmanifest")
if err != nil {
globals.Warn(fmt.Sprintf("[service] failed to read site.webmanifest: %s", err.Error()))
return
}
}
if err := WriteFile("./app/dist/site.cache.webmanifest", content, true); err != nil {
globals.Warn(fmt.Sprintf("[service] failed to write site.cache.webmanifest: %s", err.Error()))
}
globals.Info("[service] pwa manifest applied to site.cache.webmanifest")
}
func ReadPWAManifest() (content string) {
// read site.cache.webmanifest content or site.webmanifest if not found
if !viper.GetBool("serve_static") {
return
}
if text, err := ReadFile("./app/dist/site.cache.webmanifest"); err == nil && len(text) > 0 {
return text
}
if text, err := ReadFile("./app/dist/site.webmanifest"); err != nil {
globals.Warn(fmt.Sprintf("[service] failed to read site.webmanifest: %s", err.Error()))
} else {
content = text
}
return
}
func RegisterStaticRoute(engine *gin.Engine) {
// static files are in ~/app/dist
if !viper.GetBool("serve_static") {
engine.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{"status": false, "message": "not found or method not allowed"})
})
return
}
if !IsFileExist("./app/dist") {
fmt.Println("[service] app/dist not found, please run `npm run build`")
return
}
ApplySeo(viper.GetString("system.general.title"), viper.GetString("system.general.logo"))
ApplyPWAManifest(viper.GetString("system.general.pwamanifest"))
engine.GET("/", func(c *gin.Context) {
c.File("./app/dist/index.cache.html")
})
engine.GET("/site.webmanifest", func(c *gin.Context) {
c.File("./app/dist/site.cache.webmanifest")
})
engine.Use(static.Serve("/", static.LocalFile("./app/dist", true)))
engine.NoRoute(func(c *gin.Context) {
c.File("./app/dist/index.cache.html")
})
for _, route := range redirectRoutes {
engine.Any(fmt.Sprintf("%s/*path", route), func(c *gin.Context) {
path := c.Param("path")
c.Request.URL.Path = path
engine.HandleContext(c)
})
}
fmt.Println(`[service] start serving static files from ~/app/dist`)
}