2024-03-19 15:49:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2024-05-06 08:44:08 +00:00
|
|
|
"os"
|
2024-03-19 15:49:29 +00:00
|
|
|
|
|
|
|
v1 "git.badhouseplants.net/softplayer/softplayer-backend/api/v1"
|
2024-03-19 16:20:32 +00:00
|
|
|
"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-05-04 17:48:19 +00:00
|
|
|
applications_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/applications"
|
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"
|
2024-05-06 09:39:28 +00:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/go-logr/zapr"
|
|
|
|
"go.uber.org/zap"
|
2024-03-19 15:49:29 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
)
|
|
|
|
|
2024-03-19 16:20:32 +00:00
|
|
|
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-05-06 08:44:08 +00:00
|
|
|
DownloadDir string `env:"SOFTPLAYER_DOWNLOAD_DIR" default:"/tmp/softplayer"`
|
2024-03-19 16:20:32 +00:00
|
|
|
}
|
2024-03-19 15:49:29 +00:00
|
|
|
|
2024-03-19 16:20:32 +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":
|
2024-03-19 16:20:32 +00:00
|
|
|
if err := server(CLI.Serve); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-03-19 15:49:29 +00:00
|
|
|
default:
|
|
|
|
panic(ctx.Command())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 16:20:32 +00:00
|
|
|
func server(params Serve) error {
|
2024-05-06 08:44:08 +00:00
|
|
|
// Make sure the download dir exists
|
2024-05-06 09:39:28 +00:00
|
|
|
var log logr.Logger
|
|
|
|
|
|
|
|
zapLog, err := zap.NewDevelopment()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
|
|
|
|
}
|
|
|
|
log = zapr.NewLogger(zapLog)
|
2024-05-06 15:30:35 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(params.DownloadDir, 0777); err != nil {
|
|
|
|
log.Error(err, "Coulnd't create a download dir")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init a logger
|
2024-05-06 09:39:28 +00:00
|
|
|
|
2024-03-19 16:20:32 +00:00
|
|
|
controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
|
2024-03-19 15:49:29 +00:00
|
|
|
if err != nil {
|
2024-05-06 15:30:35 +00:00
|
|
|
log.Error(err, "Coulnd't start a kube manager")
|
2024-03-19 16:20:32 +00:00
|
|
|
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-19 16:20:32 +00:00
|
|
|
}()
|
|
|
|
|
2024-03-21 20:10:56 +00:00
|
|
|
emailConfig := email.EmailConf{
|
2024-03-19 16:20:32 +00:00
|
|
|
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
|
|
|
|
2024-03-19 16:20:32 +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-05-06 09:39:28 +00:00
|
|
|
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl(controller, log))
|
2024-05-21 22:00:31 +00:00
|
|
|
accounts.RegisterAccountsServer(grpcServer, v1.NewAccountRPCImpl(controller, params.HashCost, &emailConfig, params.DevMode))
|
2024-04-03 18:05:23 +00:00
|
|
|
email_proto.RegisterEmailValidationServer(grpcServer, v1.InitEmailServer(controller, &emailConfig, params.DevMode))
|
2024-05-13 08:41:14 +00:00
|
|
|
applications_proto.RegisterApplicationsServer(grpcServer, v1.NewApplicationsGrpcImpl(controller, log))
|
2024-05-04 17:48:19 +00:00
|
|
|
|
2024-03-19 16:20:32 +00:00
|
|
|
if err := grpcServer.Serve(lis); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|