2026-03-19 13:48:19 -05:00
|
|
|
import 'dotenv/config';
|
2026-03-19 05:42:11 +00:00
|
|
|
import express from 'express';
|
|
|
|
|
import cors from 'cors';
|
|
|
|
|
import { toNodeHandler } from 'better-auth/node';
|
|
|
|
|
import { auth } from './auth/auth';
|
|
|
|
|
import { authRateLimiter, apiRateLimiter } from './auth/rateLimiter';
|
|
|
|
|
import chemicalsRouter from './routes/chemicals';
|
|
|
|
|
import protocolsRouter from './routes/protocols';
|
2026-03-19 19:59:01 -05:00
|
|
|
import profileRouter from './routes/profile';
|
2026-03-19 05:42:11 +00:00
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
const PORT = process.env.PORT || 3001;
|
|
|
|
|
const UPLOADS_DIR = process.env.UPLOADS_DIR || path.join(__dirname, '../uploads');
|
|
|
|
|
|
|
|
|
|
// Trust Cloudflare/proxy X-Forwarded-For headers
|
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
|
|
|
|
|
|
app.use(cors({
|
|
|
|
|
origin: [
|
|
|
|
|
'http://localhost:5173',
|
|
|
|
|
'https://labwise.wahwa.com',
|
|
|
|
|
],
|
|
|
|
|
credentials: true,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Serve uploaded files
|
|
|
|
|
app.use('/uploads', express.static(UPLOADS_DIR));
|
|
|
|
|
|
|
|
|
|
// Better Auth — must come before express.json() so it can read its own body
|
|
|
|
|
app.use('/api/auth/*', authRateLimiter);
|
|
|
|
|
app.all('/api/auth/*', toNodeHandler(auth));
|
|
|
|
|
|
|
|
|
|
// Body parsing for all other routes
|
|
|
|
|
app.use(express.json({ limit: '1mb' }));
|
|
|
|
|
|
|
|
|
|
// Application routes
|
|
|
|
|
app.use('/api', apiRateLimiter);
|
|
|
|
|
app.use('/api/chemicals', chemicalsRouter);
|
|
|
|
|
app.use('/api/protocols', protocolsRouter);
|
2026-03-19 19:59:01 -05:00
|
|
|
app.use('/api/profile', profileRouter);
|
2026-03-19 05:42:11 +00:00
|
|
|
|
|
|
|
|
app.get('/api/health', (_req, res) => res.json({ ok: true }));
|
|
|
|
|
|
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
|
console.log(`LabWise API running on http://localhost:${PORT}`);
|
|
|
|
|
});
|