MusicTopus/src/app.js

154 lines
4.3 KiB
JavaScript
Raw Normal View History

2022-02-13 17:59:42 +01:00
import path from "path";
import express from "express";
import cookieParser from "cookie-parser";
import passport from "passport";
import mongoose from "mongoose";
import flash from "connect-flash";
import session from "express-session";
import MongoStore from "connect-mongo";
2022-02-13 14:03:04 +01:00
import passportConfig from "./libs/passport";
2022-02-13 17:59:42 +01:00
import config, { env, mongoDbUri, secret } from "./config";
2022-02-15 16:45:14 +01:00
import { isXhr } from "./helpers";
2022-02-15 11:03:20 +01:00
import indexRouter from "./routes";
import maCollectionRouter from "./routes/ma-collection";
import monCompteRouter from "./routes/mon-compte";
import collectionRouter from "./routes/collection";
import importJobsRouter from "./routes/jobs";
2022-02-15 11:03:20 +01:00
import importAlbumRouterApiV1 from "./routes/api/v1/albums";
import importSearchRouterApiV1 from "./routes/api/v1/search";
import importMastodonRouterApiV1 from "./routes/api/v1/mastodon";
import importMeRouterApiV1 from "./routes/api/v1/me";
import importContactRouterApiV1 from "./routes/api/v1/contact";
2022-02-13 17:59:42 +01:00
passportConfig(passport);
2022-02-13 17:59:42 +01:00
mongoose.set("strictQuery", false);
2022-02-13 17:59:42 +01:00
mongoose
.connect(mongoDbUri, { useNewUrlParser: true, useUnifiedTopology: true })
.catch(() => {
process.exit();
});
const sess = {
cookie: {
maxAge: 86400000,
},
secret,
saveUninitialized: false,
resave: false,
store: MongoStore.create({
mongoUrl: mongoDbUri,
mongoOptions: { useNewUrlParser: true, useUnifiedTopology: true },
}),
};
2022-02-13 14:03:04 +01:00
const app = express();
2022-02-13 17:59:42 +01:00
app.use(cookieParser());
app.use(flash());
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ extended: false, limit: "50mb" }));
2022-02-13 17:59:42 +01:00
app.use(session(sess));
if (["production"].indexOf(env) !== -1) {
app.enable("trust proxy", 1);
sess.cookie.secure = true;
/* eslint-disable func-names */
app.use((req, res, next) => {
if (req.secure) {
next();
} else {
res.redirect(`https://${req.headers.host}${req.url}`);
}
});
}
app.use(passport.initialize());
app.use(passport.session());
2022-02-15 11:03:20 +01:00
app.set("views", path.join(__dirname, "../views"));
2022-02-13 17:59:42 +01:00
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "../public")));
app.use("/", indexRouter);
app.use("/mon-compte", monCompteRouter);
app.use("/ma-collection", maCollectionRouter);
app.use("/collection", collectionRouter);
app.use("/jobs", importJobsRouter);
2022-02-15 11:03:20 +01:00
app.use("/api/v1/albums", importAlbumRouterApiV1);
app.use("/api/v1/search", importSearchRouterApiV1);
app.use("/api/v1/mastodon", importMastodonRouterApiV1);
app.use("/api/v1/me", importMeRouterApiV1);
app.use("/api/v1/contact", importContactRouterApiV1);
2022-02-13 17:59:42 +01:00
// Handle 404
app.use((req, res) => {
2022-02-15 16:45:14 +01:00
if (isXhr(req)) {
2022-02-13 17:59:42 +01:00
res.status(404).send({ message: "404: Not found" });
} else {
2022-02-15 11:03:20 +01:00
res.status(404).render("index", {
2022-02-13 17:59:42 +01:00
page: { title: `404: Cette page n'existe pas.` },
2022-02-15 11:03:20 +01:00
viewname: "error",
2022-02-13 17:59:42 +01:00
session: req.session || null,
flash: {
info: req.flash("info"),
error: [
...req.flash("error"),
...(req.session?.flash?.error || []),
],
success: req.flash("success"),
},
query: req.query,
params: req.params,
user: req.user,
config,
getBaseUrl: null,
errorCode: 404,
2022-02-13 17:59:42 +01:00
});
}
});
// Handle 500
app.use((error, req, res, next) => {
2022-02-15 16:45:14 +01:00
if (isXhr(req)) {
2022-02-15 11:03:20 +01:00
const { message, errorCode, date } = error;
res.status(error.errorCode || 500).send({ message, errorCode, date });
2022-02-13 17:59:42 +01:00
} else {
2022-02-15 11:03:20 +01:00
res.status(error.errorCode || 500);
res.render("index", {
page: {
title: error.title || "500: Oups… le serveur a crashé !",
error,
},
2022-02-15 11:03:20 +01:00
viewname: "error",
2022-02-13 17:59:42 +01:00
session: req.session || null,
flash: {
info: req.flash("info"),
error: [
...req.flash("error"),
...(req.session?.flash?.error || []),
],
success: req.flash("success"),
},
query: req.query,
params: req.params,
user: req.user,
config,
getBaseUrl: null,
errorCode: error.errorCode || 500,
2022-02-13 17:59:42 +01:00
});
2022-02-13 14:03:04 +01:00
2022-02-13 17:59:42 +01:00
next();
}
});
2022-02-13 14:03:04 +01:00
export default app;