softplayer-backend/main.go

86 lines
2.4 KiB
Go
Raw Normal View History

2024-03-19 15:49:29 +00:00
package main
import (
"context"
"fmt"
"net"
v1 "git.badhouseplants.net/softplayer/softplayer-backend/api/v1"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
2024-03-19 15:49:29 +00:00
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts"
2024-03-21 20:10:56 +00:00
email_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
2024-04-03 18:05:23 +00:00
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
2024-03-19 15:49:29 +00:00
"github.com/alecthomas/kong"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
ctrl "sigs.k8s.io/controller-runtime"
)
type Serve struct {
2024-04-03 18:05:23 +00:00
Port int64 `short:"p" env:"SOFTPLAYER_PORT" default:"4020"`
2024-03-21 17:39:32 +00:00
Host string `env:"SOFTPLAYER_HOST" default:"0.0.0.0"`
2024-03-21 20:10:56 +00:00
HashCost int16 `env:"SOFTPLAYER_HASH_COST" default:"1"`
2024-03-21 17:39:32 +00:00
Reflection bool `env:"SOFTPLAYER_REFLECTION" default:"false"`
2024-04-03 18:05:23 +00:00
DevMode bool `env:"SOFTPLAYER_DEV_MODE" default:"false"`
2024-03-21 17:39:32 +00:00
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"`
}
2024-03-19 15:49:29 +00:00
var CLI struct {
Serve Serve `cmd:"" help:"Start the grpc server"`
2024-03-19 15:49:29 +00:00
}
func main() {
ctx := kong.Parse(&CLI)
switch ctx.Command() {
case "serve":
if err := server(CLI.Serve); err != nil {
panic(err)
}
2024-03-19 15:49:29 +00:00
default:
panic(ctx.Command())
}
}
func server(params Serve) error {
controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
2024-03-19 15:49:29 +00:00
if err != nil {
return err
}
2024-03-21 20:10:56 +00:00
// TODO: Handle the error
2024-03-21 17:39:32 +00:00
go func() {
2024-03-21 20:10:56 +00:00
if err := controller.Start(context.Background()); err != nil {
panic(err)
}
}()
2024-03-21 20:10:56 +00:00
emailConfig := email.EmailConf{
From: params.SmtpFrom,
Password: params.SmtpPassword,
SmtpHost: params.SmtpHost,
SmtpPort: params.SmtpPort,
2024-03-19 15:49:29 +00:00
}
2024-04-03 18:05:23 +00:00
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)
}
2024-04-03 18:05:23 +00:00
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl(controller))
2024-03-21 20:10:56 +00:00
accounts.RegisterAccountsServer(grpcServer, v1.NewAccountRPCImpl(controller, params.HashCost))
2024-04-03 18:05:23 +00:00
email_proto.RegisterEmailValidationServer(grpcServer, v1.InitEmailServer(controller, &emailConfig, params.DevMode))
if err := grpcServer.Serve(lis); err != nil {
return err
}
return nil
2024-03-19 15:49:29 +00:00
}