mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 22:21:10 +02:00
Replace 21 separate NestJS Matrix bot processes (~2.1 GB RAM, ~4.2 GB Docker images) with a single Go binary using plugin architecture (8.6 MB binary, ~30 MB RAM). New services: - services/mana-matrix-bot/ — Go Matrix bot with 21 plugins (mautrix-go, Redis sessions) - services/mana-api-gateway-go/ — Go API gateway (rate limiting, API keys, credit billing) Deleted: - 21 services/matrix-*-bot/ directories - packages/bot-services/ and packages/matrix-bot-common/ - Legacy deploy scripts and CI build jobs Updated: - docker-compose.macmini.yml: new Go services, legacy bots removed - CI/CD: change detection + build jobs for Go services - Root package.json: new dev:matrix, build:matrix, test:matrix scripts Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package plugin
|
|
|
|
import "strings"
|
|
|
|
// CommandRouter routes !command messages to the right handler.
|
|
type CommandRouter struct {
|
|
routes []route
|
|
}
|
|
|
|
type route struct {
|
|
pattern string
|
|
handler CommandHandler
|
|
}
|
|
|
|
// CommandHandler processes a command with its arguments.
|
|
type CommandHandler func(ctx *MessageContext, args string) error
|
|
|
|
// NewCommandRouter creates an empty command router.
|
|
func NewCommandRouter() *CommandRouter {
|
|
return &CommandRouter{}
|
|
}
|
|
|
|
// Handle registers a command pattern (e.g., "!todo") with a handler.
|
|
func (r *CommandRouter) Handle(pattern string, handler CommandHandler) {
|
|
r.routes = append(r.routes, route{pattern: strings.ToLower(pattern), handler: handler})
|
|
}
|
|
|
|
// Route attempts to match a message to a registered command.
|
|
// Returns true if a command was matched and handled.
|
|
func (r *CommandRouter) Route(mc *MessageContext) (bool, error) {
|
|
lower := strings.ToLower(mc.Body)
|
|
|
|
for _, rt := range r.routes {
|
|
if lower == rt.pattern {
|
|
return true, rt.handler(mc, "")
|
|
}
|
|
if strings.HasPrefix(lower, rt.pattern+" ") {
|
|
args := strings.TrimSpace(mc.Body[len(rt.pattern):])
|
|
return true, rt.handler(mc, args)
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|