-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (61 loc) · 1.8 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
const fs = require("fs");
const express = require("express");
const app = express();
const http = require("http");
const https = require("https");
const { Server } = require("socket.io");
const env = require("dotenv").config();
const session = require("express-session");
const path = require("path");
const { Sequelize } = require("sequelize");
//https options
const options = {
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.cert"),
};
//servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(options, app);
//sequelize
const sequelize = new Sequelize(process.env.DATABASE_URL);
//io
const io = new Server(httpServer);
const ioHttps = new Server(httpsServer);
//routers
const publicRouter = require("./routes/public");
const authRouter = require("./routes/auth");
const apiRouter = require("./routes/api");
const errorRouter = require("./routes/error");
//utils
const { initDbSchema } = require("./utils/db");
const handleIoConnection = require("./utils/io");
//middlewares
app.use(express.json());
app.use(express.static("./public"));
app.use(
session({
name: "MyPersonalSession",
resave: false,
saveUninitialized: false,
secret: process.env.SECRET_KEY,
cookie: {
secure: false,
maxAge: 60000 * 60,
},
})
);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
initDbSchema(sequelize);
app.use(publicRouter);
app.use("/auth", authRouter);
app.use("/api", apiRouter);
app.use("/error", errorRouter);
handleIoConnection(io);
handleIoConnection(ioHttps);
httpsServer.listen(4000, () => {
console.log("server in ascolto sulla porta 4000");
});
httpServer.listen(process.env.PORT, () => {
console.log(`Server in ascolto sulla porta ${process.env.PORT}`);
});