service/app/mix/cmd/main.go

150 lines
3.8 KiB
Go

package main
import (
"fmt"
"net"
"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"
videomoderation "service/library/contentaudit/video_moderation"
"service/library/dingtalk"
"service/library/httpengine"
"service/library/httpserver"
"service/library/logger"
"service/library/middleware"
"service/library/redis"
"service/library/sms"
"service/library/validator"
"strings"
"time"
)
func main() {
// 加载配置
// 1.默认配置
configPath := consts.ProductionConfigPath
//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
}
// 初始化日志
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()
err = service.DefaultService.Init(cfg)
if err != nil {
msg := fmt.Sprintf("Service init fail, err: %v", err)
PrintAndExit(msg)
}
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()
//service.DefaultService.Run()
// 启动图像审核服务
imageaudit.Init(cfg.ImageAudit)
// 启动文字审核服务
textaudit.Init(cfg.TextAudit)
// 启动视频审核服务
videomoderation.Init(cfg.VideoModeration)
// 初始化媒体填充服务
mediafiller.Init(cfg.ServerInfo)
// 初始化http server
router := httpengine.NewRouter()
middleware.InitJwtAuthenticator(service.DefaultService.OpVerifyToken)
validator.InitDefaultNotNullValidator()
controller.Init(router)
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", GetIp(), cfg.App.Port),
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)
}
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
}