splatoon/init.go
Andrea Toska 238c85da9f
Some checks failed
Build and Publish Docker Image / build-publish (push) Has been cancelled
feat!: Implement Matchmaking Rewrite (#10)
* feat(secure): Mark Splatfest sessions as OpenParticipation

Bit of a hack but good enough to make things work for now

* feat(secure): Add stubbed Splatoon Ranking protocol

Fixes freezes related to Splatfest results upload

* feat: matchmaking rewrite implementation

* feat(secure): attempt to fix index bugs

* fix: merge issue

* fix(secure): forgot to change the import name

* fix(secure): remove old index ignore

* fix(secure): misspelling

* fix(secure): remove unused var

* fix(secure): revert previous fix

* fix(secure): using another function

* fix(secure): using another function

* working on it

* fix(secure): i hate these variable names

* fix(secure): misspelled again

* debug(secure): adding debugging

* fix(secure): hopefully fix the error (thanks dani)

* std(secure): standardize variable names

* fix(secure): fix string issue

* fix(secure): oops

* fix(secure): remove attribute 1

* fix(secure): final fixups

* fix(secure): final fixups

* feat(secure): try fixing fest rooms

* feat(secure): implement postgresql fix

* merge

* fix(secure): try once again to fix fests

* fix(secure): ignore first param

* fix: bump library versions and remove override

* chore: go fmt all files

* chore: Remove unused code

* chore: Explicitly ignore error in criteria cleanup

It's fine we just won't mm into this room

---------

Co-authored-by: Ash Logan <ash@heyquark.com>
2024-11-07 21:59:38 +11:00

114 lines
3.9 KiB
Go

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!")
}