managarten/services/mana-matrix-bot/internal/services/backend.go
Till JS 819568c3df feat(infra): consolidate 21 Matrix bots into Go binary + add Go API gateway
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>
2026-03-27 21:03:00 +01:00

154 lines
3.7 KiB
Go

package services
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// BackendClient is a generic HTTP client for calling NestJS backends.
type BackendClient struct {
baseURL string
httpClient *http.Client
}
// NewBackendClient creates a new backend client.
func NewBackendClient(baseURL string) *BackendClient {
return &BackendClient{
baseURL: baseURL,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// Get sends a GET request and decodes the JSON response.
func (c *BackendClient) Get(ctx context.Context, path string, token string, result any) error {
req, err := http.NewRequestWithContext(ctx, "GET", c.baseURL+path, nil)
if err != nil {
return err
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("backend GET %s: %w", path, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("backend GET %s: %d %s", path, resp.StatusCode, string(body))
}
if result != nil {
return json.NewDecoder(resp.Body).Decode(result)
}
return nil
}
// Post sends a POST request with a JSON body and decodes the response.
func (c *BackendClient) Post(ctx context.Context, path string, token string, body any, result any) error {
var reqBody io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshal body: %w", err)
}
reqBody = bytes.NewReader(data)
}
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+path, reqBody)
if err != nil {
return err
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("backend POST %s: %w", path, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("backend POST %s: %d %s", path, resp.StatusCode, string(body))
}
if result != nil {
return json.NewDecoder(resp.Body).Decode(result)
}
return nil
}
// Put sends a PUT request with a JSON body.
func (c *BackendClient) Put(ctx context.Context, path string, token string, body any, result any) error {
var reqBody io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshal body: %w", err)
}
reqBody = bytes.NewReader(data)
}
req, err := http.NewRequestWithContext(ctx, "PUT", c.baseURL+path, reqBody)
if err != nil {
return err
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("backend PUT %s: %w", path, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
respBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("backend PUT %s: %d %s", path, resp.StatusCode, string(respBody))
}
if result != nil {
return json.NewDecoder(resp.Body).Decode(result)
}
return nil
}
// Delete sends a DELETE request.
func (c *BackendClient) Delete(ctx context.Context, path string, token string) error {
req, err := http.NewRequestWithContext(ctx, "DELETE", c.baseURL+path, nil)
if err != nil {
return err
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("backend DELETE %s: %w", path, err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("backend DELETE %s: %d %s", path, resp.StatusCode, string(body))
}
return nil
}