2019-01-17 21:41:26 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// P A C K A G E S
|
|
|
|
|
|
|
|
import color from "colorette";
|
|
|
|
import compress from "fastify-compress";
|
|
|
|
import cors from "cors";
|
|
|
|
import fastify from "fastify";
|
|
|
|
import helmet from "fastify-helmet";
|
|
|
|
import ssr from "choo-ssr/fastify";
|
|
|
|
import statik from "fastify-static";
|
|
|
|
import websockets from "@inc/fastify-ws";
|
|
|
|
|
|
|
|
// U T I L S
|
|
|
|
|
|
|
|
import handleSocketMessages from "./sockets";
|
2019-01-30 00:29:14 +01:00
|
|
|
import messageSlack from "@helper/slack";
|
|
|
|
import redirects from "@data/redirects.json";
|
2019-01-17 21:41:26 +01:00
|
|
|
|
|
|
|
const server = fastify({
|
|
|
|
logger: {
|
|
|
|
level: "warn",
|
|
|
|
prettyPrint: process.env.NODE_ENV === "development" ? true : false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// P R O G R A M
|
|
|
|
|
|
|
|
server
|
|
|
|
.use(cors())
|
|
|
|
.register(compress)
|
|
|
|
.register(websockets)
|
|
|
|
.register(helmet, {
|
|
|
|
hidePoweredBy: {
|
|
|
|
setTo: "LBRY"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.register(statik, {
|
|
|
|
prefix: "/assets/",
|
|
|
|
root: `${__dirname}/dist/`
|
|
|
|
})
|
|
|
|
.register(ssr, {
|
|
|
|
app: require("./client")
|
|
|
|
})
|
|
|
|
.addHook("preHandler", (request, reply, next) => {
|
2019-01-18 19:15:17 +01:00
|
|
|
if (redirects[request.raw.originalUrl])
|
|
|
|
reply.redirect(301, redirects[request.raw.originalUrl]);
|
|
|
|
|
2019-01-17 21:41:26 +01:00
|
|
|
if (process.env.NODE_ENV !== "development") {
|
|
|
|
if (request.headers["x-forwarded-proto"] !== "https")
|
|
|
|
reply.redirect(302, `https://${request.raw.hostname}${request.raw.originalUrl}`);
|
|
|
|
|
|
|
|
else
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
.ready(err => {
|
2019-01-30 23:15:10 +01:00
|
|
|
if (err)
|
|
|
|
throw err;
|
2019-01-17 21:41:26 +01:00
|
|
|
|
|
|
|
server.ws.on("connection", socket => {
|
|
|
|
socket.on("message", data => {
|
|
|
|
data = JSON.parse(data);
|
|
|
|
return handleSocketMessages(socket, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("close", () => socket.terminate());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// B E G I N
|
|
|
|
|
2019-01-17 22:51:00 +01:00
|
|
|
server.listen(process.env.PORT || 8080, process.env.IP || "0.0.0.0", async() => {
|
2019-01-17 21:41:26 +01:00
|
|
|
process.env.NODE_ENV === "development" ?
|
|
|
|
process.stdout.write(`\n— ${color.green("⚡")} ${server.server.address().port}\n`) :
|
|
|
|
messageSlack({
|
|
|
|
message: `Server started at port \`${server.server.address().port}\``,
|
|
|
|
title: "APP BOOT"
|
|
|
|
});
|
2019-01-17 22:51:00 +01:00
|
|
|
});
|