connect to maps grpc
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user