2020-06-08 10:56:38 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const app = require("../app");
|
|
|
|
const debug = require("debug")("letu:server");
|
|
|
|
const http = require("http");
|
2020-11-21 10:56:44 +01:00
|
|
|
const fs = require("fs");
|
|
|
|
const sass = require("sass");
|
2020-06-08 10:56:38 +02:00
|
|
|
const models = require("../models");
|
2020-11-02 13:43:12 +01:00
|
|
|
const { SMTPClient } = require("emailjs");
|
|
|
|
const mailClient = new SMTPClient(process.env.NODE_ENV === "test" ? {} : require("../config/config.json").email.server);
|
2020-06-08 10:56:38 +02:00
|
|
|
|
2020-11-21 10:56:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render all Sass to css
|
|
|
|
*/
|
2020-12-12 21:52:55 +01:00
|
|
|
let css = sass.renderSync({
|
2020-11-21 10:56:44 +01:00
|
|
|
file: "sass/style.sass",
|
|
|
|
includePaths: ["sass/"],
|
|
|
|
outputStyle: "compressed"
|
|
|
|
});
|
|
|
|
if (!fs.existsSync("public/stylesheets"))
|
|
|
|
fs.mkdirSync("public/stylesheets");
|
|
|
|
fs.writeFileSync("public/stylesheets/style.css", css.css);
|
|
|
|
|
2020-06-08 10:56:38 +02:00
|
|
|
/**
|
|
|
|
* Get port from environment and store in Express.
|
|
|
|
*/
|
|
|
|
|
2020-12-12 18:28:44 +01:00
|
|
|
const port = normalizePort(process.env.PORT || "3000");
|
2020-06-08 10:56:38 +02:00
|
|
|
app.set("port", port);
|
2020-11-02 13:43:12 +01:00
|
|
|
app.set("mailClient", mailClient);
|
2020-06-08 10:56:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create HTTP server.
|
|
|
|
*/
|
|
|
|
|
2020-12-12 18:28:44 +01:00
|
|
|
const server = http.createServer(app);
|
2020-06-08 10:56:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create socket.io server
|
|
|
|
*/
|
|
|
|
|
|
|
|
const io = require("socket.io")(server);
|
2020-10-12 20:54:32 +02:00
|
|
|
io.use((socket, next) => app.get("sessionMiddleware")(socket.request, socket.request.res || {}, next));
|
2020-11-02 13:43:12 +01:00
|
|
|
io.mailClient = mailClient;
|
2020-06-08 10:56:38 +02:00
|
|
|
io.on("connection", require("../sockets"));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen on provided port, on all network interfaces.
|
|
|
|
*/
|
|
|
|
|
|
|
|
models.sequelize.sync().then(() => {
|
|
|
|
server.listen(port, () => {
|
|
|
|
debug("Express server listening on port " + server.address().port);
|
|
|
|
});
|
|
|
|
server.on("error", onError);
|
|
|
|
server.on("listening", onListening);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize a port into a number, string, or false.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function normalizePort(val) {
|
2020-12-12 18:28:44 +01:00
|
|
|
const port = parseInt(val, 10);
|
2020-06-08 10:56:38 +02:00
|
|
|
|
|
|
|
if (isNaN(port)) {
|
|
|
|
// named pipe
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port >= 0) {
|
|
|
|
// port number
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "error" event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function onError(error) {
|
|
|
|
if (error.syscall !== "listen") {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2020-12-12 18:28:44 +01:00
|
|
|
const bind = typeof port === "string"
|
2020-06-08 10:56:38 +02:00
|
|
|
? "Pipe " + port
|
|
|
|
: "Port " + port;
|
|
|
|
|
|
|
|
// handle specific listen errors with friendly messages
|
|
|
|
switch (error.code) {
|
|
|
|
case "EACCES":
|
|
|
|
console.error(bind + " requires elevated privileges");
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
case "EADDRINUSE":
|
|
|
|
console.error(bind + " is already in use");
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event listener for HTTP server "listening" event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function onListening() {
|
2020-12-12 18:28:44 +01:00
|
|
|
const addr = server.address();
|
|
|
|
const bind = typeof addr === "string"
|
2020-06-08 10:56:38 +02:00
|
|
|
? "pipe " + addr
|
|
|
|
: "port " + addr.port;
|
|
|
|
debug("Listening on " + bind);
|
2020-11-24 01:09:20 +01:00
|
|
|
require("../agenda")(app);
|
2020-06-08 10:56:38 +02:00
|
|
|
}
|