Migrate from JSON to PostgreSQL, update SLA to 99.5%
- Replace JSON file storage with PostgreSQL (pg package) - Add db.ts service for connection pool and schema init - Rewrite keys.ts, verification.ts, usage.ts for async PostgreSQL - Update all routes for async function signatures - Add migration script (scripts/migrate-to-postgres.mjs) - Update docker-compose.yml with DATABASE_* env vars - Change SLA from 99.9% to 99.5% in landing page
This commit is contained in:
parent
bb1881af61
commit
e9d16bf2a3
13 changed files with 395 additions and 198 deletions
20
src/index.ts
20
src/index.ts
|
|
@ -11,19 +11,17 @@ import { recoverRouter } from "./routes/recover.js";
|
|||
import { billingRouter } from "./routes/billing.js";
|
||||
import { emailChangeRouter } from "./routes/email-change.js";
|
||||
import { authMiddleware } from "./middleware/auth.js";
|
||||
import { usageMiddleware } from "./middleware/usage.js";
|
||||
import { usageMiddleware, loadUsageData } from "./middleware/usage.js";
|
||||
import { getUsageStats } from "./middleware/usage.js";
|
||||
import { pdfRateLimitMiddleware, getConcurrencyStats } from "./middleware/pdfRateLimit.js";
|
||||
import { initBrowser, closeBrowser } from "./services/browser.js";
|
||||
import { loadKeys, getAllKeys } from "./services/keys.js";
|
||||
import { verifyToken } from "./services/verification.js";
|
||||
import { verifyToken, loadVerifications } from "./services/verification.js";
|
||||
import { initDatabase } from "./services/db.js";
|
||||
|
||||
const app = express();
|
||||
const PORT = parseInt(process.env.PORT || "3100", 10);
|
||||
|
||||
// Load API keys from persistent store
|
||||
loadKeys();
|
||||
|
||||
app.use(helmet({ crossOriginResourcePolicy: { policy: "cross-origin" } }));
|
||||
|
||||
// Differentiated CORS middleware
|
||||
|
|
@ -34,10 +32,8 @@ app.use((req, res, next) => {
|
|||
req.path.startsWith('/v1/email-change');
|
||||
|
||||
if (isAuthBillingRoute) {
|
||||
// Auth/billing routes: restrict to docfast.dev
|
||||
res.setHeader("Access-Control-Allow-Origin", "https://docfast.dev");
|
||||
} else {
|
||||
// Conversion API routes: allow all origins
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +56,7 @@ app.use(express.text({ limit: "2mb", type: "text/*" }));
|
|||
// Trust nginx proxy
|
||||
app.set("trust proxy", 1);
|
||||
|
||||
// Global rate limiting - reduced from 10,000 to reasonable limit
|
||||
// Global rate limiting
|
||||
const limiter = rateLimit({
|
||||
windowMs: 60_000,
|
||||
max: 100,
|
||||
|
|
@ -174,6 +170,14 @@ app.get("/api", (_req, res) => {
|
|||
});
|
||||
|
||||
async function start() {
|
||||
// Initialize PostgreSQL
|
||||
await initDatabase();
|
||||
|
||||
// Load data from PostgreSQL
|
||||
await loadKeys();
|
||||
await loadVerifications();
|
||||
await loadUsageData();
|
||||
|
||||
await initBrowser();
|
||||
console.log(`Loaded ${getAllKeys().length} API keys`);
|
||||
app.listen(PORT, () => console.log(`DocFast API running on :${PORT}`));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue