wp-go/internal/cmd/main.go

154 lines
3.2 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"
"github.com/fthvgb1/wp-go/internal/cmd/cachemanager"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
2023-01-18 15:04:12 +00:00
"github.com/fthvgb1/wp-go/internal/cmd/route"
"github.com/fthvgb1/wp-go/internal/mail"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/db"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/plugins"
2023-05-04 11:46:06 +00:00
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
2023-01-18 15:04:12 +00:00
"github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model"
2022-11-15 08:36:21 +00:00
"log"
"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
2023-01-28 15:12:46 +00:00
var address string
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")
2023-04-25 12:58:22 +00:00
flag.StringVar(&address, "p", "", "listen address and port")
2022-11-05 07:07:08 +00:00
flag.Parse()
2023-01-28 15:12:46 +00:00
if address == "" && os.Getenv("PORT") == "" {
address = "80"
2022-11-16 07:31:37 +00:00
}
2023-01-28 15:12:46 +00:00
if intReg.MatchString(address) && !strings.Contains(address, ":") {
address = ":" + address
2022-11-16 07:31:37 +00:00
}
2022-11-16 02:17:29 +00:00
err := initConf(confPath)
2022-08-27 13:21:05 +00:00
if err != nil {
panic(err)
}
2023-01-12 12:42:16 +00:00
cache.InitActionsCommonCache()
2022-11-16 02:17:29 +00:00
plugins.InitDigestCache()
2023-02-14 11:47:47 +00:00
theme.InitTheme()
2022-11-16 02:17:29 +00:00
go cronClearCache()
}
func initConf(c string) (err error) {
err = config.InitConfig(c)
if err != nil {
return
}
2023-04-23 14:56:02 +00:00
err = config.InitTrans()
if err != nil {
return err
}
2023-04-07 14:59:07 +00:00
err = logs.InitLogger()
if err != nil {
return err
}
2023-02-06 09:58:24 +00:00
database, err := db.InitDb()
2022-08-27 13:21:05 +00:00
if err != nil {
2022-11-16 02:17:29 +00:00
return
2022-08-27 13:21:05 +00:00
}
2023-02-10 13:23:30 +00:00
model.InitDB(db.QueryDb(database))
2023-01-12 12:42:16 +00:00
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
}
2023-05-04 11:46:06 +00:00
wphandle.LoadPlugins()
2022-11-16 02:17:29 +00:00
return
2022-09-28 13:16:05 +00:00
}
func cronClearCache() {
2023-02-05 13:06:04 +00:00
t := time.NewTicker(config.GetConfig().CacheTime.CrontabClearCacheTime)
2022-09-28 13:16:05 +00:00
for {
select {
case <-t.C:
cachemanager.ClearExpired()
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 {
2023-02-05 13:06:04 +00:00
err := mail.SendMail([]string{config.GetConfig().Mail.User}, "清空缓存失败", fmt.Sprintf("err:[%s]", r))
2023-04-07 14:59:07 +00:00
logs.IfError(err, "发邮件失败")
2022-11-16 02:17:29 +00:00
}
}()
cachemanager.Flush()
2022-11-16 02:51:39 +00:00
log.Println("all cache flushed")
2022-11-16 02:17:29 +00:00
}
func reloads() {
2022-11-16 02:17:29 +00:00
defer func() {
if r := recover(); r != nil {
log.Println(r)
}
}()
err := config.InitConfig(confPath)
2023-04-07 14:59:07 +00:00
logs.IfError(err, "获取配置文件失败", confPath)
err = logs.InitLogger()
logs.IfError(err, "日志配置错误")
2023-04-05 12:54:38 +00:00
_, err = db.InitDb()
2023-04-07 14:59:07 +00:00
logs.IfError(err, "重新读取db失败", config.GetConfig().Mysql)
2023-01-12 12:42:16 +00:00
err = wpconfig.InitOptions()
2023-04-07 14:59:07 +00:00
logs.IfError(err, "获取网站设置WpOption失败")
2023-01-12 12:42:16 +00:00
err = wpconfig.InitTerms()
2023-04-07 14:59:07 +00:00
logs.IfError(err, "获取WpTerms表失败")
2023-05-04 11:46:06 +00:00
wphandle.LoadPlugins()
reload.Reload()
2022-11-16 02:51:39 +00:00
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:
go reloads()
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()
Gin := route.SetupRouter()
2023-02-05 13:06:04 +00:00
c := config.GetConfig()
2023-01-28 15:12:46 +00:00
if c.Ssl.Key != "" && c.Ssl.Cert != "" {
err := Gin.RunTLS(address, c.Ssl.Cert, c.Ssl.Key)
if err != nil {
panic(err)
}
return
}
err := Gin.Run(address)
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
}