app.ts74 lines, 1.6 KB
| 1 | import dotenv from "dotenv"; |
| 2 | |
| 3 | dotenv.config(); |
| 4 | import express from "express"; |
| 5 | import cors from "cors"; |
| 6 | import helmet from "helmet"; |
| 7 | import morgan from "morgan"; |
| 8 | import mongoose from "mongoose"; |
| 9 | import passport from "passport"; |
| 10 | import session from "express-session"; |
| 11 | import MongoStore from "connect-mongo"; |
| 12 | import "./config/passport"; |
| 13 | |
| 14 | // routes |
| 15 | import authRoute from "./routes/auth"; |
| 16 | import contractsRoute from "./routes/contracts"; |
| 17 | import paymentsRoute from "./routes/payments"; |
| 18 | import { handleWebhook } from "./controllers/payment.controller"; |
| 19 | |
| 20 | const app = express(); |
| 21 | |
| 22 | mongoose |
| 23 | .connect(process.env.MONGODB_URI!) |
| 24 | .then(() => console.log("Connected to MongoDB")) |
| 25 | .catch((err) => console.error(err)); |
| 26 | |
| 27 | app.use( |
| 28 | cors({ |
| 29 | origin: process.env.CLIENT_URL, |
| 30 | credentials: true, |
| 31 | }) |
| 32 | ); |
| 33 | |
| 34 | app.use(helmet()); |
| 35 | app.use(morgan("dev")); |
| 36 | |
| 37 | app.post( |
| 38 | "/payments/webhook", |
| 39 | express.raw({ type: "application/json" }), |
| 40 | handleWebhook |
| 41 | ); |
| 42 | |
| 43 | app.use(express.json()); |
| 44 | |
| 45 | app.use( |
| 46 | session({ |
| 47 | secret: process.env.SESSION_SECRET!, |
| 48 | resave: false, |
| 49 | saveUninitialized: false, |
| 50 | store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI! }), |
| 51 | cookie: { |
| 52 | secure: process.env.NODE_ENV === "production", |
| 53 | sameSite: process.env.NODE_ENV === "production" ? "none" : "lax", |
| 54 | maxAge: 24 * 60 * 60 * 1000, // 24 hours |
| 55 | }, |
| 56 | }) |
| 57 | ); |
| 58 | |
| 59 | app.use(passport.initialize()); |
| 60 | app.use(passport.session()); |
| 61 | |
| 62 | // health check |
| 63 | app.get("/health", (req, res) => { |
| 64 | res.send("OK"); |
| 65 | }); |
| 66 | |
| 67 | app.use("/auth", authRoute); |
| 68 | app.use("/contracts", contractsRoute); |
| 69 | app.use("/payments", paymentsRoute); |
| 70 | |
| 71 | const PORT = 8080; |
| 72 | app.listen(PORT, () => { |
| 73 | console.log(`Server started on port ${PORT}`); |
| 74 | }); |