wp-go/main.go

99 lines
1.7 KiB
Go
Raw Normal View History

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"
2022-10-07 14:27:34 +00:00
"github/fthvgb1/wp-go/actions"
2022-09-19 11:11:36 +00:00
"github/fthvgb1/wp-go/actions/common"
2022-11-04 02:38:59 +00:00
"github/fthvgb1/wp-go/config"
2022-08-27 13:21:05 +00:00
"github/fthvgb1/wp-go/db"
2022-11-15 08:36:21 +00:00
"github/fthvgb1/wp-go/mail"
"github/fthvgb1/wp-go/models"
2022-09-26 08:35:38 +00:00
"github/fthvgb1/wp-go/plugins"
2022-09-01 02:31:11 +00:00
"github/fthvgb1/wp-go/route"
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"
"syscall"
2022-09-28 13:16:05 +00:00
"time"
2022-08-27 13:21:05 +00:00
)
func init() {
2022-11-05 07:07:08 +00:00
var c string
flag.StringVar(&c, "c", "config.yaml", "config file")
flag.Parse()
2022-10-13 03:09:52 +00:00
rand.Seed(time.Now().UnixNano())
2022-11-05 07:07:08 +00:00
err := config.InitConfig(c)
2022-08-27 13:21:05 +00:00
if err != nil {
panic(err)
}
2022-09-23 13:46:34 +00:00
2022-08-27 13:21:05 +00:00
err = db.InitDb()
if err != nil {
panic(err)
}
models.InitDB(db.NewSqlxDb(db.Db))
2022-11-15 03:11:08 +00:00
err = config.InitOptions()
2022-09-01 02:31:11 +00:00
if err != nil {
panic(err)
}
2022-11-15 03:11:08 +00:00
err = config.InitTerms()
2022-09-05 01:49:14 +00:00
if err != nil {
panic(err)
}
2022-10-07 14:27:34 +00:00
actions.InitFeed()
common.InitActionsCommonCache()
plugins.InitDigestCache()
2022-09-28 13:16:05 +00:00
go cronClearCache()
}
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:
common.ClearCache()
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-15 08:36:21 +00:00
func signalNotify() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2)
conf := config.Conf.Load()
for {
switch <-c {
case syscall.SIGUSR1:
//todo 更新配置
case syscall.SIGUSR2:
go func() {
defer func() {
if r := recover(); r != nil {
mail.SendMail([]string{conf.Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r))
}
}()
common.FlushCache()
plugins.FlushCache()
actions.FlushCache()
log.Println("清除缓存成功")
}()
}
}
}
2022-08-22 01:36:13 +00:00
func main() {
2022-11-15 08:36:21 +00:00
c := config.Conf.Load()
if c.Port == "" {
c.Port = "80"
config.Conf.Store(c)
2022-11-05 07:07:08 +00:00
}
2022-11-15 08:36:21 +00:00
go signalNotify()
err := route.SetupRouter().Run(c.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
}