-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjection.go
113 lines (90 loc) · 2.85 KB
/
injection.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/jacobsngoodwin/memrizr/account/handler"
"github.com/jacobsngoodwin/memrizr/account/repository"
"github.com/jacobsngoodwin/memrizr/account/service"
)
// will initialize a handler starting from data sources
// which inject into repository layer
// which inject into service layer
// which inject into handler layer
func inject(d *dataSources) (*gin.Engine, error) {
log.Println("Injecting data sources")
/*
* repository layer
*/
userRepository := repository.NewUserRepository(d.DB)
tokenRepository := repository.NewTokenRepository(d.RedisClient)
/*
* service layer
*/
userService := service.NewUserService(&service.USConfig{
UserRepository: userRepository,
})
// load rsa keys
privKeyFile := os.Getenv("PRIV_KEY_FILE")
priv, err := ioutil.ReadFile(privKeyFile)
if err != nil {
return nil, fmt.Errorf("could not read private key pem file: %w", err)
}
privKey, err := jwt.ParseRSAPrivateKeyFromPEM(priv)
if err != nil {
return nil, fmt.Errorf("could not parse private key: %w", err)
}
pubKeyFile := os.Getenv("PUB_KEY_FILE")
pub, err := ioutil.ReadFile(pubKeyFile)
if err != nil {
return nil, fmt.Errorf("could not read public key pem file: %w", err)
}
pubKey, err := jwt.ParseRSAPublicKeyFromPEM(pub)
if err != nil {
return nil, fmt.Errorf("could not parse public key: %w", err)
}
// load refresh token secret from env variable
refreshSecret := os.Getenv("REFRESH_SECRET")
// load expiration lengths from env variables and parse as int
idTokenExp := os.Getenv("ID_TOKEN_EXP")
refreshTokenExp := os.Getenv("REFRESH_TOKEN_EXP")
idExp, err := strconv.ParseInt(idTokenExp, 0, 64)
if err != nil {
return nil, fmt.Errorf("could not parse ID_TOKEN_EXP as int: %w", err)
}
refreshExp, err := strconv.ParseInt(refreshTokenExp, 0, 64)
if err != nil {
return nil, fmt.Errorf("could not parse REFRESH_TOKEN_EXP as int: %w", err)
}
tokenService := service.NewTokenService(&service.TSConfig{
TokenRepository: tokenRepository,
PrivKey: privKey,
PubKey: pubKey,
RefreshSecret: refreshSecret,
IDExpirationSecs: idExp,
RefreshExpirationSecs: refreshExp,
})
// initialize gin.Engine
router := gin.Default()
// read in ACCOUNT_API_URL
baseURL := os.Getenv("ACCOUNT_API_URL")
// read in HANDLER_TIMEOUT
handlerTimeout := os.Getenv("HANDLER_TIMEOUT")
ht, err := strconv.ParseInt(handlerTimeout, 0, 64)
if err != nil {
return nil, fmt.Errorf("could not parse HANDLER_TIMEOUT as int: %w", err)
}
handler.NewHandler(&handler.Config{
R: router,
UserService: userService,
TokenService: tokenService,
BaseURL: baseURL,
TimeoutDuration: time.Duration(time.Duration(ht) * time.Second),
})
return router, nil
}