|
|
@ -1,4 +1,5 @@ |
|
|
|
import fastify from "fastify"; |
|
|
|
import fastifyCookie from "fastify-cookie"; |
|
|
|
import fastifyFormBody from "fastify-formbody"; |
|
|
|
import fastifyHttpProxy from "fastify-http-proxy"; |
|
|
|
import fastifyMultipart from "fastify-multipart"; |
|
|
@ -8,6 +9,7 @@ import Service from "../Service"; |
|
|
|
import { join } from "path"; |
|
|
|
import routes from "./routes"; |
|
|
|
import "./validators"; |
|
|
|
import RouteRegisterFactory, { RouteFactory } from "./routes/RouteRegisterFactory"; |
|
|
|
|
|
|
|
export default class WebServer extends Service |
|
|
|
{ |
|
|
@ -19,7 +21,7 @@ export default class WebServer extends Service |
|
|
|
/** |
|
|
|
* The internal webserver instance |
|
|
|
*/ |
|
|
|
protected server: ReturnType<typeof fastify>; |
|
|
|
protected fastify: ReturnType<typeof fastify>; |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new webserver instance |
|
|
@ -27,28 +29,35 @@ export default class WebServer extends Service |
|
|
|
public constructor(app: Application) { |
|
|
|
super("Web Server", app); |
|
|
|
this.port = parseInt(<string>process.env["WEBSERVER_PORT"]); |
|
|
|
this.server = fastify(); |
|
|
|
this.registerPlugins(); |
|
|
|
this.fastify = fastify(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Register required Fastify plugins |
|
|
|
*/ |
|
|
|
protected registerPlugins() { |
|
|
|
this.server.register(fastifyMultipart, { |
|
|
|
limits: { |
|
|
|
fileSize: 16*1024*1024, |
|
|
|
files: 50 |
|
|
|
} |
|
|
|
}); |
|
|
|
this.server.register(fastifyFormBody); |
|
|
|
return Promise.all([ |
|
|
|
this.fastify.register(fastifyCookie, { |
|
|
|
secret: this.app.APP_KEY |
|
|
|
}), |
|
|
|
this.fastify.register(fastifyFormBody), |
|
|
|
this.fastify.register(fastifyMultipart, { |
|
|
|
limits: { |
|
|
|
fileSize: 16*1024*1024, |
|
|
|
files: 50 |
|
|
|
} |
|
|
|
}) |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Boot the webserver |
|
|
|
*/ |
|
|
|
public async boot() { |
|
|
|
// Register the available routes
|
|
|
|
// Install plugins
|
|
|
|
await this.registerPlugins(); |
|
|
|
|
|
|
|
// Register the routes
|
|
|
|
this.registerRoutes(); |
|
|
|
} |
|
|
|
|
|
|
@ -57,7 +66,7 @@ export default class WebServer extends Service |
|
|
|
*/ |
|
|
|
public start() { |
|
|
|
// Start listening
|
|
|
|
this.server.listen(this.port, "0.0.0.0"); |
|
|
|
this.fastify.listen(this.port, "0.0.0.0"); |
|
|
|
this.log("Webserver listening on port:", this.port); |
|
|
|
} |
|
|
|
|
|
|
@ -66,7 +75,7 @@ export default class WebServer extends Service |
|
|
|
*/ |
|
|
|
public async shutdown() { |
|
|
|
this.log("Webserver shutting down"); |
|
|
|
await this.server.close(); |
|
|
|
await this.fastify.close(); |
|
|
|
} |
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
@ -76,8 +85,9 @@ export default class WebServer extends Service |
|
|
|
*/ |
|
|
|
protected registerRoutes() { |
|
|
|
this.registerSpaRoutes(); |
|
|
|
for (let routeGroup in routes) { |
|
|
|
(<any>routes)[routeGroup](this.server, this.app); |
|
|
|
let factory = new RouteRegisterFactory(this.fastify, this.app); |
|
|
|
for (let group in routes) { |
|
|
|
<RouteFactory>(<any>routes)[group](factory, this.app); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -91,15 +101,15 @@ export default class WebServer extends Service |
|
|
|
* NOTE: Static assets may be moved to nginx later... not sure yet |
|
|
|
*/ |
|
|
|
if (process.env["NODE_ENV"] == "production") { |
|
|
|
this.server.register(fastifyStatic, { |
|
|
|
this.fastify.register(fastifyStatic, { |
|
|
|
root: join(__dirname, "../../../public") |
|
|
|
}); |
|
|
|
this.server.setNotFoundHandler((request, reply) => { |
|
|
|
this.fastify.setNotFoundHandler((request, reply) => { |
|
|
|
return reply.sendFile("index.html"); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
console.log("Using Vite proxy"); |
|
|
|
this.server.register(fastifyHttpProxy, { |
|
|
|
this.fastify.register(fastifyHttpProxy, { |
|
|
|
upstream: "http://localhost:3001" |
|
|
|
}); |
|
|
|
} |
|
|
|