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
|
|
|
|
2022-04-09 00:07:22 +02: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";
|
2022-03-04 16:33:45 +01:00
|
|
|
import maCollectionRouter from "./routes/ma-collection";
|
2022-04-10 15:21:15 +02:00
|
|
|
import monCompteRouter from "./routes/mon-compte";
|
2022-03-06 14:38:26 +01:00
|
|
|
import collectionRouter from "./routes/collection";
|
2022-02-17 09:37:25 +01:00
|
|
|
|
2022-04-09 00:07:22 +02:00
|
|
|
import importJobsRouter from "./routes/jobs";
|
|
|
|
|
2022-02-15 11:03:20 +01:00
|
|
|
import importAlbumRouterApiV1 from "./routes/api/v1/albums";
|
2022-02-17 09:37:25 +01:00
|
|
|
import importSearchRouterApiV1 from "./routes/api/v1/search";
|
2023-08-02 16:11:56 +02:00
|
|
|
import importMastodonRouterApiV1 from "./routes/api/v1/mastodon";
|
2022-03-06 14:38:26 +01:00
|
|
|
import importMeRouterApiV1 from "./routes/api/v1/me";
|
2022-09-01 10:20:13 +02:00
|
|
|
import importContactRouterApiV1 from "./routes/api/v1/contact";
|
2022-02-13 17:59:42 +01:00
|
|
|
|
2022-04-09 00:07:22 +02:00
|
|
|
passportConfig(passport);
|
2022-02-13 17:59:42 +01:00
|
|
|
|
2023-08-02 16:11:56 +02: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());
|
2022-04-09 00:07:22 +02:00
|
|
|
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);
|
2022-04-10 15:21:15 +02:00
|
|
|
app.use("/mon-compte", monCompteRouter);
|
2022-03-04 16:33:45 +01:00
|
|
|
app.use("/ma-collection", maCollectionRouter);
|
2022-03-06 14:38:26 +01:00
|
|
|
app.use("/collection", collectionRouter);
|
2022-04-09 00:07:22 +02:00
|
|
|
app.use("/jobs", importJobsRouter);
|
2022-02-15 11:03:20 +01:00
|
|
|
app.use("/api/v1/albums", importAlbumRouterApiV1);
|
2022-02-17 09:37:25 +01:00
|
|
|
app.use("/api/v1/search", importSearchRouterApiV1);
|
2023-08-02 16:11:56 +02:00
|
|
|
app.use("/api/v1/mastodon", importMastodonRouterApiV1);
|
2022-03-06 14:38:26 +01:00
|
|
|
app.use("/api/v1/me", importMeRouterApiV1);
|
2022-09-01 10:20:13 +02:00
|
|
|
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,
|
2022-04-10 15:21:15 +02:00
|
|
|
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", {
|
2022-03-06 14:38:26 +01:00
|
|
|
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,
|
2022-04-10 15:21:15 +02:00
|
|
|
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;
|