67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
import express, { NextFunction, Request, Response } from 'express';
|
|
import cors from 'cors';
|
|
import { uploadsDir } from './db/client';
|
|
import { logAuthStatus, requireAuth } from './auth';
|
|
import authRouter from './routes/auth';
|
|
import usersRouter from './routes/users';
|
|
import rabbitsRouter from './routes/rabbits';
|
|
import littersRouter from './routes/litters';
|
|
import eventsRouter from './routes/events';
|
|
import showEventsRouter from './routes/showEvents';
|
|
import showsRouter from './routes/shows';
|
|
import settingsRouter from './routes/settings';
|
|
import statsRouter from './routes/stats';
|
|
import exportRouter from './routes/export';
|
|
import importRouter from './routes/import';
|
|
|
|
const app = express();
|
|
const PORT = Number(process.env.PORT) || 3001;
|
|
|
|
// The client is served same-origin (Vite proxy in dev, nginx in prod), so CORS
|
|
// isn't needed there. Keep it for cross-origin dev tools, but reflect the
|
|
// request origin and allow credentials so session cookies still round-trip
|
|
// (a wildcard origin can't be combined with credentials).
|
|
app.use(cors({ origin: true, credentials: true }));
|
|
app.use(express.json());
|
|
|
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
console.log(`${req.method} ${req.path}`);
|
|
next();
|
|
});
|
|
|
|
app.get('/health', (req, res) => {
|
|
res.json({ status: 'ok' });
|
|
});
|
|
|
|
// Auth endpoints must stay open so the login screen can reach them.
|
|
app.use('/api/auth', authRouter);
|
|
|
|
// Everything past here requires a valid session.
|
|
app.use('/uploads', requireAuth, express.static(uploadsDir));
|
|
app.use('/api/users', requireAuth, usersRouter);
|
|
app.use('/api/rabbits', requireAuth, rabbitsRouter);
|
|
app.use('/api/litters', requireAuth, littersRouter);
|
|
app.use('/api/events', requireAuth, eventsRouter);
|
|
// Mounted before /api/shows so /api/shows/events/* resolves here, not to the
|
|
// ARBA browser router (which only defines /arba paths).
|
|
app.use('/api/shows/events', requireAuth, showEventsRouter);
|
|
app.use('/api/shows', requireAuth, showsRouter);
|
|
app.use('/api/settings', requireAuth, settingsRouter);
|
|
app.use('/api/stats', requireAuth, statsRouter);
|
|
app.use('/api/export', requireAuth, exportRouter);
|
|
app.use('/api/import', requireAuth, importRouter);
|
|
|
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
res.status(404).json({ error: 'Not found' });
|
|
});
|
|
|
|
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Server listening on port ${PORT}`);
|
|
logAuthStatus();
|
|
});
|