Delete the dataReport SDK, event storage model, and scheduled uploader, along with the reportser/srv_report wrapper services and all ~20 Report* call sites in business code. Also drop the ReportSDKConfig field from the APP config response, the TrackerAppId helper, and the related dataReport yaml config block. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
210 lines
4.6 KiB
Go
210 lines
4.6 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
"videoweb/app/appg"
|
|
"videoweb/app/router"
|
|
"videoweb/app/service/appser"
|
|
"videoweb/app/service/configser"
|
|
"videoweb/app/service/playletcache"
|
|
"videoweb/app/service/translationser"
|
|
ipstatsync "videoweb/app/skd/ipstat"
|
|
"videoweb/app/skd/seo"
|
|
"videoweb/pub/common/ipaddr"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/srvversion"
|
|
"videoweb/pub/common/tgbot"
|
|
"videoweb/pub/esmanager"
|
|
"videoweb/pub/models"
|
|
"videoweb/pub/mongos"
|
|
"videoweb/pub/payCenter"
|
|
"videoweb/pub/redis"
|
|
"videoweb/pub/storage/proxy"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
func init() {
|
|
time.Local, _ = time.LoadLocation("Asia/Shanghai")
|
|
}
|
|
func Run(args []string) {
|
|
showVersion()
|
|
if err := initConfig(); err != nil {
|
|
log.Error("init config failed", log.E(err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
if err := initMongoDB(); err != nil {
|
|
log.Error("init mongodb failed", log.E(err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
//初始化tg机器人
|
|
if appg.Conf.TgToken != "" {
|
|
// tgbot.InitTgBot(appg.Conf.TgToken, "[汁乎App]")
|
|
//tgbot.SendToProgram("Test", "Test")
|
|
}
|
|
detectorV6, err := ipaddr.NewXDBDetectorV6("conf/ip2region_v6.xdb")
|
|
if err != nil {
|
|
fmt.Printf("创建检测器失败: %v\n", err)
|
|
fmt.Println("正在尝试下载数据库文件...")
|
|
}
|
|
appg.DetectorV6 = detectorV6
|
|
|
|
detectorV4, err := ipaddr.NewXDBDetectorV4("conf/ip2region_v4.xdb")
|
|
if err != nil {
|
|
fmt.Printf("创建检测器失败: %v\n", err)
|
|
fmt.Println("正在尝试下载数据库文件...")
|
|
}
|
|
appg.DetectorV4 = detectorV4
|
|
proxy.InitBucketVideoAddress(appg.Conf.BucketVideoAddressV2)
|
|
mongos.StartLid()
|
|
initRedis()
|
|
if err := initElasticSearch(); err != nil {
|
|
log.Error("init ES failed", log.E(err))
|
|
os.Exit(1)
|
|
}
|
|
initData()
|
|
ipstatsync.StartSync()
|
|
|
|
initHttpServer()
|
|
//tgbot.SendToProgram("启动", "app启动完毕")
|
|
shutdownHandler()
|
|
}
|
|
|
|
func initConfig() error {
|
|
|
|
buf, err := ioutil.ReadFile("conf/app.yaml")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = yaml.Unmarshal(buf, &appg.Conf); err != nil {
|
|
return err
|
|
}
|
|
payCenter.Init(appg.Conf.NewPayCenter)
|
|
// nameBuf, err := ioutil.ReadFile("conf/names.yaml")
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// if err = yaml.Unmarshal(nameBuf, &appg.Name); err != nil {
|
|
// return err
|
|
// }
|
|
|
|
// pictureBuf, err := ioutil.ReadFile("conf/picture.yaml")
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
|
|
// if err = yaml.Unmarshal(pictureBuf, &appg.Picture); err != nil {
|
|
// return err
|
|
// }
|
|
|
|
return nil
|
|
}
|
|
|
|
func initMongoDB() error {
|
|
shopDb := mongos.NewManager(appg.Conf.Mongodb.Addr)
|
|
if err := shopDb.Init(); err != nil {
|
|
return err
|
|
}
|
|
mongos.Shop = shopDb
|
|
|
|
ycShopDb := mongos.NewManager(appg.Conf.Mongodb.LogAddr)
|
|
if err := ycShopDb.Init(); err != nil {
|
|
return err
|
|
}
|
|
mongos.ShopLog = ycShopDb
|
|
return nil
|
|
}
|
|
|
|
func initElasticSearch() error {
|
|
es, err := esmanager.NewManager(appg.Conf.Es.Addr,
|
|
appg.Conf.Es.User, appg.Conf.Es.Pass)
|
|
if err != nil {
|
|
log.Info("new es failed", log.Any("err", err))
|
|
return err
|
|
}
|
|
if err = es.ClientConnect(); err != nil {
|
|
log.Info("connect es failed", log.Any("err", err))
|
|
return err
|
|
}
|
|
esmanager.Instance = es
|
|
log.Info("es:Init Ok!")
|
|
return nil
|
|
}
|
|
|
|
// 数据
|
|
func initData() {
|
|
models.Init()
|
|
// service.Start()
|
|
appg.InitCache()
|
|
translationser.Start()
|
|
configser.Start()
|
|
appser.Start()
|
|
playletcache.Start()
|
|
seo.Start()
|
|
}
|
|
|
|
func initRedis() error {
|
|
if err := redis.Init(redis.Options{
|
|
Addr: appg.Conf.Redis.Addr,
|
|
Psw: appg.Conf.Redis.Passwd,
|
|
Db: appg.Conf.Redis.Db,
|
|
}); err != nil {
|
|
log.Error("redis connect err", log.E(err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func showVersion() {
|
|
log.Info(fmt.Sprintf("sevice version:%s", srvversion.SERVICE_VERSION))
|
|
log.Info(fmt.Sprintf("git version:%s", srvversion.GIT_VERSION))
|
|
log.Info(fmt.Sprintf("build date:%s", srvversion.BUILD_DATE))
|
|
log.Info(fmt.Sprintf("build ip:%s", srvversion.BUILD_IP))
|
|
}
|
|
|
|
func initHttpServer() {
|
|
addr := fmt.Sprintf(":%s", appg.Conf.ListenAddr)
|
|
srv := &http.Server{
|
|
Addr: addr,
|
|
Handler: router.Init(),
|
|
}
|
|
go func() {
|
|
// service connections
|
|
log.Info("服务已启动", log.Any("地址", addr))
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatal("listen:", log.E(err))
|
|
}
|
|
}()
|
|
}
|
|
|
|
func shutdownHandler() {
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, os.Kill)
|
|
<-quit
|
|
log.Warn("Shutdown... server")
|
|
|
|
done := make(chan int)
|
|
go func() {
|
|
mongos.Shop.DisConnect()
|
|
mongos.ShopLog.DisConnect()
|
|
redis.Disconnect()
|
|
configser.Stop()
|
|
tgbot.ReleaseTgBot()
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-time.After(5 * time.Second):
|
|
log.Warn("shutdown timeout....")
|
|
case <-done:
|
|
log.Info("graceful shutdown")
|
|
}
|
|
}
|