service/app/mix/cmd/main.go

142 lines
3.5 KiB
Go
Raw Normal View History

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"
2024-05-23 11:34:24 +08:00
"service/app/mix/mediafiller"
2023-12-21 22:17:40 +08:00
"service/app/mix/service"
"service/library/apollo"
"service/library/configcenter"
2024-02-29 09:55:56 +08:00
"service/library/dingtalk"
2023-12-21 22:17:40 +08:00
"service/library/httpengine"
2024-05-17 16:13:50 +08:00
"service/library/httpserver"
2023-12-21 22:17:40 +08:00
"service/library/logger"
"service/library/middleware"
"service/library/redis"
2024-05-14 09:48:34 +08:00
"service/library/servicediscovery"
2023-12-21 22:17:40 +08:00
"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() {
2024-05-16 22:10:14 +08:00
defer func() {
logger.Recover()
}()
2023-12-21 22:17:40 +08:00
// 加载配置
// 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)
}
2024-05-14 09:48:34 +08:00
// 初始化日志
logger.InitLogger(cfg.Log)
2023-12-21 22:17:40 +08:00
//初始化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 {
2024-05-16 21:59:45 +08:00
msg := fmt.Sprintf("Redis init fail, path: %v, err: %v", configPath, err)
PrintAndExit(msg)
2023-12-21 22:17:40 +08:00
}
2024-05-14 09:48:34 +08:00
// 初始化服务发现
err = servicediscovery.Init(cfg.SD)
if err != nil {
msg := fmt.Sprintf("Init servicediscovery fail, path: %v, err: %v", configPath, err)
PrintAndExit(msg)
}
2023-12-21 22:17:40 +08:00
// 初始化短信服务
err = sms.Init(cfg.Dysmsapi)
if err != nil {
msg := fmt.Sprintf("Sms init fail, path: %v, err: %v", configPath, err)
PrintAndExit(msg)
}
2024-02-29 09:55:56 +08:00
// 初始化钉钉机器人
dingtalk.InitDefaultDingTalkClient(cfg.DingTalkRobot)
2023-12-21 22:17:40 +08:00
// 初始化服务
service.DefaultService = service.NewService()
service.DefaultConfigService = service.NewConfigService()
service.DefaultScriptsService = service.NewScriptsService()
2024-01-20 06:33:10 +08:00
service.DefaultImageAuditTaskResultHandler = service.NewImageAuditTaskResultHandler()
2024-01-20 18:48:38 +08:00
service.DefaultTextAuditTaskResultHandler = service.NewTextAuditTaskResultHandler()
2024-04-26 10:46:37 +08:00
service.DefaultVideoModerationTaskResultHandler = service.NewVideoModerationTaskResultHandler()
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-05-23 11:34:24 +08:00
// 初始化媒体填充服务
mediafiller.Init(cfg.ServerInfo)
2023-12-21 22:17:40 +08:00
// 初始化http server
2024-05-14 09:48:34 +08:00
ip := GetIp()
port := cfg.App.Port
2023-12-21 22:17:40 +08:00
router := httpengine.NewRouter()
middleware.InitJwtAuthenticator(service.DefaultService.OpVerifyToken)
validator.InitDefaultNotNullValidator()
controller.Init(router)
srv := &http.Server{
2024-05-14 09:48:34 +08:00
Addr: fmt.Sprintf("%s:%d", ip, port),
2023-12-21 22:17:40 +08:00
Handler: router,
}
2024-05-14 09:48:34 +08:00
2024-05-17 16:13:50 +08:00
httpserver.StartHttpServer(srv, cfg.DefaultConfig, ip, port)
2023-12-21 22:17:40 +08:00
}
func PrintAndExit(msg string) {
_, file, line, _ := runtime.Caller(1)
2024-05-14 09:48:34 +08:00
errorMsg := fmt.Sprintf("file %s, line %d, %s\n", file, line, msg)
_, _ = fmt.Fprintf(os.Stderr, errorMsg)
logger.Fatal(errorMsg)
2023-12-21 22:17:40 +08:00
time.Sleep(1) //wait logger flush
2024-05-14 09:48:34 +08:00
//os.Exit(1)
2023-12-21 22:17:40 +08:00
}
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
}