import type { Express, Request, Response } from "express"; import { createServer } from "node:http"; import type { Server } from "node:http"; import session from "express-session"; import MemoryStore from "memorystore"; import { SYMBOLS, computeExpirations, computeGexProfile, computeScreener, computeSummary, } from "./marketData"; import { oratsClient } from "./oratsClient"; import { registerAuthRoutes } from "./authRoutes"; const SessionMemoryStore = MemoryStore(session); const KNOWN_TICKERS = new Set(SYMBOLS.map((s) => s.ticker)); function withTicker(req: Request, res: Response, fn: (ticker: string) => unknown) { const raw = String(req.params.symbol || "").toUpperCase(); if (!KNOWN_TICKERS.has(raw)) { res.status(404).json({ error: `Unknown symbol: ${raw}` }); return; } try { res.json(fn(raw)); } catch (err) { res.status(500).json({ error: (err as Error).message }); } } export async function registerRoutes(httpServer: Server, app: Express): Promise { // Session middleware app.use(session({ store: new SessionMemoryStore({ checkPeriod: 86400000 }), secret: process.env.SESSION_SECRET || "gammadesk-dev-secret-change-me", resave: false, saveUninitialized: false, cookie: { secure: false, // Set true in production with HTTPS maxAge: 24 * 60 * 60 * 1000, // 24 hours }, })); // Auth routes registerAuthRoutes(app); app.get("/api/symbols", (_req, res) => { res.json(SYMBOLS); }); app.get("/api/orats/status", (_req, res) => { res.json({ configured: oratsClient.isConfigured(), baseUrl: "https://api.orats.io/datav2", }); }); app.get("/api/market/:symbol/summary", (req, res) => withTicker(req, res, (t) => computeSummary(t)), ); app.get("/api/market/:symbol/gex", (req, res) => withTicker(req, res, (t) => computeGexProfile(t)), ); app.get("/api/market/:symbol/expirations", (req, res) => withTicker(req, res, (t) => computeExpirations(t)), ); app.get("/api/screener", (_req, res) => { res.json(computeScreener()); }); return httpServer; }