mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-21 21:06:42 +02:00
refactor(services): rename Go services, remove -go suffix
mana-search-go → mana-search mana-notify-go → mana-notify mana-crawler-go → mana-crawler mana-api-gateway-go → mana-api-gateway Legacy NestJS versions are deleted, suffix no longer needed. Updated all references in docker-compose, CLAUDE.md, package.json, Forgejo workflows, and service package.json files. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
79080d6654
commit
7e931b1c6d
90 changed files with 41 additions and 38 deletions
132
services/mana-api-gateway/internal/handler/apikeys.go
Normal file
132
services/mana-api-gateway/internal/handler/apikeys.go
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/manacore/mana-api-gateway/internal/middleware"
|
||||
"github.com/manacore/mana-api-gateway/internal/service"
|
||||
)
|
||||
|
||||
// ApiKeysHandler handles API key management endpoints.
|
||||
type ApiKeysHandler struct {
|
||||
apiKeyService *service.ApiKeyService
|
||||
usageService *service.UsageService
|
||||
}
|
||||
|
||||
// NewApiKeysHandler creates a new handler.
|
||||
func NewApiKeysHandler(apiKeySvc *service.ApiKeyService, usageSvc *service.UsageService) *ApiKeysHandler {
|
||||
return &ApiKeysHandler{apiKeyService: apiKeySvc, usageService: usageSvc}
|
||||
}
|
||||
|
||||
// CreateKey handles POST /api-keys
|
||||
func (h *ApiKeysHandler) CreateKey(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r)
|
||||
if userID == "" {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "not authenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Tier string `json:"tier"`
|
||||
IsTest bool `json:"isTest"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if body.Name == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "name is required"})
|
||||
return
|
||||
}
|
||||
|
||||
if body.Tier == "" {
|
||||
body.Tier = "free"
|
||||
}
|
||||
|
||||
rawKey, apiKey, err := h.apiKeyService.Create(r.Context(), userID, body.Name, body.Description, body.Tier, body.IsTest)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to create key"})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, map[string]any{
|
||||
"key": rawKey,
|
||||
"apiKey": apiKey,
|
||||
})
|
||||
}
|
||||
|
||||
// ListKeys handles GET /api-keys
|
||||
func (h *ApiKeysHandler) ListKeys(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r)
|
||||
if userID == "" {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "not authenticated"})
|
||||
return
|
||||
}
|
||||
|
||||
keys, err := h.apiKeyService.ListByUser(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to list keys"})
|
||||
return
|
||||
}
|
||||
|
||||
if keys == nil {
|
||||
keys = []service.ApiKey{}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, keys)
|
||||
}
|
||||
|
||||
// DeleteKey handles DELETE /api-keys/{id}
|
||||
func (h *ApiKeysHandler) DeleteKey(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r)
|
||||
keyID := r.PathValue("id")
|
||||
|
||||
if err := h.apiKeyService.Delete(r.Context(), keyID, userID); err != nil {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "key not found"})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]string{"message": "key deleted"})
|
||||
}
|
||||
|
||||
// GetUsage handles GET /api-keys/{id}/usage
|
||||
func (h *ApiKeysHandler) GetUsage(w http.ResponseWriter, r *http.Request) {
|
||||
keyID := r.PathValue("id")
|
||||
|
||||
usage, err := h.usageService.GetDailyUsage(r.Context(), keyID, 30)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to get usage"})
|
||||
return
|
||||
}
|
||||
|
||||
if usage == nil {
|
||||
usage = []service.DailyUsage{}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, usage)
|
||||
}
|
||||
|
||||
// GetUsageSummary handles GET /api-keys/{id}/usage/summary
|
||||
func (h *ApiKeysHandler) GetUsageSummary(w http.ResponseWriter, r *http.Request) {
|
||||
keyID := r.PathValue("id")
|
||||
since := time.Now().AddDate(0, -1, 0) // last 30 days
|
||||
|
||||
summary, err := h.usageService.GetSummary(r.Context(), keyID, since)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to get summary"})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, summary)
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, data any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
69
services/mana-api-gateway/internal/handler/health.go
Normal file
69
services/mana-api-gateway/internal/handler/health.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// HealthHandler handles health and metrics endpoints.
|
||||
type HealthHandler struct {
|
||||
pool *pgxpool.Pool
|
||||
redis *redis.Client
|
||||
startTime time.Time
|
||||
}
|
||||
|
||||
// NewHealthHandler creates a new health handler.
|
||||
func NewHealthHandler(pool *pgxpool.Pool, rdb *redis.Client) *HealthHandler {
|
||||
return &HealthHandler{pool: pool, redis: rdb, startTime: time.Now()}
|
||||
}
|
||||
|
||||
// Health handles GET /health
|
||||
func (h *HealthHandler) Health(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
dbOK := "ok"
|
||||
if err := h.pool.Ping(ctx); err != nil {
|
||||
dbOK = "error"
|
||||
}
|
||||
|
||||
redisOK := "ok"
|
||||
if err := h.redis.Ping(ctx).Err(); err != nil {
|
||||
redisOK = "error"
|
||||
}
|
||||
|
||||
status := "ok"
|
||||
if dbOK != "ok" || redisOK != "ok" {
|
||||
status = "degraded"
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"status": status,
|
||||
"service": "mana-api-gateway",
|
||||
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
||||
"uptime": time.Since(h.startTime).Seconds(),
|
||||
"database": dbOK,
|
||||
"redis": redisOK,
|
||||
})
|
||||
}
|
||||
|
||||
// Metrics handles GET /metrics (Prometheus format)
|
||||
func (h *HealthHandler) Metrics(w http.ResponseWriter, r *http.Request) {
|
||||
uptime := time.Since(h.startTime).Seconds()
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintf(w, "# HELP mana_api_gateway_uptime_seconds Gateway uptime\n")
|
||||
fmt.Fprintf(w, "# TYPE mana_api_gateway_uptime_seconds gauge\n")
|
||||
fmt.Fprintf(w, "mana_api_gateway_uptime_seconds %.0f\n", uptime)
|
||||
|
||||
// DB pool stats
|
||||
stats := h.pool.Stat()
|
||||
fmt.Fprintf(w, "# HELP mana_api_gateway_db_connections Database connection pool\n")
|
||||
fmt.Fprintf(w, "# TYPE mana_api_gateway_db_connections gauge\n")
|
||||
fmt.Fprintf(w, "mana_api_gateway_db_connections{state=\"total\"} %d\n", stats.TotalConns())
|
||||
fmt.Fprintf(w, "mana_api_gateway_db_connections{state=\"idle\"} %d\n", stats.IdleConns())
|
||||
fmt.Fprintf(w, "mana_api_gateway_db_connections{state=\"acquired\"} %d\n", stats.AcquiredConns())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue