-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (83 loc) · 2.77 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
package main
import (
"encoding/base64"
"os"
"strconv"
"time"
"github.com/gorilla/sessions"
"github.com/joho/godotenv"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
echomiddleware "github.com/labstack/echo/v4/middleware"
"github.com/zzokki81/uas/handler"
"github.com/zzokki81/uas/interactor"
"github.com/zzokki81/uas/middleware"
"github.com/zzokki81/uas/service/oauth"
"github.com/zzokki81/uas/store/postgres"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"gopkg.in/tylerb/graceful.v1"
)
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
port, err := strconv.Atoi(os.Getenv("DB_PORT"))
if err != nil {
panic(err)
}
config := postgres.PostgresConfig{
Host: os.Getenv("DB_HOST"),
Port: port,
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
Name: os.Getenv("DB_NAME"),
}
store, err := postgres.Open(config)
if err != nil {
panic(err)
}
defer store.Close()
sessionAuthKey := os.Getenv("SESSION_AUTH_KEY")
sessionEncryptionKey := os.Getenv("SESSION_ENCRYPTION_KEY")
rawSessionAuthKey, err := base64.StdEncoding.DecodeString(sessionAuthKey)
if err != nil {
panic(err)
}
rawSessionEncryptionKey, err := base64.StdEncoding.DecodeString(sessionEncryptionKey)
if err != nil {
panic(err)
}
e := echo.New()
e.Use(echomiddleware.Logger())
userInteractor := interactor.NewUser(store)
userLoader := middleware.UserLoader{
Interactor: userInteractor,
}
healthInteractor := interactor.NewHealthCheck(store)
healthCheckHandler := handler.NewHealthChecker(healthInteractor)
invitationInteractor := interactor.NewInvitation(store)
invitationHandler := handler.NewInvitationHandler(invitationInteractor)
loginHandler := handler.NewLoginHandler(os.Getenv("DOMAIN"))
googleOauthConfig := &oauth2.Config{
RedirectURL: os.Getenv("REDIRECT_URL"),
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
Scopes: []string{"openid", "email", "profile"},
Endpoint: google.Endpoint,
}
google := oauth.NewGoogle(googleOauthConfig)
googleHandler := handler.NewGoogleHandler(google)
e.Use(session.Middleware(sessions.NewCookieStore(rawSessionAuthKey, rawSessionEncryptionKey)))
e.GET("/v1/health", healthCheckHandler.HealthCheck)
e.GET("/invitations", invitationHandler.FindInvitationByInviter)
e.POST("/v1/invitations", invitationHandler.Create, userLoader.Do)
e.GET("/v1/healthz", healthCheckHandler.HealthCheck)
e.GET("/v1/login", loginHandler.Login)
e.GET("/v1/google/login", googleHandler.HandleMain)
e.GET("/v1/google/exchange", googleHandler.ExchangeAuthCodeForTOken)
e.Server.Addr = ":8080"
graceful.DefaultLogger().Println("Application has successfully started at port: ", 8080)
graceful.ListenAndServe(e.Server, 5*time.Second)
}