55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
basev1 "gl/gen/base/v1"
|
|
ledgerv1 "gl/gen/ledger/v1"
|
|
"gl/infrastructure/config"
|
|
"gl/infrastructure/postgres"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
func main() {
|
|
configPath := flag.String("conf", "/app/config.toml", "path to the GL TOML configuration file")
|
|
grpcProbe := flag.Bool("grpc", false, "check the serving GL API instead of only its database")
|
|
flag.Parse()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
cfg, err := config.Load(*configPath)
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *grpcProbe {
|
|
host := cfg.GRPC.Host
|
|
if host == "0.0.0.0" || host == "::" {
|
|
host = "127.0.0.1"
|
|
}
|
|
connection, err := grpc.NewClient(net.JoinHostPort(host, strconv.Itoa(cfg.GRPC.Port)), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
defer connection.Close()
|
|
health, err := ledgerv1.NewGeneralLedgerServiceClient(connection).Health(ctx, &basev1.Empty{})
|
|
if err != nil || !health.GetServing() || !health.GetDatabaseReady() {
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
database, err := postgres.Open(ctx, cfg.Database)
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
defer database.Close()
|
|
if err := database.Ping(ctx); err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|