feat(apps): create Hono compute servers for Traces, Planta, NutriPhi

Add lightweight Hono + Bun servers for server-only compute endpoints.
CRUD is handled by mana-sync, these handle AI + file upload only.

Traces: AI guide generation, location sync (Port 3026)
Planta: Photo upload (S3), AI plant analysis (Port 3022)
NutriPhi: AI meal analysis (photo+text), recommendations (Port 3023)

Each uses @manacore/shared-hono for auth/health/errors. ~100-200 LOC.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-03-28 16:16:57 +01:00
parent 4d26196590
commit d3d11e661d
30 changed files with 1161 additions and 221 deletions

View file

@ -3,6 +3,7 @@ module github.com/manacore/mana-notify
go 1.25.0
require (
github.com/manacore/shared-go v0.0.0
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/jackc/pgx/v5 v5.9.1
github.com/prometheus/client_golang v1.22.0
@ -24,3 +25,5 @@ require (
golang.org/x/text v0.29.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
)
replace github.com/manacore/shared-go => ../../packages/shared-go

View file

@ -1,9 +1,7 @@
package config
import (
"os"
"strconv"
"strings"
"github.com/manacore/shared-go/envutil"
)
type Config struct {
@ -18,7 +16,7 @@ type Config struct {
RedisPassword string
// Auth
ServiceKey string
ServiceKey string
ManaCoreAuthURL string
// SMTP (Brevo)
@ -45,54 +43,31 @@ type Config struct {
func Load() *Config {
return &Config{
Port: getEnvInt("PORT", 3040),
Port: envutil.GetInt("PORT", 3040),
DatabaseURL: getEnv("DATABASE_URL", "postgresql://manacore:manacore@localhost:5432/mana_notify"),
DatabaseURL: envutil.Get("DATABASE_URL", "postgresql://manacore:manacore@localhost:5432/mana_notify"),
RedisHost: getEnv("REDIS_HOST", "localhost"),
RedisPort: getEnvInt("REDIS_PORT", 6379),
RedisPassword: getEnv("REDIS_PASSWORD", ""),
RedisHost: envutil.Get("REDIS_HOST", "localhost"),
RedisPort: envutil.GetInt("REDIS_PORT", 6379),
RedisPassword: envutil.Get("REDIS_PASSWORD", ""),
ServiceKey: getEnv("SERVICE_KEY", "dev-service-key"),
ManaCoreAuthURL: getEnv("MANA_CORE_AUTH_URL", "http://localhost:3001"),
ServiceKey: envutil.Get("SERVICE_KEY", "dev-service-key"),
ManaCoreAuthURL: envutil.Get("MANA_CORE_AUTH_URL", "http://localhost:3001"),
SMTPHost: getEnv("SMTP_HOST", "smtp-relay.brevo.com"),
SMTPPort: getEnvInt("SMTP_PORT", 587),
SMTPUser: getEnv("SMTP_USER", ""),
SMTPPassword: getEnv("SMTP_PASSWORD", ""),
SMTPFrom: getEnv("SMTP_FROM", "ManaCore <noreply@mana.how>"),
SMTPHost: envutil.Get("SMTP_HOST", "smtp-relay.brevo.com"),
SMTPPort: envutil.GetInt("SMTP_PORT", 587),
SMTPUser: envutil.Get("SMTP_USER", ""),
SMTPPassword: envutil.Get("SMTP_PASSWORD", ""),
SMTPFrom: envutil.Get("SMTP_FROM", "ManaCore <noreply@mana.how>"),
ExpoAccessToken: getEnv("EXPO_ACCESS_TOKEN", ""),
ExpoAccessToken: envutil.Get("EXPO_ACCESS_TOKEN", ""),
MatrixHomeserverURL: getEnv("MATRIX_HOMESERVER_URL", ""),
MatrixAccessToken: getEnv("MATRIX_ACCESS_TOKEN", ""),
MatrixHomeserverURL: envutil.Get("MATRIX_HOMESERVER_URL", ""),
MatrixAccessToken: envutil.Get("MATRIX_ACCESS_TOKEN", ""),
RateLimitEmailPerMinute: getEnvInt("RATE_LIMIT_EMAIL_PER_MINUTE", 10),
RateLimitPushPerMinute: getEnvInt("RATE_LIMIT_PUSH_PER_MINUTE", 100),
RateLimitEmailPerMinute: envutil.GetInt("RATE_LIMIT_EMAIL_PER_MINUTE", 10),
RateLimitPushPerMinute: envutil.GetInt("RATE_LIMIT_PUSH_PER_MINUTE", 100),
CORSOrigins: getEnvSlice("CORS_ORIGINS", []string{"http://localhost:3000", "http://localhost:5173"}),
CORSOrigins: envutil.GetSlice("CORS_ORIGINS", []string{"http://localhost:3000", "http://localhost:5173"}),
}
}
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func getEnvInt(key string, fallback int) int {
if v := os.Getenv(key); v != "" {
if i, err := strconv.Atoi(v); err == nil {
return i
}
}
return fallback
}
func getEnvSlice(key string, fallback []string) []string {
if v := os.Getenv(key); v != "" {
return strings.Split(v, ",")
}
return fallback
}

View file

@ -1,24 +0,0 @@
package handler
import (
"encoding/json"
"net/http"
"time"
)
func writeJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(data)
}
func writeError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, map[string]any{
"success": false,
"error": map[string]any{
"statusCode": status,
"message": message,
"timestamp": time.Now().UTC().Format(time.RFC3339),
},
})
}

View file

@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"
"github.com/manacore/shared-go/httputil"
"github.com/manacore/mana-notify/internal/auth"
"github.com/manacore/mana-notify/internal/db"
)
@ -20,7 +22,7 @@ func NewDevicesHandler(database *db.DB) *DevicesHandler {
func (h *DevicesHandler) Register(w http.ResponseWriter, r *http.Request) {
user := auth.GetUser(r)
if user == nil {
writeError(w, http.StatusUnauthorized, "unauthorized")
httputil.WriteError(w, http.StatusUnauthorized, "unauthorized")
return
}
@ -32,12 +34,12 @@ func (h *DevicesHandler) Register(w http.ResponseWriter, r *http.Request) {
AppID string `json:"appId,omitempty"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.PushToken == "" {
writeError(w, http.StatusBadRequest, "pushToken is required")
httputil.WriteError(w, http.StatusBadRequest, "pushToken is required")
return
}
@ -60,18 +62,18 @@ func (h *DevicesHandler) Register(w http.ResponseWriter, r *http.Request) {
user.UserID, req.PushToken, tokenType, nilIfEmpty(req.Platform), nilIfEmpty(req.DeviceName), nilIfEmpty(req.AppID),
).Scan(&id)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to register device")
httputil.WriteError(w, http.StatusInternalServerError, "failed to register device")
return
}
writeJSON(w, http.StatusCreated, map[string]any{"device": map[string]any{"id": id}})
httputil.WriteJSON(w, http.StatusCreated, map[string]any{"device": map[string]any{"id": id}})
}
// List handles GET /api/v1/devices
func (h *DevicesHandler) List(w http.ResponseWriter, r *http.Request) {
user := auth.GetUser(r)
if user == nil {
writeError(w, http.StatusUnauthorized, "unauthorized")
httputil.WriteError(w, http.StatusUnauthorized, "unauthorized")
return
}
@ -79,7 +81,7 @@ func (h *DevicesHandler) List(w http.ResponseWriter, r *http.Request) {
`SELECT id, user_id, push_token, token_type, platform, device_name, app_id, is_active, last_seen_at, created_at, updated_at
FROM notify.devices WHERE user_id = $1 AND is_active = true ORDER BY created_at DESC`, user.UserID)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to list devices")
httputil.WriteError(w, http.StatusInternalServerError, "failed to list devices")
return
}
defer rows.Close()
@ -94,14 +96,14 @@ func (h *DevicesHandler) List(w http.ResponseWriter, r *http.Request) {
devices = append(devices, d)
}
writeJSON(w, http.StatusOK, map[string]any{"devices": devices})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"devices": devices})
}
// Delete handles DELETE /api/v1/devices/{id}
func (h *DevicesHandler) Delete(w http.ResponseWriter, r *http.Request) {
user := auth.GetUser(r)
if user == nil {
writeError(w, http.StatusUnauthorized, "unauthorized")
httputil.WriteError(w, http.StatusUnauthorized, "unauthorized")
return
}
@ -109,13 +111,13 @@ func (h *DevicesHandler) Delete(w http.ResponseWriter, r *http.Request) {
result, err := h.db.Pool.Exec(r.Context(),
`UPDATE notify.devices SET is_active = false, updated_at = NOW() WHERE id = $1 AND user_id = $2`, id, user.UserID)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to delete device")
httputil.WriteError(w, http.StatusInternalServerError, "failed to delete device")
return
}
if result.RowsAffected() == 0 {
writeError(w, http.StatusNotFound, "device not found")
httputil.WriteError(w, http.StatusNotFound, "device not found")
return
}
writeJSON(w, http.StatusOK, map[string]any{"deleted": true})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"deleted": true})
}

View file

@ -2,6 +2,8 @@ package handler
import (
"net/http"
"github.com/manacore/shared-go/httputil"
"time"
"github.com/manacore/mana-notify/internal/db"
@ -24,7 +26,7 @@ func (h *HealthHandler) Health(w http.ResponseWriter, r *http.Request) {
status = "unhealthy"
}
writeJSON(w, http.StatusOK, map[string]any{
httputil.WriteJSON(w, http.StatusOK, map[string]any{
"status": status,
"version": "1.0.0",
"service": "mana-notify",

View file

@ -6,6 +6,8 @@ import (
"fmt"
"log/slog"
"net/http"
"github.com/manacore/shared-go/httputil"
"time"
"github.com/manacore/mana-notify/internal/db"
@ -77,12 +79,12 @@ type BatchRequest struct {
func (h *NotificationsHandler) Send(w http.ResponseWriter, r *http.Request) {
var req SendRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
if err := validateSendRequest(&req); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
httputil.WriteError(w, http.StatusBadRequest, err.Error())
return
}
@ -93,7 +95,7 @@ func (h *NotificationsHandler) Send(w http.ResponseWriter, r *http.Request) {
`SELECT id FROM notify.notifications WHERE external_id = $1`, req.ExternalID,
).Scan(&existingID)
if err == nil {
writeJSON(w, http.StatusOK, map[string]any{
httputil.WriteJSON(w, http.StatusOK, map[string]any{
"notification": map[string]any{"id": existingID, "status": "existing"},
"deduplicated": true,
})
@ -105,7 +107,7 @@ func (h *NotificationsHandler) Send(w http.ResponseWriter, r *http.Request) {
if req.UserID != "" {
blocked, reason := h.checkPreferences(r.Context(), req.UserID, req.Channel)
if blocked {
writeJSON(w, http.StatusOK, map[string]any{
httputil.WriteJSON(w, http.StatusOK, map[string]any{
"notification": map[string]any{"status": "cancelled", "reason": reason},
})
return
@ -146,7 +148,7 @@ func (h *NotificationsHandler) Send(w http.ResponseWriter, r *http.Request) {
).Scan(&notificationID)
if err != nil {
slog.Error("create notification failed", "error", err)
writeError(w, http.StatusInternalServerError, "failed to create notification")
httputil.WriteError(w, http.StatusInternalServerError, "failed to create notification")
return
}
@ -185,7 +187,7 @@ func (h *NotificationsHandler) Send(w http.ResponseWriter, r *http.Request) {
h.pool.Enqueue(job)
writeJSON(w, http.StatusAccepted, map[string]any{
httputil.WriteJSON(w, http.StatusAccepted, map[string]any{
"notification": map[string]any{
"id": notificationID,
"status": "pending",
@ -197,22 +199,22 @@ func (h *NotificationsHandler) Send(w http.ResponseWriter, r *http.Request) {
func (h *NotificationsHandler) Schedule(w http.ResponseWriter, r *http.Request) {
var req ScheduleRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
scheduledFor, err := time.Parse(time.RFC3339, req.ScheduledFor)
if err != nil {
writeError(w, http.StatusBadRequest, "scheduledFor must be a valid RFC3339 timestamp")
httputil.WriteError(w, http.StatusBadRequest, "scheduledFor must be a valid RFC3339 timestamp")
return
}
if scheduledFor.Before(time.Now()) {
writeError(w, http.StatusBadRequest, "scheduledFor must be in the future")
httputil.WriteError(w, http.StatusBadRequest, "scheduledFor must be in the future")
return
}
if err := validateSendRequest(&req.SendRequest); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
httputil.WriteError(w, http.StatusBadRequest, err.Error())
return
}
@ -246,7 +248,7 @@ func (h *NotificationsHandler) Schedule(w http.ResponseWriter, r *http.Request)
priority, nilIfEmpty(req.Recipient), nilIfEmpty(req.ExternalID), scheduledFor,
).Scan(&notificationID)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to create notification")
httputil.WriteError(w, http.StatusInternalServerError, "failed to create notification")
return
}
@ -262,7 +264,7 @@ func (h *NotificationsHandler) Schedule(w http.ResponseWriter, r *http.Request)
}
h.pool.Enqueue(job)
writeJSON(w, http.StatusAccepted, map[string]any{
httputil.WriteJSON(w, http.StatusAccepted, map[string]any{
"notification": map[string]any{
"id": notificationID,
"status": "pending",
@ -275,16 +277,16 @@ func (h *NotificationsHandler) Schedule(w http.ResponseWriter, r *http.Request)
func (h *NotificationsHandler) Batch(w http.ResponseWriter, r *http.Request) {
var req BatchRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 5<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
if len(req.Notifications) == 0 {
writeError(w, http.StatusBadRequest, "notifications array is required")
httputil.WriteError(w, http.StatusBadRequest, "notifications array is required")
return
}
if len(req.Notifications) > 100 {
writeError(w, http.StatusBadRequest, "maximum 100 notifications per batch")
httputil.WriteError(w, http.StatusBadRequest, "maximum 100 notifications per batch")
return
}
@ -352,7 +354,7 @@ func (h *NotificationsHandler) Batch(w http.ResponseWriter, r *http.Request) {
succeeded++
}
writeJSON(w, http.StatusAccepted, map[string]any{
httputil.WriteJSON(w, http.StatusAccepted, map[string]any{
"results": results,
"succeeded": succeeded,
"failed": failed,
@ -363,7 +365,7 @@ func (h *NotificationsHandler) Batch(w http.ResponseWriter, r *http.Request) {
func (h *NotificationsHandler) GetNotification(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "notification id required")
httputil.WriteError(w, http.StatusBadRequest, "notification id required")
return
}
@ -376,33 +378,33 @@ func (h *NotificationsHandler) GetNotification(w http.ResponseWriter, r *http.Re
&n.Attempts, &n.DeliveredAt, &n.ErrorMessage, &n.CreatedAt, &n.UpdatedAt)
if err != nil {
writeError(w, http.StatusNotFound, "notification not found")
httputil.WriteError(w, http.StatusNotFound, "notification not found")
return
}
writeJSON(w, http.StatusOK, map[string]any{"notification": n})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"notification": n})
}
// CancelNotification handles DELETE /api/v1/notifications/{id}
func (h *NotificationsHandler) CancelNotification(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "notification id required")
httputil.WriteError(w, http.StatusBadRequest, "notification id required")
return
}
result, err := h.db.Pool.Exec(r.Context(),
`UPDATE notify.notifications SET status = 'cancelled', updated_at = NOW() WHERE id = $1 AND status = 'pending'`, id)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to cancel notification")
httputil.WriteError(w, http.StatusInternalServerError, "failed to cancel notification")
return
}
if result.RowsAffected() == 0 {
writeError(w, http.StatusNotFound, "notification not found or already processed")
httputil.WriteError(w, http.StatusNotFound, "notification not found or already processed")
return
}
writeJSON(w, http.StatusOK, map[string]any{"cancelled": true})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"cancelled": true})
}
func (h *NotificationsHandler) checkPreferences(ctx context.Context, userID, ch string) (bool, string) {

View file

@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"
"github.com/manacore/shared-go/httputil"
"github.com/manacore/mana-notify/internal/auth"
"github.com/manacore/mana-notify/internal/db"
)
@ -20,7 +22,7 @@ func NewPreferencesHandler(database *db.DB) *PreferencesHandler {
func (h *PreferencesHandler) Get(w http.ResponseWriter, r *http.Request) {
user := auth.GetUser(r)
if user == nil {
writeError(w, http.StatusUnauthorized, "unauthorized")
httputil.WriteError(w, http.StatusUnauthorized, "unauthorized")
return
}
@ -33,7 +35,7 @@ func (h *PreferencesHandler) Get(w http.ResponseWriter, r *http.Request) {
if err != nil {
// Return defaults
writeJSON(w, http.StatusOK, map[string]any{
httputil.WriteJSON(w, http.StatusOK, map[string]any{
"preferences": map[string]any{
"emailEnabled": false,
"pushEnabled": true,
@ -44,14 +46,14 @@ func (h *PreferencesHandler) Get(w http.ResponseWriter, r *http.Request) {
return
}
writeJSON(w, http.StatusOK, map[string]any{"preferences": p})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"preferences": p})
}
// Update handles PUT /api/v1/preferences
func (h *PreferencesHandler) Update(w http.ResponseWriter, r *http.Request) {
user := auth.GetUser(r)
if user == nil {
writeError(w, http.StatusUnauthorized, "unauthorized")
httputil.WriteError(w, http.StatusUnauthorized, "unauthorized")
return
}
@ -65,7 +67,7 @@ func (h *PreferencesHandler) Update(w http.ResponseWriter, r *http.Request) {
CategoryPreferences map[string]any `json:"categoryPreferences,omitempty"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
@ -87,9 +89,9 @@ func (h *PreferencesHandler) Update(w http.ResponseWriter, r *http.Request) {
req.QuietHoursStart, req.QuietHoursEnd, req.Timezone, catJSON,
)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update preferences")
httputil.WriteError(w, http.StatusInternalServerError, "failed to update preferences")
return
}
writeJSON(w, http.StatusOK, map[string]any{"updated": true})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"updated": true})
}

View file

@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"
"github.com/manacore/shared-go/httputil"
"github.com/manacore/mana-notify/internal/db"
tmpl "github.com/manacore/mana-notify/internal/template"
)
@ -23,7 +25,7 @@ func (h *TemplatesHandler) List(w http.ResponseWriter, r *http.Request) {
`SELECT id, slug, app_id, channel, subject, body_template, locale, is_active, is_system, variables, created_at, updated_at
FROM notify.templates ORDER BY slug`)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to list templates")
httputil.WriteError(w, http.StatusInternalServerError, "failed to list templates")
return
}
defer rows.Close()
@ -38,7 +40,7 @@ func (h *TemplatesHandler) List(w http.ResponseWriter, r *http.Request) {
templates = append(templates, t)
}
writeJSON(w, http.StatusOK, map[string]any{"templates": templates})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"templates": templates})
}
// Get handles GET /api/v1/templates/{slug}
@ -56,11 +58,11 @@ func (h *TemplatesHandler) Get(w http.ResponseWriter, r *http.Request) {
).Scan(&t.ID, &t.Slug, &t.AppID, &t.Channel, &t.Subject, &t.BodyTemplate,
&t.Locale, &t.IsActive, &t.IsSystem, &t.Variables, &t.CreatedAt, &t.UpdatedAt)
if err != nil {
writeError(w, http.StatusNotFound, "template not found")
httputil.WriteError(w, http.StatusNotFound, "template not found")
return
}
writeJSON(w, http.StatusOK, map[string]any{"template": t})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"template": t})
}
// Create handles POST /api/v1/templates
@ -75,12 +77,12 @@ func (h *TemplatesHandler) Create(w http.ResponseWriter, r *http.Request) {
Variables any `json:"variables,omitempty"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.Slug == "" || req.Channel == "" || req.BodyTemplate == "" {
writeError(w, http.StatusBadRequest, "slug, channel, and bodyTemplate are required")
httputil.WriteError(w, http.StatusBadRequest, "slug, channel, and bodyTemplate are required")
return
}
if req.Locale == "" {
@ -96,11 +98,11 @@ func (h *TemplatesHandler) Create(w http.ResponseWriter, r *http.Request) {
req.Slug, nilIfEmpty(req.AppID), req.Channel, nilIfEmpty(req.Subject), req.BodyTemplate, req.Locale, varsJSON,
).Scan(&id)
if err != nil {
writeError(w, http.StatusConflict, "template already exists for this slug+locale")
httputil.WriteError(w, http.StatusConflict, "template already exists for this slug+locale")
return
}
writeJSON(w, http.StatusCreated, map[string]any{"id": id})
httputil.WriteJSON(w, http.StatusCreated, map[string]any{"id": id})
}
// Update handles PUT /api/v1/templates/{slug}
@ -118,7 +120,7 @@ func (h *TemplatesHandler) Update(w http.ResponseWriter, r *http.Request) {
Variables any `json:"variables,omitempty"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
@ -132,15 +134,15 @@ func (h *TemplatesHandler) Update(w http.ResponseWriter, r *http.Request) {
nilIfEmpty(req.Subject), nilIfEmpty(req.BodyTemplate), req.IsActive, slug, locale,
)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update template")
httputil.WriteError(w, http.StatusInternalServerError, "failed to update template")
return
}
if result.RowsAffected() == 0 {
writeError(w, http.StatusNotFound, "template not found or is a system template")
httputil.WriteError(w, http.StatusNotFound, "template not found or is a system template")
return
}
writeJSON(w, http.StatusOK, map[string]any{"updated": true})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"updated": true})
}
// Delete handles DELETE /api/v1/templates/{slug}
@ -150,15 +152,15 @@ func (h *TemplatesHandler) Delete(w http.ResponseWriter, r *http.Request) {
result, err := h.db.Pool.Exec(r.Context(),
`DELETE FROM notify.templates WHERE slug = $1 AND is_system = false`, slug)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to delete template")
httputil.WriteError(w, http.StatusInternalServerError, "failed to delete template")
return
}
if result.RowsAffected() == 0 {
writeError(w, http.StatusNotFound, "template not found or is a system template")
httputil.WriteError(w, http.StatusNotFound, "template not found or is a system template")
return
}
writeJSON(w, http.StatusOK, map[string]any{"deleted": true})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"deleted": true})
}
// Preview handles POST /api/v1/templates/{slug}/preview
@ -168,17 +170,17 @@ func (h *TemplatesHandler) Preview(w http.ResponseWriter, r *http.Request) {
Data map[string]any `json:"data"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
rendered, err := h.engine.RenderBySlug(r.Context(), slug, req.Data, "")
if err != nil {
writeError(w, http.StatusNotFound, "template not found")
httputil.WriteError(w, http.StatusNotFound, "template not found")
return
}
writeJSON(w, http.StatusOK, map[string]any{"subject": rendered.Subject, "body": rendered.Body})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"subject": rendered.Subject, "body": rendered.Body})
}
// PreviewCustom handles POST /api/v1/templates/preview
@ -189,7 +191,7 @@ func (h *TemplatesHandler) PreviewCustom(w http.ResponseWriter, r *http.Request)
Data map[string]any `json:"data"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
@ -197,7 +199,7 @@ func (h *TemplatesHandler) PreviewCustom(w http.ResponseWriter, r *http.Request)
if req.Subject != "" {
s, err := tmpl.RenderDirect(req.Subject, req.Data)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid subject template: "+err.Error())
httputil.WriteError(w, http.StatusBadRequest, "invalid subject template: "+err.Error())
return
}
subject = s
@ -205,9 +207,9 @@ func (h *TemplatesHandler) PreviewCustom(w http.ResponseWriter, r *http.Request)
body, err := tmpl.RenderDirect(req.BodyTemplate, req.Data)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid body template: "+err.Error())
httputil.WriteError(w, http.StatusBadRequest, "invalid body template: "+err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"subject": subject, "body": body})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"subject": subject, "body": body})
}

View file

@ -3,6 +3,7 @@ module github.com/manacore/mana-search
go 1.25.0
require (
github.com/manacore/shared-go v0.0.0
github.com/JohannesKaufmann/html-to-markdown/v2 v2.3.3
github.com/go-shiori/go-readability v0.0.0-20251205110129-5db1dc9836f0
github.com/prometheus/client_golang v1.22.0
@ -29,3 +30,5 @@ require (
golang.org/x/text v0.24.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
)
replace github.com/manacore/shared-go => ../../packages/shared-go

View file

@ -1,9 +1,7 @@
package config
import (
"os"
"strconv"
"strings"
"github.com/manacore/shared-go/envutil"
)
type Config struct {
@ -35,47 +33,24 @@ type Config struct {
func Load() *Config {
return &Config{
Port: getEnvInt("PORT", 3021),
Port: envutil.GetInt("PORT", 3021),
SearxngURL: getEnv("SEARXNG_URL", "http://localhost:8080"),
SearxngTimeout: getEnvInt("SEARXNG_TIMEOUT", 15000),
SearxngDefaultLanguage: getEnv("SEARXNG_DEFAULT_LANGUAGE", "de-DE"),
SearxngURL: envutil.Get("SEARXNG_URL", "http://localhost:8080"),
SearxngTimeout: envutil.GetInt("SEARXNG_TIMEOUT", 15000),
SearxngDefaultLanguage: envutil.Get("SEARXNG_DEFAULT_LANGUAGE", "de-DE"),
RedisHost: getEnv("REDIS_HOST", "localhost"),
RedisPort: getEnvInt("REDIS_PORT", 6379),
RedisPassword: getEnv("REDIS_PASSWORD", ""),
RedisHost: envutil.Get("REDIS_HOST", "localhost"),
RedisPort: envutil.GetInt("REDIS_PORT", 6379),
RedisPassword: envutil.Get("REDIS_PASSWORD", ""),
RedisPrefix: "mana-search:",
CacheSearchTTL: getEnvInt("CACHE_SEARCH_TTL", 3600),
CacheExtractTTL: getEnvInt("CACHE_EXTRACT_TTL", 86400),
CacheSearchTTL: envutil.GetInt("CACHE_SEARCH_TTL", 3600),
CacheExtractTTL: envutil.GetInt("CACHE_EXTRACT_TTL", 86400),
ExtractTimeout: getEnvInt("EXTRACT_TIMEOUT", 10000),
ExtractMaxLength: getEnvInt("EXTRACT_MAX_LENGTH", 50000),
ExtractUserAgent: getEnv("EXTRACT_USER_AGENT", "Mozilla/5.0 (compatible; ManaSearchBot/1.0; +https://mana.how)"),
ExtractTimeout: envutil.GetInt("EXTRACT_TIMEOUT", 10000),
ExtractMaxLength: envutil.GetInt("EXTRACT_MAX_LENGTH", 50000),
ExtractUserAgent: envutil.Get("EXTRACT_USER_AGENT", "Mozilla/5.0 (compatible; ManaSearchBot/1.0; +https://mana.how)"),
CORSOrigins: getEnvSlice("CORS_ORIGINS", []string{"http://localhost:3000", "http://localhost:5173", "http://localhost:8081"}),
CORSOrigins: envutil.GetSlice("CORS_ORIGINS", []string{"http://localhost:3000", "http://localhost:5173", "http://localhost:8081"}),
}
}
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func getEnvInt(key string, fallback int) int {
if v := os.Getenv(key); v != "" {
if i, err := strconv.Atoi(v); err == nil {
return i
}
}
return fallback
}
func getEnvSlice(key string, fallback []string) []string {
if v := os.Getenv(key); v != "" {
return strings.Split(v, ",")
}
return fallback
}

View file

@ -1,24 +0,0 @@
package handler
import (
"encoding/json"
"net/http"
"time"
)
func writeJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(data)
}
func writeError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, map[string]any{
"success": false,
"error": map[string]any{
"statusCode": status,
"message": message,
"timestamp": time.Now().UTC().Format(time.RFC3339),
},
})
}

View file

@ -3,6 +3,8 @@ package handler
import (
"encoding/json"
"net/http"
"github.com/manacore/shared-go/httputil"
"net/url"
"time"
@ -34,27 +36,27 @@ func (h *ExtractHandler) Extract(w http.ResponseWriter, r *http.Request) {
var req extract.ExtractRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.URL == "" {
writeError(w, http.StatusBadRequest, "url is required")
httputil.WriteError(w, http.StatusBadRequest, "url is required")
return
}
if _, err := url.ParseRequestURI(req.URL); err != nil {
writeError(w, http.StatusBadRequest, "url must be a valid URL")
httputil.WriteError(w, http.StatusBadRequest, "url must be a valid URL")
return
}
// Validate options
if req.Options != nil {
if req.Options.MaxLength > 0 && (req.Options.MaxLength < 100 || req.Options.MaxLength > 100000) {
writeError(w, http.StatusBadRequest, "maxLength must be between 100 and 100000")
httputil.WriteError(w, http.StatusBadRequest, "maxLength must be between 100 and 100000")
return
}
if req.Options.Timeout > 0 && (req.Options.Timeout < 1000 || req.Options.Timeout > 30000) {
writeError(w, http.StatusBadRequest, "timeout must be between 1000 and 30000")
httputil.WriteError(w, http.StatusBadRequest, "timeout must be between 1000 and 30000")
return
}
}
@ -68,7 +70,7 @@ func (h *ExtractHandler) Extract(w http.ResponseWriter, r *http.Request) {
cached.Meta.Cached = true
duration := time.Since(start).Seconds()
h.metrics.RecordRequest("extract", "200", duration)
writeJSON(w, http.StatusOK, cached)
httputil.WriteJSON(w, http.StatusOK, cached)
return
}
}
@ -89,7 +91,7 @@ func (h *ExtractHandler) Extract(w http.ResponseWriter, r *http.Request) {
duration := time.Since(start).Seconds()
h.metrics.RecordRequest("extract", status, duration)
writeJSON(w, http.StatusOK, resp)
httputil.WriteJSON(w, http.StatusOK, resp)
}
// BulkExtract handles POST /api/v1/extract/bulk
@ -98,22 +100,22 @@ func (h *ExtractHandler) BulkExtract(w http.ResponseWriter, r *http.Request) {
var req extract.BulkExtractRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
if len(req.URLs) == 0 {
writeError(w, http.StatusBadRequest, "urls is required")
httputil.WriteError(w, http.StatusBadRequest, "urls is required")
return
}
if len(req.URLs) > 20 {
writeError(w, http.StatusBadRequest, "maximum 20 URLs allowed")
httputil.WriteError(w, http.StatusBadRequest, "maximum 20 URLs allowed")
return
}
for _, u := range req.URLs {
if _, err := url.ParseRequestURI(u); err != nil {
writeError(w, http.StatusBadRequest, "invalid URL: "+u)
httputil.WriteError(w, http.StatusBadRequest, "invalid URL: "+u)
return
}
}
@ -123,5 +125,5 @@ func (h *ExtractHandler) BulkExtract(w http.ResponseWriter, r *http.Request) {
duration := time.Since(start).Seconds()
h.metrics.RecordRequest("extract_bulk", "200", duration)
writeJSON(w, http.StatusOK, resp)
httputil.WriteJSON(w, http.StatusOK, resp)
}

View file

@ -2,6 +2,8 @@ package handler
import (
"net/http"
"github.com/manacore/shared-go/httputil"
"time"
"github.com/manacore/mana-search/internal/cache"
@ -34,7 +36,7 @@ func (h *HealthHandler) Health(w http.ResponseWriter, r *http.Request) {
overall = "degraded"
}
writeJSON(w, http.StatusOK, map[string]any{
httputil.WriteJSON(w, http.StatusOK, map[string]any{
"status": overall,
"service": "mana-search",
"version": "1.0.0",

View file

@ -4,6 +4,8 @@ import (
"encoding/json"
"log/slog"
"net/http"
"github.com/manacore/shared-go/httputil"
"sort"
"time"
@ -37,23 +39,23 @@ func (h *SearchHandler) Search(w http.ResponseWriter, r *http.Request) {
var req search.SearchRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
httputil.WriteError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.Query == "" {
writeError(w, http.StatusBadRequest, "query is required")
httputil.WriteError(w, http.StatusBadRequest, "query is required")
return
}
// Validate options
if req.Options != nil {
if req.Options.Limit < 0 || req.Options.Limit > 50 {
writeError(w, http.StatusBadRequest, "limit must be between 1 and 50")
httputil.WriteError(w, http.StatusBadRequest, "limit must be between 1 and 50")
return
}
if req.Options.SafeSearch < 0 || req.Options.SafeSearch > 2 {
writeError(w, http.StatusBadRequest, "safeSearch must be 0, 1, or 2")
httputil.WriteError(w, http.StatusBadRequest, "safeSearch must be 0, 1, or 2")
return
}
}
@ -69,7 +71,7 @@ func (h *SearchHandler) Search(w http.ResponseWriter, r *http.Request) {
cached.Meta.CacheKey = cacheKey
duration := time.Since(start).Seconds()
h.metrics.RecordRequest("search", "200", duration)
writeJSON(w, http.StatusOK, cached)
httputil.WriteJSON(w, http.StatusOK, cached)
return
}
}
@ -81,7 +83,7 @@ func (h *SearchHandler) Search(w http.ResponseWriter, r *http.Request) {
slog.Error("search failed", "error", err, "query", req.Query)
duration := time.Since(start).Seconds()
h.metrics.RecordRequest("search", "502", duration)
writeError(w, http.StatusBadGateway, err.Error())
httputil.WriteError(w, http.StatusBadGateway, err.Error())
return
}
@ -119,13 +121,13 @@ func (h *SearchHandler) Search(w http.ResponseWriter, r *http.Request) {
duration := time.Since(start).Seconds()
h.metrics.RecordRequest("search", "200", duration)
writeJSON(w, http.StatusOK, resp)
httputil.WriteJSON(w, http.StatusOK, resp)
}
// Engines handles GET /api/v1/search/engines
func (h *SearchHandler) Engines(w http.ResponseWriter, r *http.Request) {
engines := h.provider.GetEngines(r.Context())
writeJSON(w, http.StatusOK, map[string]any{"engines": engines})
httputil.WriteJSON(w, http.StatusOK, map[string]any{"engines": engines})
}
// Health handles GET /api/v1/search/health
@ -134,7 +136,7 @@ func (h *SearchHandler) Health(w http.ResponseWriter, r *http.Request) {
redisHealth := h.cache.HealthCheck(r.Context())
cacheStats := h.cache.Stats()
writeJSON(w, http.StatusOK, map[string]any{
httputil.WriteJSON(w, http.StatusOK, map[string]any{
"searxng": map[string]any{
"status": sxStatus,
"latency": sxLatency,
@ -148,10 +150,10 @@ func (h *SearchHandler) Health(w http.ResponseWriter, r *http.Request) {
func (h *SearchHandler) ClearCache(w http.ResponseWriter, r *http.Request) {
deleted, err := h.cache.Clear(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to clear cache")
httputil.WriteError(w, http.StatusInternalServerError, "failed to clear cache")
return
}
writeJSON(w, http.StatusOK, map[string]any{
httputil.WriteJSON(w, http.StatusOK, map[string]any{
"cleared": true,
"keysRemoved": deleted,
})