-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.ts
More file actions
60 lines (48 loc) · 1.43 KB
/
app.ts
File metadata and controls
60 lines (48 loc) · 1.43 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
import express from "express";
import cors from "cors";
import multer from "multer";
import { json, urlencoded } from "body-parser";
import routes from "./routes";
import { errorHandler } from "./utils/apiError";
import { createClient } from "@supabase/supabase-js";
import config from "./config";
import path from "path";
import { logger } from "./utils/logger";
import morgan from "morgan";
// Initialize Supabase client for storage operations
export const supabase = createClient(
config.SUPABASE_URL,
config.SUPABASE_SERVICE_ROLE_KEY,
);
const app = express();
class LoggerStream {
write(message: string) {
logger.info(message.substring(0, message.lastIndexOf('\n')));
}
}
app.use(morgan('combined', { stream: new LoggerStream()}));
app.use(
cors({
origin: config.ALLOWED_ORIGINS || "*",
methods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
credentials: true,
}),
);
app.use(json());
app.use(urlencoded({ extended: true }));
const upload = multer({ storage: multer.memoryStorage(),
limits: { fileSize: 2 * 1024 * 1024 }
});
app.use("/health",(req,res)=>{
res.status(200).json({ message: "OK" });
})
app.use("/api/v1", routes(upload, supabase));
// 404 handler
app.use((req, res) => {
res.status(404).json({ message: "Not Found" });
});
// Global error handler
app.use(errorHandler);
// Serve API documentation
app.use("/docs", express.static(path.join(__dirname, "..", "docs/apidoc")));
export default app;