wp-go/app/cmd/main.go

112 lines
2.2 KiB
Go
Raw Permalink 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"
2024-06-19 08:10:42 +00:00
"github.com/fthvgb1/wp-go/app/ossigns"
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/db"
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/plugins"
"github.com/fthvgb1/wp-go/app/plugins/wphandle"
2024-01-21 13:29:36 +00:00
"github.com/fthvgb1/wp-go/app/route"
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/theme"
"github.com/fthvgb1/wp-go/app/wpconfig"
2023-11-07 07:18:34 +00:00
"github.com/fthvgb1/wp-go/cache/cachemanager"
2023-01-18 15:04:12 +00:00
"github.com/fthvgb1/wp-go/model"
2022-11-15 08:36:21 +00:00
"os"
2022-11-16 07:31:37 +00:00
"regexp"
"strings"
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
2023-11-13 14:53:20 +00:00
func inits() {
2023-05-31 15:12:16 +00:00
flag.StringVar(&confPath, "c", "config.yaml", "config file support json,yaml or url")
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
func main() {
2023-08-26 14:01:20 +00:00
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
os.Exit(1)
}
}()
2023-11-13 14:53:20 +00:00
inits()
2024-06-19 08:10:42 +00:00
ossigns.SetConfPath(confPath)
go ossigns.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
}