service/app/mix/cmd/main.go

106 lines
2.5 KiB
Go

package main
import (
"fmt"
"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"
"time"
)
func main() {
// 加载配置
// 1.默认配置
configPath := consts.MainConfigPath
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()
err = service.DefaultService.Init(cfg)
if err != nil {
msg := fmt.Sprintf("Service init fail, err: %v", err)
PrintAndExit(msg)
}
// 连接到图像审核任务
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{
Addr: fmt.Sprintf("%s:%d", cfg.App.Ip, 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)
}