-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.go
319 lines (265 loc) · 9.04 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) [2022] [巴拉迪维 BaratSemet]
// [ohUrlShortener] is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package main
import (
"embed"
"flag"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"strings"
"time"
"ohurlshortener/controller"
"ohurlshortener/service"
"ohurlshortener/storage"
"ohurlshortener/utils"
"github.com/Masterminds/sprig"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
)
const (
WebReadTimeout = 15 * time.Second
WebWriteTimeout = 15 * time.Second
// AccessLogCleanInterval 清理 Redis 中的访问日志的时间间隔
AccessLogCleanInterval = 1 * time.Minute
// Top25CalcInterval Top25 榜单计算间隔
Top25CalcInterval = 5 * time.Minute
// StatsSumCalcInterval 仪表盘页面中其他几个统计数据计算间隔
StatsSumCalcInterval = 5 * time.Minute
// StatsIpSumCalcInterval 全部访问日志分析统计的间隔
StatsIpSumCalcInterval = 30 * time.Minute
)
var (
//go:embed assets/* templates/*
FS embed.FS
group errgroup.Group
cmdStart string
cmdConfig string
)
func main() {
flag.StringVar(&cmdStart, "s", "", "starts ohUrlShortener service: admin | portal ")
flag.StringVar(&cmdConfig, "c", "config.ini", "config file path")
flag.Usage = func() {
fmt.Fprintf(os.Stdout, `ohUrlShortener version:%s
Usage: ohurlshortener [-s admin|portal|<omit to start both>] [-c config_file_path]`, utils.Version)
flag.PrintDefaults()
}
flag.Parse()
initSettings()
portalRoutes, err := initPortalRoutes()
utils.ExitOnError("Portal Routes initialization failed.", err)
adminRoutes, err := initAdminRoutes()
utils.ExitOnError("Admin Routes initialization failed.", err)
portal := &http.Server{
Addr: fmt.Sprintf(":%d", utils.AppConfig.Port),
Handler: portalRoutes,
ReadTimeout: WebReadTimeout,
WriteTimeout: WebWriteTimeout,
}
admin := &http.Server{
Addr: fmt.Sprintf(":%d", utils.AppConfig.AdminPort),
Handler: adminRoutes,
ReadTimeout: WebReadTimeout,
WriteTimeout: WebWriteTimeout,
}
if strings.EqualFold("admin", strings.TrimSpace(cmdStart)) {
startAdmin(group, *admin)
} else if strings.EqualFold("portal", strings.TrimSpace(cmdStart)) {
startPortal(group, *portal)
} else if utils.EmptyString(cmdStart) {
startPortal(group, *portal)
startAdmin(group, *admin)
} else {
flag.Usage()
}
err = group.Wait()
utils.ExitOnError("Group failed,", err)
}
func initSettings() {
_, err := utils.InitConfig(cmdConfig)
utils.ExitOnError("Config initialization failed.", err)
rs, err := storage.InitRedisService()
utils.ExitOnError("Redis initialization failed.", err)
if strings.EqualFold("redis", strings.ToLower(utils.CaptchaConfig.Store)) {
crs := storage.CaptchaRedisStore{KeyPrefix: "oh_captcha", Expiration: 1 * time.Minute, RedisService: rs}
captcha.SetCustomStore(&crs)
}
_, err = storage.InitDatabaseService()
storage.CallProcedureStatsIPSum() //recalculate when ohUrlShortener starts
storage.CallProcedureStatsTop25() // recalculate when ohUrlShortener starts
storage.CallProcedureStatsSum() // recalculate when ohUrlShortener starts
utils.ExitOnError("Database initialization failed.", err)
err = service.StoreAccessLogs()
utils.PrintOnError("StoreAccessLogs failed.", err)
_, err = service.ReloadUrls()
utils.PrintOnError("Reload urls failed.", err)
err = service.ReloadUsers()
utils.PrintOnError("Reload users failed.", err)
}
func startPortal(g errgroup.Group, server http.Server) {
group.Go(func() error {
log.Println("[StoreAccessLog] ticker starts to serve")
return startAccessLogsTicker()
})
group.Go(func() error {
log.Printf("[ohUrlShortener] portal starts at http://localhost:%d", utils.AppConfig.Port)
return server.ListenAndServe()
})
}
func startAdmin(g errgroup.Group, server http.Server) {
group.Go(func() error {
log.Println("[Top25Urls] ticker starts to serve")
return startTop25StatsTicker()
})
group.Go(func() error {
log.Println("[StatsIpSum] ticker starts to serve")
return startIPSumStatsTicker()
})
group.Go(func() error {
log.Println("[StatsSum] ticker starts to serve")
return startSumStatsTicker()
})
group.Go(func() error {
log.Printf("[ohUrlShortener] admin starts at http://localhost:%d", utils.AppConfig.AdminPort)
return server.ListenAndServe()
})
}
func initPortalRoutes() (http.Handler, error) {
if utils.AppConfig.Debug {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
router.Use(gin.Recovery(), controller.WebLogFormatHandler("Portal"))
sub, err := fs.Sub(FS, "assets")
if err != nil {
return nil, err
}
router.StaticFS("/assets", http.FS(sub))
tmpl, err := template.New("").Funcs(sprig.FuncMap()).ParseFS(FS, "templates/*.html")
if err != nil {
return nil, err
}
router.SetHTMLTemplate(tmpl)
router.GET("/:url", controller.ShortUrlDetail)
router.NoRoute(func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, "error.html", gin.H{
"title": "404 - ohUrlShortener",
"message": "您访问的页面已失效",
"code": http.StatusNotFound,
"label": "Error",
})
})
return router, nil
} // end of initPortalRoutes
func initAdminRoutes() (http.Handler, error) {
if utils.AppConfig.Debug {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
router.Use(gin.Recovery(), controller.WebLogFormatHandler("Admin"))
sub, err := fs.Sub(FS, "assets")
if err != nil {
return nil, err
}
router.StaticFS("/assets", http.FS(sub))
tmpl, err := template.New("").Funcs(sprig.FuncMap()).ParseFS(FS, "templates/**/*.html")
if err != nil {
return nil, err
}
router.SetHTMLTemplate(tmpl)
router.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusTemporaryRedirect, "/login")
})
router.GET("/login", controller.LoginPage)
router.POST("/login", controller.DoLogin)
router.GET("/captcha/:imageId", controller.ServeCaptchaImage)
router.POST("/captcha", controller.RequestCaptchaImage)
admin := router.Group("/admin", controller.AdminAuthHandler())
admin.GET("/", func(ctx *gin.Context) {
ctx.Redirect(http.StatusTemporaryRedirect, "/admin/dashboard")
})
admin.POST("/logout", controller.DoLogout)
admin.GET("/dashboard", controller.DashboardPage)
admin.GET("/urls", controller.UrlsPage)
admin.GET("/stats", controller.StatsPage)
admin.GET("/search_stats", controller.SearchStatsPage)
admin.GET("/access_logs", controller.AccessLogsPage)
admin.POST("/urls/generate", controller.GenerateShortUrl)
admin.POST("/urls/state", controller.ChangeState)
admin.POST("/urls/delete", controller.DeleteShortUrl)
admin.POST("/access_logs_export", controller.AccessLogsExport)
admin.GET("/users", controller.UsersPage)
api := router.Group("/api", controller.APIAuthHandler())
api.POST("/account", controller.APINewAdmin)
api.PUT("/account/:account/update", controller.APIAdminUpdate)
api.POST("/url", controller.APIGenShortUrl)
api.GET("/url/:url", controller.APIUrlInfo)
api.DELETE("/url/:url", controller.APIDeleteUrl)
api.PUT("/url/:url/change_state", controller.APIUpdateUrl)
router.NoRoute(func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, "error.html", gin.H{
"title": "404 - ohUrlShortener",
"message": "您访问的页面已失效",
"code": http.StatusNotFound,
"label": "Error",
})
})
return router, nil
} // end of initAdminRoutes
func startAccessLogsTicker() error {
redisTicker := time.NewTicker(AccessLogCleanInterval)
for range redisTicker.C {
log.Println("[StoreAccessLog] Start.")
if err := service.StoreAccessLogs(); err != nil {
log.Printf("Error while trying to store access_log %s", err)
}
log.Println("[StoreAccessLog] Finish.")
}
return nil
}
func startTop25StatsTicker() error {
top25Ticker := time.NewTicker(Top25CalcInterval)
for range top25Ticker.C {
log.Println("[Top25Urls Ticker] Start.")
if err := storage.CallProcedureStatsTop25(); err != nil {
log.Printf("Error while trying to calculate Top25Urls %s", err)
}
log.Println("[Top25Urls Ticker] Finish.")
}
return nil
}
func startIPSumStatsTicker() error {
statsIpSumTicker := time.NewTicker(StatsIpSumCalcInterval)
for range statsIpSumTicker.C {
log.Println("[StatsIpSum Ticker] Start.")
if err := storage.CallProcedureStatsIPSum(); err != nil {
log.Printf("Error while trying to calculate StatsIpSum %s", err)
}
log.Println("[StatsIpSum Ticker] Finish.")
}
return nil
}
func startSumStatsTicker() error {
statsSumTicker := time.NewTicker(StatsSumCalcInterval)
for range statsSumTicker.C {
log.Println("[StatsSum Ticker] Start.")
if err := storage.CallProcedureStatsSum(); err != nil {
log.Printf("Error while trying to calculate StatsSum %s", err)
}
log.Println("[StatsSum Ticker] Finish.")
}
return nil
}