-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
122 lines (96 loc) · 2.97 KB
/
server.js
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
// Logger
import winston from "winston";
const logger = winston.createLogger({
level: "info",
// format: winston.format.json(),
format: winston.format.simple(),
transports: [new winston.transports.Console()],
});
logger.info("Starting the Server 🎉 🎉 🎉 !");
import "express-async-errors";
// Load ENV Variables
import * as dotenv from "dotenv";
dotenv.config();
// Express
import express from "express";
const app = express();
app.use(express.json());
// app.use(express.urlencoded({ extended: true }))
// GCP Initialize Cloud Storage
logger.info("Attempting to connect to GCP Cloud Storage");
import { initializeApp, cert } from "firebase-admin/app";
const serviceAccount = JSON.parse(process.env.GCP_KEY);
initializeApp({
credential: cert(serviceAccount),
storageBucket: "cloudxapi.appspot.com",
});
logger.info("Connected to GCP Cloud Storage");
// Database
import { Sequelize } from "sequelize";
import pg from "pg";
const sequelize = new Sequelize(process.env.DB_URL, {
logging: false,
dialectModule: pg,
});
// Models
import fileModel from "./models/FileModel.js";
import userModel from "./models/UserModel.js";
const File = fileModel(sequelize);
const User = userModel(sequelize);
export { File, User };
// Morgan - better logging during Dev
import morgan from "morgan";
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
//cookie parser
import cookieParser from "cookie-parser";
app.use(cookieParser());
// routers
import fileRouter from "./routes/fileRouter.js";
import authRouter from "./routes/authRouter.js";
import userRouter from "./routes/userRouter.js";
// middleware
import errorHandlerMiddleware from "./middlewares/errorHandlerMiddleware.js";
import { authenticateUser } from "./middlewares/authMiddleware.js";
// app.get("/", (req, res) => {
// res.send("Hello World");
// });
//test endpoint
app.get("/api/v1/test", (req, res) => {
res.json({ msg: "Success" });
});
app.use("/api/v1/files", authenticateUser, fileRouter);
app.use("/api/v1/user", authenticateUser, userRouter);
app.use("/api/v1/auth", authRouter);
// Serve Frontend
import { dirname } from "path";
import { fileURLToPath } from "url";
import path from "path";
const __dirname = dirname(fileURLToPath(import.meta.url));
app.use(express.static(path.resolve(__dirname, "./client/dist")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "./client/dist", "index.html"));
});
// Route not found
app.use("*", (req, res) => {
res.status(404).json({ msg: "Not Found" });
});
// Error in server
app.use(errorHandlerMiddleware);
// port
const port = process.env.PORT || 8000;
try {
logger.info("Attempting to connect to Supabase Postgres");
await sequelize.authenticate();
sequelize.sync({ alter: true });
logger.info("Connected to Supabase Postgres");
// spin up the server
app.listen(port, () => {
logger.info(`Server running on PORT ${port}... 🎉 🎉 🎉 !`);
});
} catch (error) {
console.log(error);
process.exit(1);
}
export default app;