alt/services/api/internal/socket/handlers.go
toki 3ee268816b refactor: update API socket handlers and contract parsers
- Add parser map for contract type resolution
- Update socket handlers with new message routing
- Add parser map tests
- Remove outdated code review and plan docs for G07
2026-05-30 19:14:07 +09:00

72 lines
2.4 KiB
Go

package socket
import (
protoSocket "git.toki-labs.com/toki/proto-socket/go"
altv1 "git.toki-labs.com/toki/alt/packages/contracts/gen/go/alt/v1"
)
// sessionHandler is one request-response unit attached to a freshly connected
// client communicator. Splitting registration into independent units keeps the
// api hub control-plane surface thin: later command/query handlers attach
// through the same registry instead of growing a single registration function.
type sessionHandler struct {
// requestType is the ALT protobuf request type name the handler answers.
// It lets the registry detect duplicate or missing handler coverage without
// a live socket connection.
requestType string
// register attaches the handler onto the client communicator.
register func(*protoSocket.WsClient)
}
// sessionHandlers returns the ordered set of handlers registered on every API
// client session. Hello is registered through this registry so its behaviour
// stays identical while gaining the shared registration path.
func sessionHandlers() []sessionHandler {
return []sessionHandler{
helloHandler(),
}
}
// registerSessionHandlers wires every session handler onto a newly connected
// client. It is the OnClientConnected entrypoint for the API socket server.
func registerSessionHandlers(client *protoSocket.WsClient) {
registerHandlers(client, sessionHandlers())
}
// registerHandlers attaches the given handlers onto a client. nil registrars
// are skipped so a malformed registry entry cannot panic the connection setup
// path.
func registerHandlers(client *protoSocket.WsClient, handlers []sessionHandler) {
for _, handler := range handlers {
if handler.register == nil {
continue
}
handler.register(client)
}
}
func helloHandler() sessionHandler {
return sessionHandler{
requestType: protoSocket.TypeNameOf(&altv1.HelloRequest{}),
register: func(client *protoSocket.WsClient) {
protoSocket.AddRequestListenerTyped[*altv1.HelloRequest, *altv1.HelloResponse](&client.Communicator, handleHello)
},
}
}
func handleHello(req *altv1.HelloRequest) (*altv1.HelloResponse, error) {
protocolVersion := req.GetAltProtocolVersion()
if protocolVersion == "" {
protocolVersion = defaultAltProtocolVersion
}
return &altv1.HelloResponse{
ServerName: serverName,
ServerVersion: serverVersion,
AltProtocolVersion: protocolVersion,
Capabilities: []string{
"hello",
"request-response",
},
}, nil
}