-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (109 loc) · 3.18 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
package main
import (
"ad/api"
"ad/database"
"fmt"
"log"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
adsUpdateDuration := 15 * time.Second
version, isEnvSet := os.LookupEnv("APP_VERSION")
if !isEnvSet {
err := godotenv.Load(".env.dev")
if err != nil {
// TODO: 改用統一的方法回傳錯誤、並提供錯誤代碼
log.Fatalf("Error loading .env file")
}
} else {
log.Printf("version: %s", version)
}
db, err := database.New()
if err != nil {
// TODO: 改用統一的方法回傳錯誤、並提供錯誤代碼
log.Fatalf("Error database.New()")
}
redisClient := database.NewRedis()
env := &api.Env{
DB: &database.MongoDB{
DB: db,
AdCollections: db.Database("dcard_ads").Collection("ads"),
CurrentAdsCollections: db.Database("dcard_ads").Collection("current_ads"),
},
Redis: &database.Redis{
R: redisClient,
ReadOnly: database.NewRedisRead(),
},
}
go env.Redis.ReplaceCountriesSet([]string{"TW", "JP", "HK", "VN"})
go autoUpdateCurrentAds(env, adsUpdateDuration)
r := gin.Default()
r.RedirectFixedPath = true
r.POST("/api/v1/ad", env.CreateAd)
r.GET("/api/v1/ad", env.GetAds)
r.Run(":80")
}
func autoUpdateCurrentAds(e *api.Env, adsUpdateDuration time.Duration) {
for {
go func() {
err := e.DB.UpdateCurrentAds()
if err != nil {
log.Fatalf("Error db.UpdateCurrentAds()")
}
ReplaceCurrentAdsSet(e)
e.Redis.UpdateAdsIntersect()
log.Println("Current Ads Updated")
}()
time.Sleep(adsUpdateDuration)
}
}
func ReplaceCurrentAdsSet(e *api.Env) {
countries := e.Redis.GetCountries()
platforms := []string{"ios", "android", "web"}
genders := []string{"M", "F"}
for _, country := range countries {
newCountrySet, err := e.DB.GetAdIDsBySingleCondition("countries", country)
if err != nil {
log.Fatalf("Error db.GetAdIDsBySingleCondition()")
}
e.Redis.ReplaceSet("ad:country:"+country, newCountrySet)
}
for _, platform := range platforms {
newPlatformSet, err := e.DB.GetAdIDsBySingleCondition("platform", platform)
if err != nil {
log.Fatalf("Error db.GetAdIDsBySingleCondition()")
}
e.Redis.ReplaceSet("ad:platform:"+platform, newPlatformSet)
}
for _, gender := range genders {
newGenderSet, err := e.DB.GetAdIDsBySingleCondition("gender", gender)
if err != nil {
log.Fatalf("Error db.GetAdIDsBySingleCondition()")
}
e.Redis.ReplaceSet("ad:gender:"+gender, newGenderSet)
}
for age := 0; age < 100; age++ {
newAgeSet, err := e.DB.GetAdIDsBySingleCondition("age", age)
if err != nil {
log.Fatalf("Error db.GetAdIDsBySingleCondition()")
}
e.Redis.ReplaceSet("ad:age:"+fmt.Sprint(age), newAgeSet)
}
for _, condition := range []string{"country", "platform", "gender", "age"} {
set, err := e.DB.GetAdIDsBySingleCondition(condition, nil)
if err != nil {
log.Fatalf("Error db.GetAdIDsBySingleCondition()")
}
e.Redis.ReplaceSet("ad:"+condition+":NotSpecified", set)
}
currentAds, err := e.DB.GetAllCurrentAds()
if err != nil {
log.Fatalf("Error db.GetAllCurrentAds()")
}
for _, condition := range []string{"country", "platform", "gender", "age"} {
e.Redis.ReplaceSet("ad:"+condition+":All", currentAds)
}
}