alt/services/api/internal/socket/handlers.go
toki ec938475e0 chore: socket rail implementation and task archival
- Add backtest and market socket handlers for API and Worker
- Add proto definitions for backtest and market services
- Update client socket integration and tests
- Generate protobuf code for Go and Dart
- Archive completed agent tasks under agent-task/archive/2026/05/
2026-05-30 22:57:18 +09:00

76 lines
2.5 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 {
handlers := []sessionHandler{
helloHandler(),
}
handlers = append(handlers, marketHandlers()...)
handlers = append(handlers, backtestHandlers()...)
return handlers
}
// 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",
"market-read",
},
}, nil
}