-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
260 lines (237 loc) · 10 KB
/
app.ts
File metadata and controls
260 lines (237 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// app.ts — env must load first so CLOUDINARY_URL is valid before cloudinary SDK loads
import "./src/env";
import express, { Application, Request, Response, NextFunction } from "express";
import bodyParser from "body-parser";
import limiter from "./src/middleware/rateLimiter";
import appRouter from "./src/routes";
import cors from "cors";
import path from "path";
import fs from "fs";
import errorHandler from "./src/middleware/errorHandler";
import swaggerUi from "swagger-ui-express";
import swaggerJsDoc from "swagger-jsdoc";
import helmet from "helmet";
import { AppError } from "./src/errors";
// Define the server configuration
const port: number = parseInt(process.env.PORT || "5002", 10);
const hostname: string = process.env.DB_HOST || "localhost";
const isDev = process.env.NODE_ENV !== "production";
// Resolve project root so public/ works in both dev (__dirname = project root) and prod (__dirname = dist)
const projectRoot = __dirname.endsWith(path.sep + "dist") ? path.join(__dirname, "..") : __dirname;
const app: Application = express();
// Base URL for API docs (same origin as server)
const apiBaseUrl = process.env.API_BASE_URL || `http://localhost:${port}`;
// Swagger options — platform for farmers and customers
const doc: any = {
definition: {
openapi: "3.0.0",
info: {
title: "Farm Marketplace API",
version: "2.0.0",
description: `**Farm Marketplace API** is the central platform that connects **farmers** (sellers) with **customers** (buyers) for farm products.
**Who consumes this API**
This API is intended to be consumed by **client applications** only — for example, a **web app** or a **mobile app**. The API runs on the server; it does not run on or access mobile devices. Only clients that you build (web or mobile) can call this API.
## For Farmers
- Register as a farmer, list your products, set prices (per kg, per unit, wholesale).
- Manage product catalog, update/remove listings, and handle orders from buyers.
- Receive and manage orders, update dispatch details, and get buyer reviews.
## For Customers
- Browse and search products by name, category, and price range.
- Place orders, manage shipping addresses, and pay via integrated transactions.
- Leave reviews for farmers and track order status.
## Security (enforced in the API)
Security is enforced **in the code** on the server: protected routes require a valid JWT and ownership/role checks are applied where required.
- **Authentication**: JWT Bearer tokens; use \`Authorization: Bearer <token>\` for protected routes.
- **Authorization**: Role-based access (farmer vs buyer); resource ownership checks (e.g. only product owner can update/delete).
- **Rate limiting**: Global rate limit per IP to prevent abuse (see 429 responses).
- **Input validation**: Request bodies validated (e.g. Zod); invalid payloads return 400 with field errors.
- **Helmet**: Security headers (X-Content-Type-Options, X-Frame-Options, etc.) applied to all responses.
- **HTTPS**: Use HTTPS in production; tokens and credentials must not be sent over plain HTTP.`,
contact: {
name: "Farm Marketplace API",
},
},
servers: [
{ url: `${apiBaseUrl}/api/v2`, description: "Current server (development or production)" },
{ url: "http://localhost:5002/api/v2", description: "Local default (port 5002)" },
],
tags: [
{ name: "Authentication", description: "Sign up, login, JWT refresh — for both farmers and customers (OTP disabled; keeping auth simple)" },
{ name: "Products", description: "Product catalog — farmers list/sell; customers browse/search" },
{ name: "Orders", description: "Orders — customers place; farmers fulfill and dispatch" },
{ name: "Reviews", description: "Buyer reviews — customers review farmers/sellers" },
{ name: "User profile", description: "Profile and avatar — all authenticated users" },
{ name: "Payment Collection", description: "Payments — customers pay for orders" },
{ name: "Notifications", description: "Push notifications — optional for clients (e.g. web or mobile)" },
{ name: "Health", description: "Health check — API and database status for monitoring" },
],
components: {
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
description: "Obtain token via POST /auth/login. Send as: Authorization: Bearer <accessToken>",
},
},
schemas: {
Product: {
type: "object",
required: ["productName", "productCat", "priceType", "price"],
properties: {
productName: { type: "string" },
productCat: { type: "string" },
priceType: { type: "string" },
price: { type: "number" },
description: { type: "string" },
wholeSale: { type: "boolean" },
imageUrl: { type: "string" },
userId: { type: "string" },
},
example: {
productName: "Tomato",
productCat: "Vegetable",
priceType: "per kg",
price: 1000,
description: "Fresh tomatoes",
wholeSale: false,
imageUrl: "https://cloudinary.com/image.jpg",
userId: "uuid-1234"
}
},
},
},
security: [{ bearerAuth: [] }],
paths: {
"/health": {
get: {
tags: ["Health"],
summary: "Health check",
description: "Returns API and database status. Use for monitoring, load balancers, and uptime checks. No authentication required.",
operationId: "getHealth",
security: [],
responses: {
"200": {
description: "API and database are running",
content: {
"application/json": {
schema: {
type: "object",
properties: {
status: { type: "string", example: "success" },
message: { type: "string", example: "Farm Marketplace API is running" },
database: { type: "string", example: "connected" },
timestamp: { type: "string", format: "date-time" },
version: { type: "string", example: "2.0.0" },
},
},
},
},
},
"503": {
description: "API is up but database connection failed",
content: {
"application/json": {
schema: {
type: "object",
properties: {
status: { type: "string", example: "error" },
database: { type: "string", example: "disconnected" },
},
},
},
},
},
},
},
},
},
},
// In production, compiled routes live at dist/src/routes/*.js (tsc preserves src/ under outDir)
apis: isDev
? [path.join(projectRoot, "src", "routes", "*.ts")]
: [path.join(projectRoot, "dist", "src", "routes", "*.js")],
};
// Update this to '.ts' as files are converted to TypeScript
const swaggerSpec = swaggerJsDoc(doc);
// app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerJsDoc(doc)));
const imagesDir = path.join(projectRoot, "public", "assets", "images");
// Ensure the directory exists at app start
if (!fs.existsSync(imagesDir)) {
fs.mkdirSync(imagesDir, { recursive: true });
}
// Middleware setup
app.use(limiter);
app.set("trust proxy", 1);
// Allow Unsplash images (hero bg, cards, etc.) — default CSP blocks external img-src
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
imgSrc: ["'self'", "data:", "https://images.unsplash.com"],
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com"],
fontSrc: ["'self'", "https://fonts.gstatic.com"],
scriptSrc: ["'self'"],
connectSrc: ["'self'"],
},
},
})
);
app.use(bodyParser.json());
app.use(cors());
app.use("/public", express.static(path.join(projectRoot, "public")));
app.use("/public/assets", express.static(path.join(projectRoot, "public", "assets")));
app.use("/public/assets/images", express.static(path.join(projectRoot, "public", "assets", "images")));
// Routes setup
app.use("/api/v2", appRouter);
// @ts-ignore-next-line
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// Swagger default route definition
/**
* @swagger
* /:
* get:
* summary: Welcome message
* description: Returns a greeting message
* responses:
* 200:
* description: A simple greeting message.
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: "Hello World!! Farming products"
*/
app.get("/", (req: Request, res: Response) => {
res.sendFile(path.join(projectRoot, "public", "index.html"));
});
// 404 handler - serve custom 404 page for HTML requests, JSON for API requests
app.use((req: Request, res: Response, next: NextFunction) => {
// Check if the request is for an API endpoint or expects JSON
const isAPIRequest = req.path.startsWith('/api/v2') || req.get('Accept')?.includes('application/json');
if (isAPIRequest) {
// For API requests, return JSON error
next(new AppError(`Not Found - ${req.originalUrl}`, 404));
} else {
// For web requests, serve the custom 404 page
res.status(404).sendFile(path.join(projectRoot, "public", "404.html"));
}
});
// Error handling middleware - must be last
app.use(errorHandler);
// Only start the server if we're not in a test environment
if (process.env.NODE_ENV !== 'test') {
app.listen(port, async () => {
try {
console.log("✅ Database is up to date.");
} catch (error) {
console.error("❌ Unable to connect to the database:", error);
}
console.log(`✅ Server running on http://localhost:${port}`);
});
}
export default app;