lbry.tech/server.js

77 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-07-12 23:07:16 +02:00
"use strict"; require("dotenv").config(); require("date-format-lite");
2018-06-01 22:23:53 +02:00
2018-03-28 16:26:59 +02:00
2018-07-05 20:50:18 +02:00
// P A C K A G E S
2018-04-10 11:08:46 +02:00
2018-08-29 01:57:18 +02:00
const color = require("colorette");
2018-07-05 20:50:18 +02:00
const cors = require("cors");
2018-09-27 18:11:26 +02:00
const local = require("app-root-path").require;
2018-05-23 16:25:16 +02:00
2018-07-12 17:21:42 +02:00
const fastify = require("fastify")({
logger: {
level: "warn",
prettyPrint: process.env.NODE_ENV === "development" ? true : false
}
});
2018-05-23 16:25:16 +02:00
2018-07-12 23:07:16 +02:00
// V A R I A B L E S
2018-09-27 18:11:26 +02:00
const handleSocketMessages = local("app/sockets");
2018-08-29 01:57:18 +02:00
const logSlackError = local("app/helpers/slack");
2018-05-23 16:25:16 +02:00
2018-04-16 15:39:11 +02:00
2018-07-05 20:50:18 +02:00
// P R O G R A M
2018-05-23 16:25:16 +02:00
2018-07-12 17:21:42 +02:00
fastify.use(cors());
2018-07-12 23:07:16 +02:00
2018-07-12 17:21:42 +02:00
fastify.register(require("fastify-compress"));
fastify.register(require("fastify-ws"));
2018-06-08 13:45:56 +02:00
2018-07-12 17:21:42 +02:00
fastify.register(require("fastify-helmet"), {
hidePoweredBy: { setTo: "LBRY" }
2018-07-05 20:50:18 +02:00
});
2018-06-08 13:45:56 +02:00
2018-07-12 17:21:42 +02:00
fastify.register(require("fastify-static"), {
2018-08-29 01:57:18 +02:00
prefix: "/assets/",
root: `${__dirname}/app/dist/`
2018-07-05 20:50:18 +02:00
});
2018-05-30 07:50:48 +02:00
2018-07-12 17:21:42 +02:00
fastify.register(require("choo-ssr/fastify"), {
2018-08-29 01:57:18 +02:00
app: require("./app")
2018-07-12 17:21:42 +02:00
});
2018-05-23 16:25:16 +02:00
2018-07-12 17:21:42 +02:00
fastify.ready(err => {
if (err) throw err;
2018-05-30 17:25:04 +02:00
2018-07-12 17:21:42 +02:00
fastify.ws.on("connection", socket => {
2018-07-16 23:06:37 +02:00
socket.on("message", data => {
data = JSON.parse(data);
2018-09-27 18:11:26 +02:00
return handleSocketMessages(socket, data);
2018-05-12 12:03:21 +02:00
});
2018-07-05 20:50:18 +02:00
2018-09-27 18:11:26 +02:00
socket.on("close", () => socket.terminate());
2018-07-05 20:50:18 +02:00
});
2018-07-12 17:21:42 +02:00
});
2018-06-01 09:10:22 +02:00
2018-07-12 17:21:42 +02:00
// B E G I N
2018-06-01 09:10:22 +02:00
2018-07-12 17:21:42 +02:00
const start = async () => {
try {
2018-09-25 03:16:34 +02:00
await fastify.listen(process.env.PORT || 8080, process.env.IP || "0.0.0.0");
2018-07-12 17:21:42 +02:00
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
2018-06-01 09:10:22 +02:00
2018-07-12 23:07:16 +02:00
process.env.NODE_ENV === "development" ?
2018-09-27 18:11:26 +02:00
process.stdout.write(`\n${color.green("⚡")} ${fastify.server.address().port}\n`) :
2018-07-12 23:07:16 +02:00
logSlackError(`Server started at port \`${fastify.server.address().port}\``)
;
2018-07-12 17:21:42 +02:00
};
2018-06-06 13:02:16 +02:00
2018-07-12 17:21:42 +02:00
start();