2023-12-21 22:17:40 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-24 13:24:01 +08:00
|
|
|
"net"
|
2023-12-21 22:17:40 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"service/api/consts"
|
|
|
|
"service/app/mix/conf"
|
|
|
|
"service/app/mix/controller"
|
|
|
|
"service/app/mix/mediafiller"
|
|
|
|
"service/app/mix/service"
|
|
|
|
"service/library/apollo"
|
|
|
|
"service/library/configcenter"
|
|
|
|
"service/library/contentaudit/imageaudit"
|
|
|
|
"service/library/contentaudit/textaudit"
|
|
|
|
"service/library/httpengine"
|
|
|
|
"service/library/httpserver"
|
|
|
|
"service/library/logger"
|
|
|
|
"service/library/middleware"
|
|
|
|
"service/library/redis"
|
|
|
|
"service/library/sms"
|
|
|
|
"service/library/validator"
|
2023-12-24 13:24:01 +08:00
|
|
|
"strings"
|
2023-12-21 22:17:40 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// 加载配置
|
|
|
|
// 1.默认配置
|
2023-12-26 20:09:35 +08:00
|
|
|
configPath := consts.MainConfigPath
|
2023-12-24 12:44:04 +08:00
|
|
|
|
2023-12-24 12:33:21 +08:00
|
|
|
//if os.Getenv("PROJECT_ENV") == "production" {
|
|
|
|
// configPath = consts.ProductionConfigPath
|
|
|
|
//}
|
2023-12-21 22:17:40 +08:00
|
|
|
cfg := new(conf.ConfigSt)
|
|
|
|
err := configcenter.LoadConfig(configPath, cfg)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("LoadConfig fail, path: %v, err: %v", configPath, err)
|
|
|
|
PrintAndExit(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
//初始化apollo
|
|
|
|
err = apollo.Init(cfg.Apollo)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Apollo init fail, path: %v, err: %v", configPath, err)
|
|
|
|
PrintAndExit(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
//初始化redis
|
|
|
|
err = redis.Init(cfg.RedisConfig)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Redis init fail, path: %v, err: %v", configPath, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 初始化日志
|
|
|
|
logger.InitLogger(cfg.Log)
|
|
|
|
|
|
|
|
// 初始化短信服务
|
|
|
|
err = sms.Init(cfg.Dysmsapi)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Sms init fail, path: %v, err: %v", configPath, err)
|
|
|
|
PrintAndExit(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 初始化服务
|
|
|
|
service.DefaultService = service.NewService()
|
|
|
|
service.DefaultConfigService = service.NewConfigService()
|
|
|
|
service.DefaultCronService = service.NewCronService()
|
2024-01-03 16:16:41 +08:00
|
|
|
service.DefaultScriptsService = service.NewScriptsService()
|
2023-12-21 22:17:40 +08:00
|
|
|
err = service.DefaultService.Init(cfg)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Service init fail, err: %v", err)
|
|
|
|
PrintAndExit(msg)
|
|
|
|
}
|
2024-01-03 16:16:41 +08:00
|
|
|
service.DefaultCronService.ReadLoggerConfig(cfg.Log)
|
2023-12-21 22:17:40 +08:00
|
|
|
|
|
|
|
// 连接到图像审核任务
|
|
|
|
service.DefaultService.ConnectToImageAudit()
|
|
|
|
//service.DefaultService.Run()
|
|
|
|
|
|
|
|
// 启动图像审核服务
|
|
|
|
imageaudit.Init(cfg.ImageAudit)
|
|
|
|
|
|
|
|
// 启动文字审核服务
|
|
|
|
textaudit.Init(cfg.TextAudit)
|
|
|
|
|
|
|
|
// 初始化媒体填充服务
|
|
|
|
mediafiller.Init(cfg.ServerInfo)
|
|
|
|
|
|
|
|
// 启动定时任务
|
|
|
|
service.DefaultCronService.Run()
|
|
|
|
|
|
|
|
// 初始化http server
|
|
|
|
router := httpengine.NewRouter()
|
|
|
|
middleware.InitJwtAuthenticator(service.DefaultService.OpVerifyToken)
|
|
|
|
validator.InitDefaultNotNullValidator()
|
|
|
|
controller.Init(router)
|
|
|
|
srv := &http.Server{
|
2023-12-24 13:24:01 +08:00
|
|
|
Addr: fmt.Sprintf("%s:%d", GetIp(), cfg.App.Port),
|
2023-12-21 22:17:40 +08:00
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
httpserver.StartHttpServer(srv, cfg.DefaultConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintAndExit(msg string) {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "file %s, line %d, %s\n", file, line, msg)
|
|
|
|
time.Sleep(1) //wait logger flush
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-12-24 13:24:01 +08:00
|
|
|
|
|
|
|
func GetIp() string {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
PrintAndExit("get ip fail")
|
|
|
|
return "127.0.0.1"
|
|
|
|
}
|
|
|
|
retIp := ""
|
|
|
|
for _, address := range addrs {
|
|
|
|
// 检查ip地址判断是否回环地址
|
|
|
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
|
|
if ipnet.IP.To4() != nil {
|
|
|
|
ip := ipnet.IP.String()
|
|
|
|
if strings.HasPrefix(ip, "172.") {
|
|
|
|
retIp = ip
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retIp
|
|
|
|
}
|