2022-04-30 16:19:31 +02:00
|
|
|
import createError from "http-errors";
|
2022-09-21 17:24:02 +02:00
|
|
|
import express, { NextFunction, Request, Response } from "express";
|
2022-04-30 16:19:31 +02:00
|
|
|
import path from "path";
|
|
|
|
import cookieParser from "cookie-parser";
|
|
|
|
import fs from "fs"
|
|
|
|
import newlog from "./config/winston.js"
|
|
|
|
import * as statusTools from "./tools/status.js"
|
|
|
|
import * as hassioApiTools from "./tools/hassioApiTools.js"
|
|
|
|
import * as settingsTools from "./tools/settingsTools.js"
|
|
|
|
import cronTools from "./tools/cronTools.js"
|
2022-09-21 17:24:02 +02:00
|
|
|
import webdav from "./tools/webdavTools.js";
|
2019-12-19 15:08:47 +01:00
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
import apiRouter from "./routes/api.js"
|
2022-09-21 17:24:02 +02:00
|
|
|
import { fileURLToPath } from "url";
|
2022-04-30 16:19:31 +02:00
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
2022-09-21 17:24:02 +02:00
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
2019-12-19 15:08:47 +01:00
|
|
|
|
|
|
|
const app = express();
|
2022-09-21 17:24:02 +02:00
|
|
|
|
|
|
|
// app.use(
|
|
|
|
// logger("dev", {
|
|
|
|
// skip: function (req, res) {
|
|
|
|
// return (res.statusCode = 304);
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
// );
|
2019-12-19 15:08:47 +01:00
|
|
|
app.use(express.json());
|
2020-11-09 12:42:26 +01:00
|
|
|
app.use(express.urlencoded({ extended: false }));
|
2019-12-19 15:08:47 +01:00
|
|
|
app.use(cookieParser());
|
2020-11-09 12:42:26 +01:00
|
|
|
app.use(express.static(path.join(__dirname, "public")));
|
2021-01-08 01:49:21 +01:00
|
|
|
|
2021-01-08 13:41:36 +01:00
|
|
|
app.use("/api", apiRouter);
|
|
|
|
|
|
|
|
/*
|
|
|
|
-----------------------------------------------------------
|
|
|
|
Error handler
|
|
|
|
----------------------------------------------------------
|
|
|
|
*/
|
2019-12-19 15:08:47 +01:00
|
|
|
// catch 404 and forward to error handler
|
2022-09-21 17:24:02 +02:00
|
|
|
app.use((req, res, next) => {
|
2020-02-15 14:28:48 +01:00
|
|
|
next(createError(404));
|
2019-12-19 15:08:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// error handler
|
2022-09-21 17:24:02 +02:00
|
|
|
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
|
2020-02-15 14:28:48 +01:00
|
|
|
// set locals, only providing error in development
|
|
|
|
res.locals.message = err.message;
|
2020-11-09 12:42:26 +01:00
|
|
|
res.locals.error = req.app.get("env") === "development" ? err : {};
|
2020-02-15 14:28:48 +01:00
|
|
|
|
|
|
|
// render the error page
|
|
|
|
res.status(err.status || 500);
|
2020-11-09 12:42:26 +01:00
|
|
|
res.render("error");
|
2019-12-19 15:08:47 +01:00
|
|
|
});
|
|
|
|
|
2021-01-08 13:41:36 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
-----------------------------------------------------------
|
|
|
|
Init app
|
|
|
|
----------------------------------------------------------
|
|
|
|
*/
|
2022-02-14 11:39:04 +01:00
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
|
2022-02-14 11:39:04 +01:00
|
|
|
|
|
|
|
newlog.info(`Log level: ${ process.env.LOG_LEVEL }`);
|
2022-09-21 17:24:02 +02:00
|
|
|
|
|
|
|
newlog.info(`Backup timeout: ${ (process.env.CREATE_BACKUP_TIMEOUT ? parseInt(process.env.CREATE_BACKUP_TIMEOUT) : false) || ( 90 * 60 * 1000 ) }`)
|
2022-02-14 11:39:04 +01:00
|
|
|
|
2020-11-09 12:42:26 +01:00
|
|
|
if (!fs.existsSync("/data")) fs.mkdirSync("/data");
|
2019-12-19 15:08:47 +01:00
|
|
|
statusTools.init();
|
2020-02-15 14:28:48 +01:00
|
|
|
newlog.info("Satus : \x1b[32mGo !\x1b[0m");
|
2022-04-30 16:19:31 +02:00
|
|
|
|
2019-12-19 15:08:47 +01:00
|
|
|
hassioApiTools.getSnapshots().then(
|
2020-02-15 14:28:48 +01:00
|
|
|
() => {
|
2020-11-09 12:42:26 +01:00
|
|
|
newlog.info("Hassio API : \x1b[32mGo !\x1b[0m");
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
newlog.error("Hassio API : \x1b[31;1mFAIL !\x1b[0m");
|
2020-02-15 14:28:48 +01:00
|
|
|
newlog.error("... " + err);
|
2020-11-09 12:42:26 +01:00
|
|
|
}
|
|
|
|
);
|
2019-12-19 15:08:47 +01:00
|
|
|
|
|
|
|
webdav.confIsValid().then(
|
2020-02-15 14:28:48 +01:00
|
|
|
() => {
|
2020-11-09 12:42:26 +01:00
|
|
|
newlog.info("Nextcloud connection : \x1b[32mGo !\x1b[0m");
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
newlog.error("Nextcloud connection : \x1b[31;1mFAIL !\x1b[0m");
|
2020-02-15 14:28:48 +01:00
|
|
|
newlog.error("... " + err);
|
|
|
|
}
|
2020-11-09 12:42:26 +01:00
|
|
|
);
|
2019-12-19 15:08:47 +01:00
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
settingsTools.check(settingsTools.getSettings(), true);
|
|
|
|
cronTools.init();
|
2021-01-09 17:09:44 +01:00
|
|
|
|
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
export default app;
|