maps-service/pkg/cmds/serve.go

145 lines
3.5 KiB
Go
Raw Normal View History

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"
2024-12-14 12:06:49 +00:00
"net/http"
2024-12-12 22:29:20 +00:00
"git.itzana.me/strafesnet/go-grpc/auth"
2024-11-26 23:30:58 +00:00
"git.itzana.me/strafesnet/maps-service/pkg/api"
"git.itzana.me/strafesnet/maps-service/pkg/datastore/gormstore"
2024-12-14 12:06:49 +00:00
internal "git.itzana.me/strafesnet/maps-service/pkg/internal"
2024-11-26 23:30:58 +00:00
"git.itzana.me/strafesnet/maps-service/pkg/service"
2024-12-14 12:06:49 +00:00
"git.itzana.me/strafesnet/maps-service/pkg/service_internal"
2024-12-12 22:29:20 +00:00
"github.com/nats-io/nats.go"
2024-11-26 23:30:58 +00:00
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
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-12-14 12:06:49 +00:00
&cli.IntFlag{
Name: "port-internal",
Usage: "Port to listen on for internal api",
Value: 8081,
EnvVars: []string{"PORT_INTERNAL"},
},
2024-12-10 03:26:59 +00:00
&cli.StringFlag{
2024-11-29 23:49:25 +00:00
Name: "auth-rpc-host",
Usage: "Host of auth rpc",
EnvVars: []string{"AUTH_RPC_HOST"},
Value: "auth-service:8090",
},
&cli.StringFlag{
Name: "nats-host",
Usage: "Host of nats",
EnvVars: []string{"NATS_HOST"},
Value: "nats:4222",
2024-12-10 03:26:59 +00:00
},
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 23:49:25 +00:00
nc, err := nats.Connect(ctx.String("nats-host"))
if err != nil {
log.WithError(err).Fatal("failed to connect nats")
}
2024-12-11 03:30:14 +00:00
js, err := nc.JetStream()
if err != nil {
log.WithError(err).Fatal("failed to start jetstream")
}
2024-12-12 05:07:51 +00:00
_, err = js.AddStream(&nats.StreamConfig{
2024-12-12 22:29:20 +00:00
Name: "maptest",
Subjects: []string{"maptest.>"},
2024-12-12 06:58:48 +00:00
Retention: nats.WorkQueuePolicy,
2024-12-12 05:07:51 +00:00
})
2024-12-11 03:30:14 +00:00
if err != nil {
2024-12-12 05:07:51 +00:00
log.WithError(err).Fatal("failed to add stream")
2024-12-11 03:30:14 +00:00
}
2024-12-12 05:07:51 +00:00
2024-11-29 21:58:47 +00:00
svc := &service.Service{
2024-12-12 22:29:20 +00:00
DB: db,
2024-12-11 03:30:14 +00:00
Nats: js,
2024-11-29 21:58:47 +00:00
}
2024-12-10 03:26:59 +00:00
conn, err := grpc.Dial(ctx.String("auth-rpc-host"), grpc.WithTransportCredentials(insecure.NewCredentials()))
2024-11-29 21:58:47 +00:00
if err != nil {
log.Fatal(err)
}
sec := service.SecurityHandler{
2024-12-10 04:10:23 +00:00
Client: auth.NewAuthServiceClient(conn),
2024-11-29 21:58:47 +00:00
}
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")
}
2024-12-14 12:06:49 +00:00
svc2 := &service_internal.Service{
DB: db,
Nats: js,
}
srv2, err := internal.NewServer(svc2, nil, internal.WithPathPrefix("/v1"))
if err != nil {
log.WithError(err).Fatal("failed to initialize api server")
}
// idk how else to do this
go http.ListenAndServe(fmt.Sprintf(":%d", ctx.Int("port-internal")), srv2)
2024-11-26 23:30:58 +00:00
return http.ListenAndServe(fmt.Sprintf(":%d", ctx.Int("port")), srv)
}