-
Notifications
You must be signed in to change notification settings - Fork 79
/
main.go
115 lines (93 loc) · 2.54 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
package main
import (
"bytes"
"embed"
"errors"
"github.com/julienschmidt/httprouter"
"github.com/spf13/viper"
"go_movies/config"
"go_movies/routes"
"go_movies/utils"
"go_movies/utils/spider"
"io/fs"
"log"
"net/http"
"os"
)
//go:embed static2/*
var embedStatic2 embed.FS
// Reads from the routes slice to translate the values to httprouter.Handle
// 遍历路由
func traversingRouter() *httprouter.Router {
AllRoutes := routes.AllRoutes()
router := httprouter.New()
for _, route := range AllRoutes {
var handle httprouter.Handle
handle = route.HandlerFunc
log.Println(route.Path)
router.Handle(route.Method, route.Path, handle)
}
if viper.GetString(`app.debug_mod`) == "false" {
// live 模式 打包用
fsys, _ := fs.Sub(embedStatic2, "static2")
router.ServeFiles("/static2/*filepath", http.FS(fsys))
} else {
// dev 开发用 避免修改静态资源需要重启服务
router.ServeFiles("/static2/*filepath", http.Dir("static2"))
}
return router
}
// 初始化配置文件
func init() {
viper.SetConfigType("json") // 设置配置文件的类型
readConfig := errors.New("未定义配置文件")
if _, err := os.Stat("./app.json"); os.IsNotExist(err) {
readConfig = viper.ReadConfig(bytes.NewBuffer(config.AppJsonConfig))
} else {
viper.SetConfigName("app")
viper.AddConfigPath(".")
readConfig = viper.ReadInConfig()
}
if err := readConfig; err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
log.Println("no such config file")
} else {
// Config file was found but another error was produced
log.Println("read config error")
}
log.Fatal(err) // 读取配置文件失败致命错误
}
}
// 首次启动自动开启爬虫
func firstSpider() {
hasHome := utils.RedisDB.Exists("detail_links:id:1").Val()
log.Println("hasHome", hasHome)
// 不存在首页的key 则认为是第一次启动
if hasHome == 0 {
spider.Create().Start()
}
}
func main() {
// 注册所有路由
router := traversingRouter()
// 初始化 redis 连接
utils.InitRedisDB()
defer utils.CloseRedisDB()
port := viper.GetString(`app.port`)
mod := viper.GetString(`app.spider_mod`)
log.Println("监听端口", "http://127.0.0.1"+port)
log.Println("spider_mod:" + mod)
firstSpider()
// 启动定时爬虫任务 全量
utils.TimingSpider(func() {
spider.Create().Start()
return
})
// 爬虫 只爬取最近有更新的资源
utils.RecentUpdate(func() {
spider.Create().DoRecentUpdate()
return
})
log.Println(http.ListenAndServe(port, router))
}