managarten/services/mana-credits/src/index.ts
Till JS 15deaf4e0a feat(services): create mana-credits service (Hono + Bun)
Extract the credit system from mana-core-auth into a standalone service.
Uses Hono framework on Bun runtime instead of NestJS.

Service includes:
- Personal credit balance with optimistic locking
- Immutable transaction ledger
- Stripe payment integration (PaymentIntents, Checkout Sessions)
- Guild shared pools with per-member spending limits
- Gift code system (simple, personalized, split, first_come, riddle)
- Service-to-service internal API (X-Service-Key auth)
- JWT validation via JWKS from mana-core-auth (jose library)

Architecture:
- 27 files, ~2.2k LOC (vs ~4.1k in NestJS)
- Drizzle ORM schemas adapted for standalone DB (no FK to auth tables)
- Zod validation instead of class-validator
- Manual service instantiation instead of NestJS DI
- Hono middleware for JWT + service key auth

Port: 3060
Database: mana_credits (separate from mana_auth)

Next steps: Update CreditClientService URL, update mana-core-auth
registration hooks, configure Docker + Cloudflare Tunnel.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 22:08:43 +01:00

82 lines
3 KiB
TypeScript

/**
* mana-credits — Standalone credit management service
*
* Hono + Bun runtime. Extracted from mana-core-auth.
* Handles: personal credits, guild pools, gift codes, Stripe payments.
*/
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { loadConfig } from './config';
import { getDb } from './db/connection';
import { errorHandler } from './middleware/error-handler';
import { jwtAuth } from './middleware/jwt-auth';
import { serviceAuth } from './middleware/service-auth';
import { StripeService } from './services/stripe';
import { GuildPoolService } from './services/guild-pool';
import { CreditsService } from './services/credits';
import { GiftCodeService } from './services/gift-code';
import { healthRoutes } from './routes/health';
import { createCreditsRoutes } from './routes/credits';
import { createGuildRoutes } from './routes/guild';
import { createGiftRoutes } from './routes/gifts';
import { createInternalRoutes } from './routes/internal';
import { createWebhookRoutes } from './routes/stripe-webhook';
// ─── Bootstrap ──────────────────────────────────────────────
const config = loadConfig();
const db = getDb(config.databaseUrl);
// Instantiate services (manual DI — no NestJS)
const stripeService = new StripeService(db, config.stripe.secretKey);
const guildPoolService = new GuildPoolService(db, config.manaAuthUrl, config.serviceKey);
const creditsService = new CreditsService(db, stripeService, guildPoolService);
const giftCodeService = new GiftCodeService(db, config.baseUrl);
// ─── App ────────────────────────────────────────────────────
const app = new Hono();
// Global middleware
app.onError(errorHandler);
app.use(
'*',
cors({
origin: config.cors.origins,
credentials: true,
})
);
// Health check (no auth)
app.route('/health', healthRoutes);
// User-facing routes (JWT auth)
app.use('/api/v1/credits/*', jwtAuth(config.manaAuthUrl));
app.route('/api/v1/credits', createCreditsRoutes(creditsService));
app.route('/api/v1/credits/guild', createGuildRoutes(guildPoolService));
// Gift routes (mixed: public GET /:code, JWT for rest)
app.route('/api/v1/gifts', createGiftRoutes(giftCodeService, config.manaAuthUrl));
// Service-to-service routes (X-Service-Key auth)
app.use('/api/v1/internal/*', serviceAuth(config.serviceKey));
app.route(
'/api/v1/internal',
createInternalRoutes(creditsService, giftCodeService, guildPoolService)
);
// Stripe webhooks (verified by signature, no auth middleware)
app.route(
'/api/v1/webhooks',
createWebhookRoutes(stripeService, creditsService, config.stripe.webhookSecret)
);
// ─── Start ──────────────────────────────────────────────────
console.log(`mana-credits starting on port ${config.port}...`);
export default {
port: config.port,
fetch: app.fetch,
};