service/app/mix/cmd_offline/main.go

157 lines
4.1 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"net"
2024-05-23 22:39:37 +08:00
"net/http"
"os"
"runtime"
"service/api/consts"
"service/app/mix/conf"
2024-05-23 22:39:37 +08:00
"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"
videomoderation "service/library/contentaudit/video_moderation"
"service/library/dingtalk"
2024-05-23 22:39:37 +08:00
"service/library/httpengine"
"service/library/httpserver"
"service/library/logger"
2024-05-24 03:52:57 +08:00
"service/library/middleware"
"service/library/redis"
"service/library/sms"
2024-05-23 22:39:37 +08:00
"service/library/validator"
"strings"
"time"
)
func main() {
// 加载配置
// 1.默认配置
2024-05-24 03:56:36 +08:00
configPath := consts.MainOfflineConfigPath
//if os.Getenv("PROJECT_ENV") == "production" {
// configPath = consts.ProductionConfigPath
//}
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
}
// 初始化日志
2024-05-24 03:52:57 +08:00
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)
}
// 初始化钉钉机器人
dingtalk.InitDefaultDingTalkClient(cfg.DingTalkRobot)
// 初始化服务
service.DefaultService = service.NewService()
service.DefaultConfigService = service.NewConfigService()
service.DefaultCronService = service.NewCronService()
service.DefaultScriptsService = service.NewScriptsService()
service.DefaultImageAuditTaskResultHandler = service.NewImageAuditTaskResultHandler()
service.DefaultTextAuditTaskResultHandler = service.NewTextAuditTaskResultHandler()
service.DefaultVideoModerationTaskResultHandler = service.NewVideoModerationTaskResultHandler()
2024-05-30 14:53:38 +08:00
service.DefaultStreamerRecommService = service.NewStreamerRecommService()
err = service.DefaultService.Init(cfg)
if err != nil {
msg := fmt.Sprintf("Service init fail, err: %v", err)
PrintAndExit(msg)
}
exec, err := service.DefaultCronService.Init(cfg)
if err != nil {
msg := fmt.Sprintf("CronService init fail, err: %v", err)
PrintAndExit(msg)
}
// 连接到审核任务数据库接口
service.DefaultService.ConnectToImageAudit()
service.DefaultService.ConnectToTextAudit()
service.DefaultService.ConnectToVideoModeration()
2024-05-30 14:53:38 +08:00
// 连接到推荐服务
service.DefaultService.ConnectToStreamerRecommService(service.DefaultStreamerRecommService)
// 启动图像审核服务
imageaudit.Init(cfg.ImageAudit)
// 启动文字审核服务
textaudit.Init(cfg.TextAudit)
// 启动视频审核服务
videomoderation.Init(cfg.VideoModeration)
// 初始化媒体填充服务
mediafiller.Init(cfg.ServerInfo)
2024-05-23 22:39:37 +08:00
go exec.Run()
2024-05-24 03:52:57 +08:00
// 初始化http server
2024-05-23 22:47:33 +08:00
ip := GetIp()
port := cfg.App.Port
2024-05-23 22:39:37 +08:00
router := httpengine.NewRouter()
2024-05-24 03:52:57 +08:00
middleware.InitJwtAuthenticator(service.DefaultService.OpVerifyToken)
2024-05-23 22:39:37 +08:00
validator.InitDefaultNotNullValidator()
2024-05-24 08:59:21 +08:00
controller.InitOffline(router)
2024-05-23 22:39:37 +08:00
srv := &http.Server{
2024-05-23 22:47:33 +08:00
Addr: fmt.Sprintf("%s:%d", ip, port),
2024-05-23 22:39:37 +08:00
Handler: router,
}
2024-05-24 04:30:34 +08:00
//httpserver.StartHttpServer(srv, cfg.DefaultConfig)
httpserver.StartOfflineHttpServer(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)
}
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
}