2022-08-22 01:36:13 +00:00
|
|
|
package main
|
|
|
|
|
2022-08-27 13:21:05 +00:00
|
|
|
import (
|
2022-11-05 07:07:08 +00:00
|
|
|
"flag"
|
2022-11-15 08:36:21 +00:00
|
|
|
"fmt"
|
2023-01-12 10:17:34 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/actions"
|
2023-01-12 12:42:16 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/cache"
|
|
|
|
"github/fthvgb1/wp-go/internal/config"
|
|
|
|
"github/fthvgb1/wp-go/internal/db"
|
2023-01-13 04:50:37 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/logs"
|
|
|
|
"github/fthvgb1/wp-go/internal/mail"
|
2023-01-12 12:42:16 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/plugins"
|
2023-01-12 10:17:34 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/route"
|
2023-01-12 12:42:16 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/wpconfig"
|
|
|
|
"github/fthvgb1/wp-go/model"
|
2022-11-15 08:36:21 +00:00
|
|
|
"log"
|
2022-10-13 03:09:52 +00:00
|
|
|
"math/rand"
|
2022-11-15 08:36:21 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-11-16 07:31:37 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-11-15 08:36:21 +00:00
|
|
|
"syscall"
|
2022-09-28 13:16:05 +00:00
|
|
|
"time"
|
2022-08-27 13:21:05 +00:00
|
|
|
)
|
|
|
|
|
2022-11-16 02:17:29 +00:00
|
|
|
var confPath string
|
2022-11-16 07:31:37 +00:00
|
|
|
var port string
|
2022-11-16 03:10:11 +00:00
|
|
|
var middleWareReloadFn func()
|
2022-11-16 07:31:37 +00:00
|
|
|
var intReg = regexp.MustCompile(`^\d`)
|
2022-11-16 02:17:29 +00:00
|
|
|
|
2022-08-27 13:21:05 +00:00
|
|
|
func init() {
|
2022-11-16 02:17:29 +00:00
|
|
|
flag.StringVar(&confPath, "c", "config.yaml", "config file")
|
2022-11-16 07:31:37 +00:00
|
|
|
flag.StringVar(&port, "p", "", "port")
|
2022-11-05 07:07:08 +00:00
|
|
|
flag.Parse()
|
2022-11-16 07:31:37 +00:00
|
|
|
if port == "" && os.Getenv("PORT") == "" {
|
|
|
|
port = "80"
|
|
|
|
}
|
|
|
|
if intReg.MatchString(port) && !strings.Contains(port, ":") {
|
|
|
|
port = ":" + port
|
|
|
|
}
|
2022-10-13 03:09:52 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2022-11-16 02:17:29 +00:00
|
|
|
err := initConf(confPath)
|
2022-08-27 13:21:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-11-16 02:17:29 +00:00
|
|
|
actions.InitFeed()
|
2023-01-12 12:42:16 +00:00
|
|
|
cache.InitActionsCommonCache()
|
2022-11-16 02:17:29 +00:00
|
|
|
plugins.InitDigestCache()
|
|
|
|
go cronClearCache()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initConf(c string) (err error) {
|
|
|
|
err = config.InitConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-09-23 13:46:34 +00:00
|
|
|
|
2022-08-27 13:21:05 +00:00
|
|
|
err = db.InitDb()
|
|
|
|
if err != nil {
|
2022-11-16 02:17:29 +00:00
|
|
|
return
|
2022-08-27 13:21:05 +00:00
|
|
|
}
|
2023-01-12 12:42:16 +00:00
|
|
|
model.InitDB(db.NewSqlxDb(db.Db))
|
|
|
|
err = wpconfig.InitOptions()
|
2022-09-01 02:31:11 +00:00
|
|
|
if err != nil {
|
2022-11-16 02:17:29 +00:00
|
|
|
return
|
2022-09-01 02:31:11 +00:00
|
|
|
}
|
2023-01-12 12:42:16 +00:00
|
|
|
err = wpconfig.InitTerms()
|
2022-09-05 01:49:14 +00:00
|
|
|
if err != nil {
|
2022-11-16 02:17:29 +00:00
|
|
|
return
|
2022-09-05 01:49:14 +00:00
|
|
|
}
|
2022-11-16 02:17:29 +00:00
|
|
|
return
|
2022-09-28 13:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cronClearCache() {
|
2022-11-15 08:36:21 +00:00
|
|
|
t := time.NewTicker(config.Conf.Load().CrontabClearCacheTime)
|
2022-09-28 13:16:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2023-01-12 12:42:16 +00:00
|
|
|
cache.ClearCache()
|
2022-09-28 13:16:05 +00:00
|
|
|
plugins.ClearDigestCache()
|
2022-10-07 15:16:25 +00:00
|
|
|
actions.ClearCache()
|
2022-09-28 13:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-27 13:21:05 +00:00
|
|
|
}
|
2022-08-22 01:36:13 +00:00
|
|
|
|
2022-11-16 02:17:29 +00:00
|
|
|
func flushCache() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err := mail.SendMail([]string{config.Conf.Load().Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r))
|
|
|
|
logs.ErrPrintln(err, "发邮件失败")
|
|
|
|
}
|
|
|
|
}()
|
2023-01-12 12:42:16 +00:00
|
|
|
cache.FlushCache()
|
2022-11-16 02:17:29 +00:00
|
|
|
plugins.FlushCache()
|
|
|
|
actions.FlushCache()
|
2022-11-16 02:51:39 +00:00
|
|
|
log.Println("all cache flushed")
|
2022-11-16 02:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func reload() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Println(r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err := config.InitConfig(confPath)
|
|
|
|
logs.ErrPrintln(err, "获取配置文件失败", confPath)
|
2023-01-12 12:42:16 +00:00
|
|
|
err = wpconfig.InitOptions()
|
2022-11-16 02:17:29 +00:00
|
|
|
logs.ErrPrintln(err, "获取网站设置WpOption失败")
|
2023-01-12 12:42:16 +00:00
|
|
|
err = wpconfig.InitTerms()
|
2022-11-16 02:17:29 +00:00
|
|
|
logs.ErrPrintln(err, "获取WpTerms表失败")
|
2022-11-16 02:51:39 +00:00
|
|
|
if middleWareReloadFn != nil {
|
|
|
|
middleWareReloadFn()
|
|
|
|
}
|
|
|
|
flushCache()
|
2022-11-16 02:17:29 +00:00
|
|
|
log.Println("reload complete")
|
|
|
|
}
|
|
|
|
|
2022-11-15 08:36:21 +00:00
|
|
|
func signalNotify() {
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2)
|
|
|
|
for {
|
|
|
|
switch <-c {
|
|
|
|
case syscall.SIGUSR1:
|
2022-11-16 02:17:29 +00:00
|
|
|
go reload()
|
2022-11-15 08:36:21 +00:00
|
|
|
case syscall.SIGUSR2:
|
2022-11-16 02:17:29 +00:00
|
|
|
go flushCache()
|
2022-11-15 08:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 01:36:13 +00:00
|
|
|
func main() {
|
2022-11-15 08:36:21 +00:00
|
|
|
go signalNotify()
|
2022-11-16 02:17:29 +00:00
|
|
|
Gin, reloadFn := route.SetupRouter()
|
|
|
|
middleWareReloadFn = reloadFn
|
2022-11-16 07:31:37 +00:00
|
|
|
err := Gin.Run(port)
|
2022-08-27 13:21:05 +00:00
|
|
|
if err != nil {
|
2022-09-01 02:31:11 +00:00
|
|
|
panic(err)
|
2022-08-27 13:21:05 +00:00
|
|
|
}
|
2022-08-22 01:36:13 +00:00
|
|
|
}
|