Files
dev-service/pkg/cmds/rpc.go
itzaname c35b47447f
Some checks failed
continuous-integration/drone/push Build is failing
Add rpc
2025-06-22 12:10:52 -04:00

76 lines
1.6 KiB
Go

package cmds
import (
"fmt"
"git.itzana.me/strafesnet/dev-portal/pkg/cache"
"git.itzana.me/strafesnet/dev-portal/pkg/datastore/gormstore"
"git.itzana.me/strafesnet/dev-portal/pkg/ratelimit"
"git.itzana.me/strafesnet/dev-portal/pkg/rpc"
"git.itzana.me/strafesnet/go-grpc/dev"
"github.com/redis/go-redis/v9"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"net"
)
func NewRpcCommand() *cli.Command {
return &cli.Command{
Name: "rpc",
Usage: "Run dev rpc service",
Action: rpcServer,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "redis-addr",
Usage: "Address of redis database",
EnvVars: []string{"REDIS_ADDR"},
Required: true,
},
&cli.StringFlag{
Name: "redis-pass",
Usage: "Password of redis database",
EnvVars: []string{"REDIS_PASS"},
Required: false,
Value: "",
},
&cli.IntFlag{
Name: "redis-db",
Usage: "Number of database to connect to",
EnvVars: []string{"REDIS_DB"},
Required: true,
},
},
}
}
func rpcServer(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"),
})
// grpc server
ls, err := net.Listen("tcp", fmt.Sprintf(":%d", ctx.Int("port")))
if err != nil {
return err
}
grpcServer := grpc.NewServer()
dev.RegisterDevServiceServer(grpcServer, &rpc.Dev{
Store: store,
Limiter: ratelimit.New(rdb),
Cache: cache.NewCache(rdb, "app"),
})
return grpcServer.Serve(ls)
}