-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
231 lines (192 loc) · 8.14 KB
/
init.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
pb_account "github.com/PretendoNetwork/grpc-go/account"
pb_friends "github.com/PretendoNetwork/grpc-go/friends"
"github.com/PretendoNetwork/pokemon-rumble-world/database"
"github.com/PretendoNetwork/pokemon-rumble-world/globals"
"github.com/PretendoNetwork/plogger-go"
"github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func init() {
globals.Logger = plogger.NewLogger()
var err error
err = godotenv.Load()
if err != nil {
globals.Logger.Warning("Error loading .env file")
}
postgresURI := os.Getenv("PN_PRW_POSTGRES_URI")
kerberosPassword := os.Getenv("PN_PRW_KERBEROS_PASSWORD")
authenticationServerPort := os.Getenv("PN_PRW_AUTHENTICATION_SERVER_PORT")
secureServerHost := os.Getenv("PN_PRW_SECURE_SERVER_HOST")
secureServerPort := os.Getenv("PN_PRW_SECURE_SERVER_PORT")
hppServerHost := os.Getenv("PN_PRW_HPP_SERVER_HOST")
hppServerPort := os.Getenv("PN_PRW_HPP_SERVER_PORT")
accountGRPCHost := os.Getenv("PN_PRW_ACCOUNT_GRPC_HOST")
accountGRPCPort := os.Getenv("PN_PRW_ACCOUNT_GRPC_PORT")
accountGRPCAPIKey := os.Getenv("PN_PRW_ACCOUNT_GRPC_API_KEY")
friendsGRPCHost := os.Getenv("PN_PRW_FRIENDS_GRPC_HOST")
friendsGRPCPort := os.Getenv("PN_PRW_FRIENDS_GRPC_PORT")
friendsGRPCAPIKey := os.Getenv("PN_PRW_FRIENDS_GRPC_API_KEY")
s3Endpoint := os.Getenv("PN_PRW_S3_ENDPOINT")
s3Region := os.Getenv("PN_PRW_S3_REGION")
s3AccessKey := os.Getenv("PN_PRW_S3_ACCESS_KEY")
s3AccessSecret := os.Getenv("PN_PRW_S3_ACCESS_SECRET")
s3Bucket := os.Getenv("PN_PRW_S3_BUCKET")
if strings.TrimSpace(postgresURI) == "" {
globals.Logger.Error("PN_PRW_POSTGRES_URI environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(kerberosPassword) == "" {
globals.Logger.Warningf("PN_PRW_KERBEROS_PASSWORD environment variable not set. Using default password: %q", globals.KerberosPassword)
} else {
globals.KerberosPassword = kerberosPassword
}
if strings.TrimSpace(authenticationServerPort) == "" {
globals.Logger.Error("PN_PRW_AUTHENTICATION_SERVER_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(authenticationServerPort); err != nil {
globals.Logger.Errorf("PN_PRW_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort)
os.Exit(0)
} else if port < 0 || port > 65535 {
globals.Logger.Errorf("PN_PRW_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort)
os.Exit(0)
}
if strings.TrimSpace(secureServerHost) == "" {
globals.Logger.Error("PN_PRW_SECURE_SERVER_HOST environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(secureServerPort) == "" {
globals.Logger.Error("PN_PRW_SECURE_SERVER_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(secureServerPort); err != nil {
globals.Logger.Errorf("PN_PRW_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort)
os.Exit(0)
} else if port < 0 || port > 65535 {
globals.Logger.Errorf("PN_PRW_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort)
os.Exit(0)
}
if strings.TrimSpace(hppServerHost) == "" {
globals.Logger.Error("PN_PRW_HPP_SERVER_HOST environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(hppServerPort) == "" {
globals.Logger.Error("PN_PRW_HPP_SERVER_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(hppServerPort); err != nil {
globals.Logger.Errorf("PN_PRW_HPP_SERVER_PORT is not a valid port. Expected 0-65535, got %s", hppServerPort)
os.Exit(0)
} else if port < 0 || port > 65535 {
globals.Logger.Errorf("PN_PRW_HPP_SERVER_PORT is not a valid port. Expected 0-65535, got %s", hppServerPort)
os.Exit(0)
}
if strings.TrimSpace(accountGRPCHost) == "" {
globals.Logger.Error("PN_PRW_ACCOUNT_GRPC_HOST environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(accountGRPCPort) == "" {
globals.Logger.Error("PN_PRW_ACCOUNT_GRPC_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(accountGRPCPort); err != nil {
globals.Logger.Errorf("PN_PRW_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort)
os.Exit(0)
} else if port < 0 || port > 65535 {
globals.Logger.Errorf("PN_PRW_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort)
os.Exit(0)
}
if strings.TrimSpace(accountGRPCAPIKey) == "" {
globals.Logger.Warning("Insecure gRPC server detected. PN_PRW_ACCOUNT_GRPC_API_KEY environment variable not set")
}
globals.GRPCAccountClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", accountGRPCHost, accountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
globals.Logger.Criticalf("Failed to connect to account gRPC server: %v", err)
os.Exit(0)
}
globals.GRPCAccountClient = pb_account.NewAccountClient(globals.GRPCAccountClientConnection)
globals.GRPCAccountCommonMetadata = metadata.Pairs(
"X-API-Key", accountGRPCAPIKey,
)
if strings.TrimSpace(friendsGRPCHost) == "" {
globals.Logger.Error("PN_PRW_FRIENDS_GRPC_HOST environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(friendsGRPCPort) == "" {
globals.Logger.Error("PN_PRW_FRIENDS_GRPC_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(friendsGRPCPort); err != nil {
globals.Logger.Errorf("PN_PRW_FRIENDS_GRPC_PORT is not a valid port. Expected 0-65535, got %s", friendsGRPCPort)
os.Exit(0)
} else if port < 0 || port > 65535 {
globals.Logger.Errorf("PN_PRW_FRIENDS_GRPC_PORT is not a valid port. Expected 0-65535, got %s", friendsGRPCPort)
os.Exit(0)
}
if strings.TrimSpace(friendsGRPCAPIKey) == "" {
globals.Logger.Warning("Insecure gRPC server detected. PN_PRW_FRIENDS_GRPC_API_KEY environment variable not set")
}
globals.GRPCFriendsClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", friendsGRPCHost, friendsGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
globals.Logger.Criticalf("Failed to connect to friends gRPC server: %v", err)
os.Exit(0)
}
globals.GRPCFriendsClient = pb_friends.NewFriendsClient(globals.GRPCFriendsClientConnection)
globals.GRPCFriendsCommonMetadata = metadata.Pairs(
"X-API-Key", friendsGRPCAPIKey,
)
if strings.TrimSpace(s3Endpoint) == "" {
globals.Logger.Error("PN_PRW_S3_ENDPOINT environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(s3Region) == "" {
globals.Logger.Error("PN_PRW_S3_REGION environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(s3AccessKey) == "" {
globals.Logger.Error("PN_PRW_S3_ACCESS_KEY environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(s3AccessSecret) == "" {
globals.Logger.Error("PN_PRW_S3_ACCESS_SECRET environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(s3Bucket) == "" {
globals.Logger.Error("PN_PRW_S3_BUCKET environment variable not set")
os.Exit(0)
}
staticCredentials := credentials.NewStaticCredentialsProvider(s3AccessKey, s3AccessSecret, "")
endpointResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: s3Endpoint,
SigningRegion: s3Region,
}, nil
})
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion(s3Region),
config.WithCredentialsProvider(staticCredentials),
config.WithEndpointResolverWithOptions(endpointResolver),
)
if err != nil {
globals.Logger.Criticalf("Failed to create S3 config: %v", err)
os.Exit(0)
}
globals.S3Client = s3.NewFromConfig(cfg)
globals.S3PresignClient = s3.NewPresignClient(globals.S3Client)
globals.S3PresignPostClient = globals.NewPresignClient(cfg)
database.ConnectPostgres()
}