summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-02 09:54:16 +0200
committerPaul Buetow <paul@buetow.org>2026-03-02 09:54:16 +0200
commit7295937e97bf21affc784ec4e0d0d898e59b44e8 (patch)
tree47439507244b68c4ec383fccf65cdb12a8b00411
parent7fee96ccd4328eb619a9c586802a2caba68c12fc (diff)
refactor(handlers): use command registry in server handler
-rw-r--r--internal/server/handlers/serverhandler.go86
1 files changed, 50 insertions, 36 deletions
diff --git a/internal/server/handlers/serverhandler.go b/internal/server/handlers/serverhandler.go
index f40081e..c562c9a 100644
--- a/internal/server/handlers/serverhandler.go
+++ b/internal/server/handlers/serverhandler.go
@@ -23,10 +23,13 @@ type ServerHandler struct {
tailLimiter chan struct{}
serverCfg *config.ServerConfig
regex string
+ commands map[string]commandHandler
// Track pending files waiting for limiter slots
pendingFiles int32
}
+type commandHandler func(context.Context, lcontext.LContext, int, []string, func())
+
var _ Handler = (*ServerHandler)(nil)
// NewServerHandler returns the server handler.
@@ -54,6 +57,7 @@ func NewServerHandler(user *user.User, catLimiter,
regex: ".",
}
h.handleCommandCb = h.handleUserCommand
+ h.commands = h.newCommandRegistry()
fqdn, err := config.Hostname()
if err != nil {
@@ -82,45 +86,55 @@ func (h *ServerHandler) handleUserCommand(ctx context.Context, ltx lcontext.LCon
}
}
- switch commandName {
- case "grep":
- command := newReadCommand(h, omode.GrepClient)
- go func() {
- command.Start(ctx, ltx, argc, args, 1)
- commandFinished()
- }()
- case "cat":
- command := newReadCommand(h, omode.CatClient)
- go func() {
- command.Start(ctx, ltx, argc, args, 1)
- commandFinished()
- }()
- case "tail":
- command := newReadCommand(h, omode.TailClient)
- go func() {
- command.Start(ctx, ltx, argc, args, 10)
- commandFinished()
- }()
- case "map":
- command, aggregate, turboAggregate, err := newMapCommand(h, argc, args)
- if err != nil {
- h.sendln(h.serverMessages, err.Error())
- dlog.Server.Error(h.user, err)
- commandFinished()
- return
- }
- h.aggregate = aggregate
- h.turboAggregate = turboAggregate
+ handler, found := h.commands[commandName]
+ if !found {
+ h.sendln(h.serverMessages, dlog.Server.Error(h.user,
+ "Received unknown user command", commandName, argc, args))
+ commandFinished()
+ return
+ }
+
+ handler(ctx, ltx, argc, args, commandFinished)
+}
+
+func (h *ServerHandler) newCommandRegistry() map[string]commandHandler {
+ return map[string]commandHandler{
+ "grep": h.makeReadCommandHandler(omode.GrepClient, 1),
+ "cat": h.makeReadCommandHandler(omode.CatClient, 1),
+ "tail": h.makeReadCommandHandler(omode.TailClient, 10),
+ "map": h.handleMapCommand,
+ ".ack": h.handleAckUserCommand,
+ }
+}
+
+func (h *ServerHandler) makeReadCommandHandler(mode omode.Mode, tailBackoff int) commandHandler {
+ return func(ctx context.Context, ltx lcontext.LContext, argc int, args []string, commandFinished func()) {
+ command := newReadCommand(h, mode)
go func() {
- command.Start(ctx, h.maprMessages)
+ command.Start(ctx, ltx, argc, args, tailBackoff)
commandFinished()
}()
- case ".ack":
- h.handleAckCommand(argc, args)
- commandFinished()
- default:
- h.sendln(h.serverMessages, dlog.Server.Error(h.user,
- "Received unknown user command", commandName, argc, args))
+ }
+}
+
+func (h *ServerHandler) handleMapCommand(ctx context.Context, _ lcontext.LContext, argc int, args []string, commandFinished func()) {
+ command, aggregate, turboAggregate, err := newMapCommand(h, argc, args)
+ if err != nil {
+ h.sendln(h.serverMessages, err.Error())
+ dlog.Server.Error(h.user, err)
commandFinished()
+ return
}
+
+ h.aggregate = aggregate
+ h.turboAggregate = turboAggregate
+ go func() {
+ command.Start(ctx, h.maprMessages)
+ commandFinished()
+ }()
+}
+
+func (h *ServerHandler) handleAckUserCommand(_ context.Context, _ lcontext.LContext, argc int, args []string, commandFinished func()) {
+ h.handleAckCommand(argc, args)
+ commandFinished()
}