softplayer-backend/main.go

111 lines
3.2 KiB
Go

package main
import (
"context"
"fmt"
"net"
"os"
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"
applications_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/applications"
email_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
"github.com/alecthomas/kong"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"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"`
DownloadDir string `env:"SOFTPLAYER_DOWNLOAD_DIR" default:"/tmp/softplayer"`
}
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 {
// Make sure the download dir exists
var log logr.Logger
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log = zapr.NewLogger(zapLog)
if err := os.MkdirAll(params.DownloadDir, 0777); err != nil {
log.Error(err, "Coulnd't create a download dir")
return err
}
// Init a logger
controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
if err != nil {
log.Error(err, "Coulnd't start a kube manager")
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, log))
accounts.RegisterAccountsServer(grpcServer, v1.NewAccountRPCImpl(controller, params.HashCost, &emailConfig, params.DevMode))
email_proto.RegisterEmailValidationServer(grpcServer, v1.InitEmailServer(controller, &emailConfig, params.DevMode))
applications_proto.RegisterApplicationsServer(grpcServer, v1.NewApplicationsGrpcImpl(controller, log))
if err := grpcServer.Serve(lis); err != nil {
return err
}
return nil
}