-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (103 loc) · 3.69 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/Zli-UoA/ryouomoi-checker-backend/controller"
"github.com/Zli-UoA/ryouomoi-checker-backend/repository"
"github.com/Zli-UoA/ryouomoi-checker-backend/service"
"github.com/Zli-UoA/ryouomoi-checker-backend/usecase"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Println(err)
}
}
func connectDB() (*sqlx.DB, error) {
dbUser := os.Getenv("DB_USER")
dbPassword := os.Getenv("DB_PASSWORD")
dbAddress := os.Getenv("DB_ADDRESS")
dbPort := os.Getenv("DB_PORT")
dbName := os.Getenv("DB_NAME")
dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=true&loc=UTC", dbUser, dbPassword, dbAddress, dbPort, dbName)
var err error
for i := 0; i < 10; i++ {
db, err := sqlx.Connect("mysql", dsn)
if err == nil {
return db, nil
}
time.Sleep(100 * time.Millisecond)
}
return nil, err
}
func main() {
apiKey := os.Getenv("API_KEY")
apiKeySecret := os.Getenv("API_KEY_SECRET")
callbackUrl := os.Getenv("CALLBACK_URL")
botApiKey := os.Getenv("BOT_API_KEY")
botApiKeySecret := os.Getenv("BOT_API_KEY_SECRET")
botCallbackUrl := os.Getenv("BOT_CALLBACK_URL")
botUserID, _ := strconv.ParseInt(os.Getenv("BOT_USER_ID"), 10, 64)
frontRedirectUrl := os.Getenv("FRONT_REDIRECT_URL")
jwtSecret := os.Getenv("JWT_SECRET")
db, err := connectDB()
if err != nil {
log.Fatal(err)
}
ur := repository.NewUserRepository(db)
cr := repository.NewChatRepository(db)
ts := service.NewTwitterService(apiKey, apiKeySecret, callbackUrl)
bts := service.NewTwitterService(botApiKey, botApiKeySecret, botCallbackUrl)
ujs := service.NewUserJWTService(jwtSecret)
gtluu := usecase.NewGetTwitterLoginUrlUseCase(ts)
htcu := usecase.NewHandleTwitterCallbackUseCase(ts, ujs, ur)
fsu := usecase.NewFriendsSearchUseCase(ts, ur)
slpu := usecase.NewSetLovePointUseCase(6, botUserID, ur, cr, bts)
gfu := usecase.NewGetFolloweesUseCase(ts, ur)
fru := usecase.NewGetFollowersUseCase(ts, ur)
glpu := usecase.NewGetLovePointUsecase(ur)
dlpu := usecase.NewDeleteLovePointUseCase(ur)
glptsu := usecase.NewGetLovePointsUseCase(ur, ts)
gclu := usecase.NewGetCurrentLover(ur)
gcedu := usecase.NewGetCoupleElapsedDaysUseCase(ur)
dclu := usecase.NewDeleteCurrentLover(botUserID, ur, bts)
gliuu := usecase.NewGetLoggedInUserUseCase(ur, ts)
tc := controller.NewTwitterController(frontRedirectUrl, gtluu, htcu)
btc := controller.NewTwitterController(
"https://example.com/",
usecase.NewGetTwitterLoginUrlUseCase(bts),
usecase.NewHandleTwitterCallbackUseCase(bts, ujs, ur),
)
fc := controller.NewFriendsController(ujs, fsu, slpu, gfu, fru)
mc := controller.NewMeController(ujs, glpu, dlpu, glptsu, gclu, gcedu, dclu, gliuu)
r := gin.Default()
corsConfig := cors.DefaultConfig()
corsConfig.AddAllowHeaders("Authorization", "Accept")
corsConfig.AllowOrigins = []string{"*"}
corsConfig.ExposeHeaders = []string{"Link"}
r.Use(cors.New(corsConfig))
r.GET("/twitter/login", tc.GetTwitterLoginUrl)
r.GET("/twitter/callback", tc.HandleTwitterCallback)
r.GET("/bot/login", btc.GetTwitterLoginUrl)
r.GET("/bot/callback", btc.HandleTwitterCallback)
r.GET("/friends/search", fc.FriendsSearch)
r.GET("/friends/follower", fc.GetFollowers)
r.GET("/friends/followee", fc.GetFollowees)
r.POST("/friends/:id", fc.SetLovePoint)
r.GET("/me", mc.GetLoggedInUser)
r.GET("/me/lovers", mc.GetLovePoints)
r.GET("/me/lovers/:id", mc.GetLovePoint)
r.DELETE("/me/lovers/:id", mc.DeleteLovePoint)
r.GET("/me/lover", mc.GetCurrentLover)
r.GET("/me/lover/days", mc.GetCoupleElapsedDays)
r.DELETE("/me/lover", mc.DeleteCurrentLover)
r.Run(":8080")
}