Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 22 additions & 30 deletions packages/recovery-system/backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Application, Request, Response, NextFunction } from "express";
import cors from "cors";
import helmet from "helmet";
import path from "path"; // ← ADD THIS
import { toNodeHandler } from "better-auth/node";
import { auth } from "./lib/auth";
import progressRoutes from "./routes/index";
Expand All @@ -13,80 +14,72 @@ import { getProfile, updateProfile, getProfileDetails, signUp } from "./controll
import chatRoutes from "./routes/chat.routes";
import gamesRoutes from "./routes/games.routes";
import resourceRoutes from "./routes/resource.routes";
//import analyzer from 'express-api-timer';
// import apisnap from '@umeshindu222/apisnap';




const app: Application = express();

app.use(express.json());
//app.use(analyzer.monitor({ slowThreshold: 300 }));

// CORS Configuration
// TODO: In production, change 'origin' to specific domain (e.g., 'https://relife.com')
// and use process.env.FRONTEND_URL instead of hardcoded localhost
// ── Static file serving for local image uploads ──────────────────────────────
// Images uploaded when Cloudinary is NOT configured are saved to /uploads.
// This middleware serves them at http://localhost:5000/uploads/<filename>
// so the Next.js frontend (port 3000) can load them with an absolute URL.
app.use('/uploads', express.static(path.join(process.cwd(), 'uploads')));

// ── CORS ──────────────────────────────────────────────────────────────────────
app.use(cors({
origin: process.env.NODE_ENV === 'production'
? process.env.FRONTEND_URL
: true, // Development - allow all (for Postman testing), restricted in production
: true,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));

app.use(helmet());

// Custom auth routes (MUST be before BetterAuth catch-all)
// ── Custom auth routes (MUST be before BetterAuth catch-all) ─────────────────
app.get("/api/auth/me", isAuth, getProfile);
app.put("/api/auth/profile", isAuth, updateProfile);
app.get("/api/auth/profile/details", isAuth, getProfileDetails);
app.post("/api/auth/register", signUp);


// Auth routes (BetterAuth catch-all)
// ── Auth routes (BetterAuth catch-all) ───────────────────────────────────────
app.all("/api/auth/*", (req, res) => toNodeHandler(auth)(req, res));

app.get("/", (_req: Request, res: Response) => {
res.status(200).send("Re-Life API is running...");
});

// Progress Tracking Routes
// ── Progress Tracking Routes ──────────────────────────────────────────────────
app.use('/api', progressRoutes);

// Journal Routes
// ── Journal Routes ────────────────────────────────────────────────────────────
app.use('/api', journalRoutes);

// Community Routes
// ── Community Routes ──────────────────────────────────────────────────────────
app.use('/api/community', communityRoutes);

// Counselor Routes
// ── Counselor Routes ──────────────────────────────────────────────────────────
app.use('/api/counselor', counselorRoutes);

// Booking Routes
// ── Booking Routes ────────────────────────────────────────────────────────────
app.use('/api', bookingRoutes);

// Chat Routes
// ── Chat Routes ───────────────────────────────────────────────────────────────
app.use('/api/chat', chatRoutes);

// Games Routes
// ── Games Routes ─────────────────────────────────────────────────────────────
app.use('/api/games', gamesRoutes);

// Resource save Routes
// ── Resource Routes ───────────────────────────────────────────────────────────
app.use('/api/resources', resourceRoutes);

// apisnap.init(app);

// 404 Handler
// ── 404 Handler ───────────────────────────────────────────────────────────────
app.use((req: Request, res: Response) => {
res.status(404).json({
message: 'Route not found',
path: req.path
});
res.status(404).json({ message: 'Route not found', path: req.path });
});

// Global Error Handler
// ── Global Error Handler ──────────────────────────────────────────────────────
app.use((err: any, _req: Request, res: Response, _next: NextFunction) => {
console.error('Error:', err);
res.status(err.status || 500).json({
Expand All @@ -95,5 +88,4 @@ app.use((err: any, _req: Request, res: Response, _next: NextFunction) => {
});
});


export default app;
Loading
Loading