service/app/mix/cmd/main.go

153 lines
3.8 KiB
Go

package main
import (
"fmt"
"math/rand"
"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/dingtalk"
"service/library/httpengine"
"service/library/httpserver"
"service/library/logger"
"service/library/middleware"
"service/library/redis"
"service/library/servicediscovery"
"service/library/sms"
"service/library/validator"
"strings"
"time"
)
func main() {
defer func() {
logger.Recover()
}()
rand.Seed(time.Now().UnixNano())
// 加载配置
// 1.默认配置
configPath := consts.MainConfigPath
//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)
}
// 初始化日志
logger.InitLogger(cfg.Log)
//初始化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 {
msg := fmt.Sprintf("Redis init fail, path: %v, err: %v", configPath, err)
PrintAndExit(msg)
}
// 初始化服务发现
err = servicediscovery.Init(cfg.SD)
if err != nil {
msg := fmt.Sprintf("Init servicediscovery fail, path: %v, err: %v", configPath, err)
PrintAndExit(msg)
}
// 初始化短信服务
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.DefaultScriptsService = service.NewScriptsService()
service.DefaultImageAuditTaskResultHandler = service.NewImageAuditTaskResultHandler()
service.DefaultTextAuditTaskResultHandler = service.NewTextAuditTaskResultHandler()
service.DefaultVideoModerationTaskResultHandler = service.NewVideoModerationTaskResultHandler()
service.DefaultEsbService = service.NewEsbService()
service.DefaultHvyogoService = service.NewHvyogoService(cfg.Hvyogo)
service.DefaultZthyService = service.NewZthyService(cfg.Zthy)
err = service.DefaultService.Init(cfg)
if err != nil {
msg := fmt.Sprintf("Service init fail, err: %v", err)
PrintAndExit(msg)
}
err = service.DefaultEsbService.Init()
if err != nil {
msg := fmt.Sprintf("EsbService init fail, err: %v", err)
PrintAndExit(msg)
}
// 初始化媒体填充服务
mediafiller.Init(cfg.ServerInfo)
// 初始化http server
ip := GetIp()
port := cfg.App.Port
router := httpengine.NewRouter()
middleware.InitJwtAuthenticator(service.DefaultService.OpVerifyToken)
validator.InitDefaultNotNullValidator()
controller.Init(router)
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", ip, port),
Handler: router,
}
httpserver.StartHttpServer(srv, cfg.DefaultConfig, ip, port)
}
func PrintAndExit(msg string) {
_, file, line, _ := runtime.Caller(1)
errorMsg := fmt.Sprintf("file %s, line %d, %s\n", file, line, msg)
_, _ = fmt.Fprintf(os.Stderr, errorMsg)
logger.Fatal(errorMsg)
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
}