connect to maps grpc
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

This commit is contained in:
2025-07-25 20:45:30 -07:00
parent b98b184a32
commit 7a37224487
4 changed files with 32 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ const (
// Handler is a base handler that provides common functionality for all HTTP handlers.
type Handler struct {
dataClient *grpc.ClientConn
mapsClient *grpc.ClientConn
}
// HandlerOption defines a functional option for configuring a Handler
@@ -26,6 +27,13 @@ func WithDataClient(dataClient *grpc.ClientConn) HandlerOption {
}
}
// WithMapsClient sets the maps client for the Handler
func WithMapsClient(mapsClient *grpc.ClientConn) HandlerOption {
return func(h *Handler) {
h.mapsClient = mapsClient
}
}
// NewHandler creates a new Handler with the provided options.
// It requires both a datastore and an authentication service to function properly.
func NewHandler(options ...HandlerOption) (*Handler, error) {

View File

@@ -49,7 +49,7 @@ func (h *MapHandler) Get(ctx *gin.Context) {
}
// Call the gRPC service
mapData, err := maps_extended.NewMapsServiceClient(h.dataClient).Get(ctx, &maps_extended.MapId{
mapData, err := maps_extended.NewMapsServiceClient(h.mapsClient).Get(ctx, &maps_extended.MapId{
ID: mapID,
})
if err != nil {
@@ -116,7 +116,7 @@ func (h *MapHandler) List(ctx *gin.Context) {
}
// Call the gRPC service
mapList, err := maps_extended.NewMapsServiceClient(h.dataClient).List(ctx, &maps_extended.ListRequest{
mapList, err := maps_extended.NewMapsServiceClient(h.mapsClient).List(ctx, &maps_extended.ListRequest{
Filter: &maps_extended.MapFilter{
GameID: filter.GameID,
},

View File

@@ -25,6 +25,7 @@ type RouterConfig struct {
port int
devClient *grpc.ClientConn
dataClient *grpc.ClientConn
mapsClient *grpc.ClientConn
context *cli.Context
shutdownTimeout time.Duration
}
@@ -57,6 +58,13 @@ func WithDataClient(conn *grpc.ClientConn) Option {
}
}
// WithMapsClient sets the maps gRPC client
func WithMapsClient(conn *grpc.ClientConn) Option {
return func(cfg *RouterConfig) {
cfg.mapsClient = conn
}
}
// WithShutdownTimeout sets the graceful shutdown timeout
func WithShutdownTimeout(timeout time.Duration) Option {
return func(cfg *RouterConfig) {
@@ -72,6 +80,7 @@ func setupRoutes(cfg *RouterConfig) (*gin.Engine, error) {
handlerOptions := []handlers.HandlerOption{
handlers.WithDataClient(cfg.dataClient),
handlers.WithMapsClient(cfg.mapsClient),
}
// Times handler

View File

@@ -31,6 +31,12 @@ func NewApiCommand() *cli.Command {
EnvVars: []string{"DATA_RPC_HOST"},
Value: "data-service:9000",
},
&cli.StringFlag{
Name: "maps-rpc-host",
Usage: "Host of maps rpc",
EnvVars: []string{"MAPS_RPC_HOST"},
Value: "maptest-api:8081",
},
},
}
}
@@ -48,10 +54,17 @@ func runAPI(ctx *cli.Context) error {
return err
}
// Maps service client
mapsConn, err := grpc.Dial(ctx.String("maps-rpc-host"), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
return api.NewRouter(
api.WithContext(ctx),
api.WithPort(ctx.Int("port")),
api.WithDevClient(devConn),
api.WithDataClient(dataConn),
api.WithMapsClient(mapsConn),
)
}