-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
155 lines (130 loc) · 3.43 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"devread/crawler"
"devread/db"
_ "devread/docs"
"devread/handle_log"
"devread/handler"
"devread/helper"
"devread/repository/repo_impl"
"devread/router"
"os"
"time"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
echoSwagger "github.com/swaggo/echo-swagger"
)
func init() {
if err := godotenv.Load(".env-pro"); err != nil {
return
}
}
// @title DevRead API
// @version 1.0
// @description Ứng dụng tổng hợp kiến thức cho developer
// @description Tổng hợp bài viết hay nhất trên các blog IT như viblo, toidicodedao, yellowcodebooks, thefullsnack, quan-cam, codeaholicguy,...
// @securityDefinitions.apikey jwt
// @in header
// @name Authorization
// @host devread.herokuapp.com
// @BasePath /
func main() {
// write log
log, _ := handle_log.WriteLog()
// // Dùng dưới local
// redisHost := os.Getenv("REDIS_HOST")
// redisPort := os.Getenv("REDIS_PORT")
// Dùng trên server heroku
redisUrl := os.Getenv("REDIS_URL")
// postgres details
host := os.Getenv("DB_HOST")
port := os.Getenv("DB_PORT")
password := os.Getenv("DB_PASSWORD")
username := os.Getenv("DB_USERNAME")
dbname := os.Getenv("DB_NAME")
// connect redis
client := &db.RedisDB{
// Dùng dưới local
// Host: redisHost,
// Port: redisPort,
// Dùng trên server heroku
Url: redisUrl,
Logger: log,
}
client.NewRedisDB()
// connect postgres
sql := &db.Sql{
Host: host,
Port: port,
UserName: username,
Password: password,
DbName: dbname,
Logger: log,
}
sql.Connect()
defer sql.Close()
e := echo.New()
e.GET("/swagger/*", echoSwagger.WrapHandler)
customValidator := helper.NewCustomValidator()
customValidator.RegisterValidate()
e.Validator = customValidator
userHandler := handler.UserHandler{
UserRepo: repo_impl.NewUserRepo(sql),
AuthRepo: repo_impl.NewAuthenRepo(client),
Logger: log,
}
postHandler := handler.PostHandler{
PostRepo: repo_impl.NewPostRepo(sql),
AuthRepo: repo_impl.NewAuthenRepo(client),
BookmarkRepo: repo_impl.NewBookmarkRepo(sql),
Logger: log,
}
api := router.API{
Echo: e,
UserHandler: userHandler,
PostHandler: postHandler,
}
api.SetupRouter()
// time start crawler
go crawler.VibloPost(postHandler.PostRepo)
go crawler.ToidicodedaoPost(postHandler.PostRepo)
go crawler.ThefullsnackPost(postHandler.PostRepo)
go crawler.QuancamPostV1(postHandler.PostRepo)
go crawler.CodeaholicguyPost(postHandler.PostRepo)
go crawler.YellowcodePost(postHandler.PostRepo)
// schedule crawler
go schedule(60*time.Minute, postHandler, 1)
go schedule(24*time.Hour, postHandler, 2)
go schedule(96*time.Hour, postHandler, 3)
go schedule(96*time.Hour, postHandler, 4)
go schedule(96*time.Hour, postHandler, 5)
go schedule(96*time.Hour, postHandler, 6)
e.Logger.Fatal(e.Start(":" + os.Getenv("PORT")))
}
func schedule(timeSchedule time.Duration, handler handler.PostHandler, crowIlnndex int) {
ticker := time.NewTicker(timeSchedule)
func() {
for {
switch crowIlnndex {
case 1:
<-ticker.C
crawler.VibloPost(handler.PostRepo)
case 2:
<-ticker.C
crawler.ToidicodedaoPost(handler.PostRepo)
case 3:
<-ticker.C
crawler.ThefullsnackPost(handler.PostRepo)
case 4:
<-ticker.C
crawler.QuancamPostV1(handler.PostRepo)
case 5:
<-ticker.C
crawler.CodeaholicguyPost(handler.PostRepo)
case 6:
<-ticker.C
crawler.YellowcodePost(handler.PostRepo)
}
}
}()
}