package main import ( "context" "fmt" "net" v1 "git.badhouseplants.net/softplayer/softplayer-backend/api/v1" "git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email" "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts" email_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email" "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments" "github.com/alecthomas/kong" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ctrl "sigs.k8s.io/controller-runtime" ) type Serve struct { Port int64 `short:"p" env:"SOFTPLAYER_PORT" default:"4020"` Host string `env:"SOFTPLAYER_HOST" default:"0.0.0.0"` HashCost int16 `env:"SOFTPLAYER_HASH_COST" default:"1"` Reflection bool `env:"SOFTPLAYER_REFLECTION" default:"false"` DevMode bool `env:"SOFTPLAYER_DEV_MODE" default:"false"` SmtpHost string `env:"SOFTPLAYER_SMTP_HOST"` SmtpPort string `env:"SOFTPLAYER_SMTP_PORT" default:"587"` SmtpFrom string `env:"SOFTPLAYER_SMTP_FROM" default:"overlord@badhouseplants.net"` SmtpPassword string `env:"SOFTPLAYER_SMTP_PASSWORD"` } var CLI struct { Serve Serve `cmd:"" help:"Start the grpc server"` } func main() { ctx := kong.Parse(&CLI) switch ctx.Command() { case "serve": if err := server(CLI.Serve); err != nil { panic(err) } default: panic(ctx.Command()) } } func server(params Serve) error { controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{}) if err != nil { return err } // TODO: Handle the error go func() { if err := controller.Start(context.Background()); err != nil { panic(err) } }() emailConfig := email.EmailConf{ From: params.SmtpFrom, Password: params.SmtpPassword, SmtpHost: params.SmtpHost, SmtpPort: params.SmtpPort, } address := fmt.Sprintf("%s:%d", params.Host, params.Port) lis, err := net.Listen("tcp", address) if err != nil { return err } grpcServer := grpc.NewServer() if params.Reflection { reflection.Register(grpcServer) } environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl(controller)) accounts.RegisterAccountsServer(grpcServer, v1.NewAccountRPCImpl(controller, params.HashCost)) email_proto.RegisterEmailValidationServer(grpcServer, v1.InitEmailServer(controller, &emailConfig, params.DevMode)) if err := grpcServer.Serve(lis); err != nil { return err } return nil }