feat: backend service
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package backendclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"galaxy/gateway/internal/downstream"
|
||||
lobbymodel "galaxy/model/lobby"
|
||||
usermodel "galaxy/model/user"
|
||||
)
|
||||
|
||||
// UserRoutes returns the authenticated `user.*` downstream routes
|
||||
// served by backend. When client is nil every route resolves to a
|
||||
// dependency-unavailable client so the static router still recognises
|
||||
// the message types.
|
||||
func UserRoutes(client *RESTClient) map[string]downstream.Client {
|
||||
target := downstream.Client(unavailableClient{})
|
||||
if client != nil {
|
||||
target = userCommandClient{rest: client}
|
||||
}
|
||||
return map[string]downstream.Client{
|
||||
usermodel.MessageTypeGetMyAccount: target,
|
||||
usermodel.MessageTypeUpdateMyProfile: target,
|
||||
usermodel.MessageTypeUpdateMySettings: target,
|
||||
}
|
||||
}
|
||||
|
||||
// LobbyRoutes returns the authenticated `lobby.*` downstream routes
|
||||
// served by backend. When client is nil every route resolves to a
|
||||
// dependency-unavailable client.
|
||||
func LobbyRoutes(client *RESTClient) map[string]downstream.Client {
|
||||
target := downstream.Client(unavailableClient{})
|
||||
if client != nil {
|
||||
target = lobbyCommandClient{rest: client}
|
||||
}
|
||||
return map[string]downstream.Client{
|
||||
lobbymodel.MessageTypeMyGamesList: target,
|
||||
lobbymodel.MessageTypeOpenEnrollment: target,
|
||||
}
|
||||
}
|
||||
|
||||
type unavailableClient struct{}
|
||||
|
||||
func (unavailableClient) ExecuteCommand(context.Context, downstream.AuthenticatedCommand) (downstream.UnaryResult, error) {
|
||||
return downstream.UnaryResult{}, downstream.ErrDownstreamUnavailable
|
||||
}
|
||||
|
||||
type userCommandClient struct {
|
||||
rest *RESTClient
|
||||
}
|
||||
|
||||
func (c userCommandClient) ExecuteCommand(ctx context.Context, command downstream.AuthenticatedCommand) (downstream.UnaryResult, error) {
|
||||
return c.rest.ExecuteUserCommand(ctx, command)
|
||||
}
|
||||
|
||||
type lobbyCommandClient struct {
|
||||
rest *RESTClient
|
||||
}
|
||||
|
||||
func (c lobbyCommandClient) ExecuteCommand(ctx context.Context, command downstream.AuthenticatedCommand) (downstream.UnaryResult, error) {
|
||||
return c.rest.ExecuteLobbyCommand(ctx, command)
|
||||
}
|
||||
|
||||
var (
|
||||
_ downstream.Client = unavailableClient{}
|
||||
_ downstream.Client = userCommandClient{}
|
||||
_ downstream.Client = lobbyCommandClient{}
|
||||
)
|
||||
Reference in New Issue
Block a user