-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.go
65 lines (53 loc) · 2.02 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
pb "github.com/PretendoNetwork/grpc-go/friends"
"github.com/PretendoNetwork/plogger-go"
"github.com/PretendoNetwork/wiiu-chat-secure/database"
"github.com/PretendoNetwork/wiiu-chat-secure/globals"
"github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
var logger = plogger.NewLogger()
func init() {
err := godotenv.Load()
if err != nil {
logger.Warning("Error loading .env file")
}
friendsGRPCHost := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_HOST")
friendsGRPCPort := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_PORT")
friendsGRPCAPIKey := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_API_KEY")
if strings.TrimSpace(friendsGRPCHost) == "" {
globals.Logger.Error("PN_WIIU_CHAT_FRIENDS_GRPC_HOST environment variable not set")
os.Exit(0)
}
if strings.TrimSpace(friendsGRPCPort) == "" {
globals.Logger.Error("PN_WIIU_CHAT_FRIENDS_GRPC_PORT environment variable not set")
os.Exit(0)
}
if port, err := strconv.Atoi(friendsGRPCPort); err != nil {
globals.Logger.Errorf("PN_WIIU_CHAT_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_WIIU_CHAT_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_WIIU_CHAT_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.NewFriendsClient(globals.GRPCFriendsClientConnection)
globals.GRPCFriendsCommonMetadata = metadata.Pairs(
"X-API-Key", friendsGRPCAPIKey,
)
database.ConnectAll()
}