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-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"
|
|
|
|
"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"
|
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
|
2023-01-28 15:12:46 +00:00
|
|
|
var address 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")
|
2023-01-28 15:12:46 +00:00
|
|
|
flag.StringVar(&address, "p", "", "listen address(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-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)
|
|
|
|
}
|
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
|
|
|
|
}
|
2022-09-23 13:46:34 +00:00
|
|
|
|
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
|
|
|
}
|
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:
|
2023-01-12 12:42:16 +00:00
|
|
|
cache.ClearCache()
|
2022-09-28 13:16:05 +00:00
|
|
|
plugins.ClearDigestCache()
|
|
|
|
}
|
|
|
|
}
|
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))
|
2022-11-16 02:17:29 +00:00
|
|
|
logs.ErrPrintln(err, "发邮件失败")
|
|
|
|
}
|
|
|
|
}()
|
2023-01-12 12:42:16 +00:00
|
|
|
cache.FlushCache()
|
2022-11-16 02:17:29 +00:00
|
|
|
plugins.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()
|
2023-02-14 11:47:47 +00:00
|
|
|
wpconfig.FlushModes()
|
2023-02-16 06:36:43 +00:00
|
|
|
theme.Reload()
|
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()
|
2023-02-05 13:06:04 +00:00
|
|
|
c := config.GetConfig()
|
2022-11-16 02:17:29 +00:00
|
|
|
middleWareReloadFn = reloadFn
|
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
|
|
|
}
|