75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package cmds
|
|
|
|
import (
|
|
"git.itzana.me/StrafesNET/dev-service/pkg/api"
|
|
"git.itzana.me/StrafesNET/dev-service/pkg/authz"
|
|
"git.itzana.me/StrafesNET/dev-service/pkg/datastore/gormstore"
|
|
"git.itzana.me/StrafesNET/dev-service/pkg/ratelimit"
|
|
"git.itzana.me/strafesnet/go-grpc/auth"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/urfave/cli/v2"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
func NewWebCommand() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "web",
|
|
Usage: "Run dev web service",
|
|
Action: runWeb,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "auth-rpc-host",
|
|
Usage: "Host of auth rpc",
|
|
EnvVars: []string{"AUTH_RPC_HOST"},
|
|
Value: "auth-service:8081",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "domain",
|
|
Usage: "Domain of the service",
|
|
EnvVars: []string{"DOMAIN"},
|
|
Required: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func runWeb(ctx *cli.Context) error {
|
|
// Setup database
|
|
store, err := gormstore.New(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Redis setup
|
|
rdb := redis.NewClient(&redis.Options{
|
|
Addr: ctx.String("redis-addr"),
|
|
Password: ctx.String("redis-pass"),
|
|
DB: ctx.Int("redis-db"),
|
|
})
|
|
|
|
// Auth service client
|
|
conn, err := grpc.Dial(ctx.String("auth-rpc-host"), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
authClient, err := authz.NewService(
|
|
authz.WithDatastore(store),
|
|
authz.WithAuthClient(auth.NewAuthServiceClient(conn)),
|
|
authz.WithDomain(ctx.String("domain")),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Launch api
|
|
return api.NewRouter(
|
|
api.WithContext(ctx),
|
|
api.WithPort(ctx.Int("port")),
|
|
api.WithDatastore(store),
|
|
api.WithAuth(authClient),
|
|
api.WithRateLimit(ratelimit.New(rdb)),
|
|
)
|
|
}
|