forked from sec-an/TVHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (115 loc) · 3.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"TVHelper/global"
"TVHelper/internal/douban"
"TVHelper/internal/parser"
"TVHelper/internal/routers"
"TVHelper/internal/vod"
"TVHelper/pkg/logging"
"TVHelper/pkg/mysql"
"TVHelper/pkg/redis"
"TVHelper/pkg/setting"
"context"
"flag"
"log"
"net/http"
"os"
"time"
"go.uber.org/zap"
"github.com/gin-gonic/gin"
)
var dir string
func init() {
// 获取当前路径
currDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
flag.StringVar(&dir, "d", currDir, "TVHelper根目录")
flag.Parse()
// 切换至TVHelper根目录
err = os.Chdir(dir)
if err != nil {
log.Fatal(err)
}
// 配置初始化
err = setupSetting()
if err != nil {
log.Fatalf("init.setupSetting err: %v", err)
}
// zap初始化
logging.Init()
// redis初始化
if global.MysqlSetting.Running {
global.Mysql = mysql.InitDB()
}
if global.RedisSetting.Running {
global.RedisClient = redis.Init()
// 每次运行先清空缓存
ctx := context.Background()
res, err := global.RedisClient.FlushDB(ctx).Result()
if err != nil {
global.Logger.Panic("Failed to flush redis", zap.Error(err))
}
global.Logger.Info("FlushDB: " + res)
}
// 豆瓣、配置解析客户端初始化
global.DouBanClient = douban.NewReqClient(global.SpiderSetting.DouBanClientTimeout)
global.AListClient = vod.NewReqClient(global.SpiderSetting.AListClientTimeout)
global.ParserClient = parser.NewReqClient(global.SpiderSetting.ParserClientTimeout)
}
func main() {
global.Logger.Info("TVHelper starting...",
zap.String("port", global.ServerSetting.HttpPort),
zap.String("dir", dir))
gin.SetMode(global.ServerSetting.RunMode)
router := routers.NewRouter()
s := &http.Server{
Addr: ":" + global.ServerSetting.HttpPort,
Handler: router,
ReadTimeout: global.ServerSetting.ReadTimeout,
WriteTimeout: global.ServerSetting.WriteTimeout,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
global.Logger.Fatal("startup service failed...", zap.Error(err))
}
}
func setupSetting() error {
newSetting, err := setting.NewSetting()
if err != nil {
return err
}
err = newSetting.ReadSection("Server", &global.ServerSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("Log", &global.LogSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("Mysql", &global.MysqlSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("Redis", &global.RedisSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("Spider", &global.SpiderSetting)
if err != nil {
return err
}
err = newSetting.ReadSection("AList", &global.AListSetting)
if err != nil {
return err
}
global.ServerSetting.ReadTimeout *= time.Second
global.ServerSetting.WriteTimeout *= time.Second
global.RedisSetting.IdleTimeout *= time.Second
global.RedisSetting.SubCacheTime *= time.Minute
global.SpiderSetting.DouBanClientTimeout *= time.Millisecond
global.SpiderSetting.AListClientTimeout *= time.Millisecond
global.SpiderSetting.ParserClientTimeout *= time.Millisecond
return nil
}