-
Notifications
You must be signed in to change notification settings - Fork 4
/
init.go
113 lines (94 loc) · 3.93 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
package main
import (
"database/sql"
"fmt"
"os"
"strconv"
"strings"
pb "github.com/PretendoNetwork/grpc-go/account"
"github.com/PretendoNetwork/plogger-go"
"github.com/PretendoNetwork/splatoon/globals"
"github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
func init() {
globals.Logger = plogger.NewLogger()
var err error
err = godotenv.Load()
if err != nil {
globals.Logger.Warning("Error loading .env file")
}
kerberosPassword := os.Getenv("PN_SPLATOON_KERBEROS_PASSWORD")
authenticationServerPort := os.Getenv("PN_SPLATOON_AUTHENTICATION_SERVER_PORT")
secureServerHost := os.Getenv("PN_SPLATOON_SECURE_SERVER_HOST")
secureServerPort := os.Getenv("PN_SPLATOON_SECURE_SERVER_PORT")
accountGRPCHost := os.Getenv("PN_SPLATOON_ACCOUNT_GRPC_HOST")
accountGRPCPort := os.Getenv("PN_SPLATOON_ACCOUNT_GRPC_PORT")
accountGRPCAPIKey := os.Getenv("PN_SPLATOON_ACCOUNT_GRPC_API_KEY")
if strings.TrimSpace(kerberosPassword) == "" {
globals.Logger.Warningf("PN_SPLATOON_KERBEROS_PASSWORD environment variable not set. Using default password: %q", globals.KerberosPassword)
} else {
globals.KerberosPassword = kerberosPassword
}
globals.InitAccounts()
if strings.TrimSpace(authenticationServerPort) == "" {
globals.Logger.Error("PN_SPLATOON_AUTHENTICATION_SERVER_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(authenticationServerPort); err != nil {
globals.Logger.Errorf("PN_SPLATOON_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_SPLATOON_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_SPLATOON_SECURE_SERVER_HOST environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(secureServerPort) == "" {
globals.Logger.Error("PN_SPLATOON_SECURE_SERVER_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(secureServerPort); err != nil {
globals.Logger.Errorf("PN_SPLATOON_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_SPLATOON_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort)
os.Exit(0)
}
if strings.TrimSpace(accountGRPCHost) == "" {
globals.Logger.Error("PN_SPLATOON_ACCOUNT_GRPC_HOST environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(accountGRPCPort) == "" {
globals.Logger.Error("PN_SPLATOON_ACCOUNT_GRPC_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(accountGRPCPort); err != nil {
globals.Logger.Errorf("PN_SPLATOON_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_SPLATOON_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_SPLATOON_ACCOUNT_GRPC_API_KEY environment variable not set")
}
globals.GRPCAccountClientConnection, err = grpc.NewClient(fmt.Sprintf("dns:%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.NewAccountClient(globals.GRPCAccountClientConnection)
globals.GRPCAccountCommonMetadata = metadata.Pairs(
"X-API-Key", accountGRPCAPIKey,
)
globals.Postgres, err = sql.Open("postgres", os.Getenv("PN_SPLATOON_POSTGRES_URI"))
if err != nil {
globals.Logger.Critical(err.Error())
}
globals.Logger.Success("Connected to Postgres!")
}