-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
133 lines (123 loc) · 3.62 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
"use strict";
/*Powered By: Manaknightdigital Inc. https://manaknightdigital.com/ Year: 2020*/
/**
* App
* @copyright 2020 Manaknightdigital Inc.
* @link https://manaknightdigital.com
* @license Proprietary Software licensing
* @author Ryan Wong
*
*/
require("dotenv").config();
const { getLocalPath } = require("./core/helpers");
const fs = require("fs");
const express = require("express");
const path = require("path");
const body_parser = require("body-parser");
const logger = require("morgan");
const helmet = require("helmet");
const PowerByService = require("./services/PowerByService");
const UploadService = require("./services/UploadService");
const cookieParser = require("cookie-parser");
const controllersEndpoints = require("./controllers/index");
const portalsEndpoints = require("./routes/index");
const db = require("./models");
const sessionClass = require("express-session");
const flash = require("connect-flash");
const cors = require("cors");
const sizeOf = require("image-size");
let app = express();
const expressip = require("express-ip");
if (process.NODE_ENV === "maintenance") {
app.all("*", (req, res) => {
res.status(503).json({ message: "website under maintenance" });
});
}
app.set("iocContainer", process.env);
app.set("db", db);
app.use(expressip().getIpInfoMiddleware);
app.use(logger("dev"));
app.use(
express.json({
limit: "50mb",
verify: (req, res, buf) => {
req.rawBody = buf;
},
})
);
app.use(
express.urlencoded({
extended: false,
})
);
app.use(cors());
app.use(flash());
let session = {
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
name: "session",
proxy: true,
cookie: { maxAge: 60 * 60 * 1000 },
};
app.set("trust proxy", true);
if (process.env.NODE_ENV === "production") {
session.cookie.secure = true; // serve secure cookies
}
app.use(sessionClass(session));
app.set("view engine", "eta");
app.set("views", path.join(__dirname, "/views"));
app.use(cookieParser());
app.use(helmet());
app.use(PowerByService);
app.use(express.static(path.join(__dirname, "/public")));
app.get("/version", (req, res) =>
res.status(200).json({ version: process.env.VERSION })
);
app.get("/", (req, res, next) => {
return res.send("<h1 style='text-align:left;'>Live!</h1>");
});
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.json({
message: err.message,
});
});
//local upload default
const upload = UploadService.local_upload();
app.post(
"/v1/upload/file",
upload.single("file"),
async function (req, res, next) {
try {
const url = getLocalPath(req.file.path);
let params = {
url: url,
user_id: req.session?.user || null,
caption: req.body?.caption || null,
type: +req.body?.type || null,
};
const uploadedfile = fs.readFileSync(req.file.path);
if (!req.file.mimetype.includes("video")) {
const dimensions = sizeOf(uploadedfile);
params.width = dimensions.width;
params.height = dimensions.height;
}
let createdImage = await db.image.insert(params);
return res.status(201).json({ id: createdImage, url });
} catch (error) {
console.log(error);
return res.status(500).json({ error: true, message: error.message });
}
}
);
app.use(portalsEndpoints);
app.use(controllersEndpoints);
app.use((req, res, next) => {
return res.status(404).send("<h1 style='text-align:left;'>Not found!</h1>");
});
module.exports = app;