1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
letu/bin/www

124 lines
2.5 KiB
Text
Raw Normal View History

#!/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");
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-11-21 10:56:44 +01:00
/**
* Render all Sass to css
*/
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);
/**
* Get port from environment and store in Express.
*/
2020-12-12 18:28:44 +01:00
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
2020-11-02 13:43:12 +01:00
app.set("mailClient", mailClient);
/**
* Create HTTP server.
*/
2020-12-12 18:28:44 +01:00
const server = http.createServer(app);
/**
* 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;
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);
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"
? "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"
? "pipe " + addr
: "port " + addr.port;
debug("Listening on " + bind);
2020-11-24 01:09:20 +01:00
require("../agenda")(app);
}