2024-11-26 23:30:58 +00:00
|
|
|
package cmds
|
2024-11-26 23:28:48 +00:00
|
|
|
|
2024-11-26 23:30:58 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/api"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/datastore/gormstore"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/service"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"net/http"
|
2024-11-29 21:58:47 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2024-11-26 23:30:58 +00:00
|
|
|
)
|
2024-11-26 23:28:48 +00:00
|
|
|
|
2024-11-26 23:30:58 +00:00
|
|
|
func NewServeCommand() *cli.Command {
|
2024-11-26 23:28:48 +00:00
|
|
|
return &cli.Command{
|
2024-11-26 23:30:58 +00:00
|
|
|
Name: "serve",
|
|
|
|
Usage: "Run maps service",
|
|
|
|
Action: serve,
|
2024-11-26 23:28:48 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pg-host",
|
|
|
|
Usage: "Host of postgres database",
|
|
|
|
EnvVars: []string{"PG_HOST"},
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "pg-port",
|
|
|
|
Usage: "Port of postgres database",
|
|
|
|
EnvVars: []string{"PG_PORT"},
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pg-db",
|
|
|
|
Usage: "Name of database to connect to",
|
|
|
|
EnvVars: []string{"PG_DB"},
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pg-user",
|
|
|
|
Usage: "User to connect with",
|
|
|
|
EnvVars: []string{"PG_USER"},
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "pg-password",
|
|
|
|
Usage: "Password to connect with",
|
|
|
|
EnvVars: []string{"PG_PASSWORD"},
|
|
|
|
Required: true,
|
|
|
|
},
|
2024-11-26 23:30:58 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "migrate",
|
|
|
|
Usage: "Run database migrations",
|
|
|
|
Value: true,
|
|
|
|
EnvVars: []string{"MIGRATE"},
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "port",
|
|
|
|
Usage: "Port to listen on",
|
|
|
|
Value: 8080,
|
|
|
|
EnvVars: []string{"PORT"},
|
|
|
|
},
|
2024-11-26 23:28:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-11-26 23:30:58 +00:00
|
|
|
|
|
|
|
func serve(ctx *cli.Context) error {
|
|
|
|
db, err := gormstore.New(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("failed to connect database")
|
|
|
|
}
|
2024-11-29 21:58:47 +00:00
|
|
|
svc := &service.Service{
|
|
|
|
DB: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := grpc.Dial("auth-service:8090", grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
sec := service.SecurityHandler{
|
|
|
|
Client: conn,
|
|
|
|
}
|
2024-11-26 23:30:58 +00:00
|
|
|
|
2024-11-29 21:58:47 +00:00
|
|
|
srv, err := api.NewServer(svc, sec, api.WithPathPrefix("/v1"))
|
2024-11-26 23:30:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("failed to initialize api server")
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.ListenAndServe(fmt.Sprintf(":%d", ctx.Int("port")), srv)
|
|
|
|
}
|