-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
70 lines (62 loc) · 1.67 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
package main
import (
"github.com/arman-aminian/twitter-backend/db"
echoSwagger "github.com/swaggo/echo-swagger"
"os"
_ "github.com/arman-aminian/twitter-backend/docs" // docs is generated by Swag CLI, you have to import it.
"github.com/arman-aminian/twitter-backend/handler"
"github.com/arman-aminian/twitter-backend/model"
"github.com/arman-aminian/twitter-backend/router"
"github.com/arman-aminian/twitter-backend/store"
// echoSwagger "github.com/swaggo/echo-swagger"
"go.mongodb.org/mongo-driver/bson"
"log"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
// default Port
port = "8080"
}
r := router.New()
r.GET("/swagger/*", echoSwagger.WrapHandler)
mongoClient, err := db.GetMongoClient()
if err != nil {
log.Fatal(err)
}
usersDb := db.SetupUsersDb(mongoClient)
tweetsDb := db.SetupTweetsDb(mongoClient)
hashtagsDb := db.SetupHashtagsDb(mongoClient)
g := r.Group("")
us := store.NewUserStore(usersDb)
ts := store.NewTweetStore(tweetsDb)
hs := store.NewHashtagStore(hashtagsDb)
h := handler.NewHandler(us, ts, hs)
h.Register(g)
// Fire up the trends beforehand
err = hs.Update()
if err != nil {
log.Fatal(err)
}
// RUN THIS IF YOUR HASHTAG DATABASE IS EMPTY
// StartUpTrends(ts, h)
r.Logger.Fatal(r.Start("0.0.0.0:" + port))
}
func StartUpTrends(ts *store.TweetStore, h *handler.Handler) {
allTweets, err := ts.GetAllTweets()
if err != nil {
log.Fatal(err)
}
for _, bm := range allTweets {
var t *model.Tweet
bsonBytes, _ := bson.Marshal(bm)
_ = bson.Unmarshal(bsonBytes, &t)
hashtags := ts.ExtractHashtags(t)
for name, cnt := range hashtags {
err = h.AddHashtag(name, t, cnt)
if err != nil {
log.Fatal(err)
}
}
}
}