|
|
@ -2,9 +2,6 @@ import fastify, { FastifyInstance } from "fastify"; |
|
|
|
import fastifyCookie from "fastify-cookie"; |
|
|
|
import fastifyFormBody from "fastify-formbody"; |
|
|
|
import fastifyMultipart from "fastify-multipart"; |
|
|
|
import fastifyHttpProxy from "fastify-http-proxy"; |
|
|
|
import fastifyStatic from "fastify-static"; |
|
|
|
import { join } from "path"; |
|
|
|
import { InternalService, Microservice } from "@autoplex/microservice"; |
|
|
|
import { RouteRegisterFactory, RouteFactory } from "./RouteRegisterFactory"; |
|
|
|
import { MiddlewareMethod } from "./middleware"; |
|
|
@ -26,12 +23,6 @@ export abstract class WebServerService<M extends Microservice = Microservice> ex |
|
|
|
*/ |
|
|
|
protected abstract readonly ROUTES: RouteFactory<MiddlewareMethod<any>, M>[]; |
|
|
|
|
|
|
|
/** |
|
|
|
* @TODO this should be removed later on |
|
|
|
* Indicate if SPA routes should be registered |
|
|
|
*/ |
|
|
|
protected readonly REGISTER_SPA_ROUTES: boolean = false; |
|
|
|
|
|
|
|
/** |
|
|
|
* The internal webserver instance |
|
|
|
*/ |
|
|
@ -49,10 +40,6 @@ export abstract class WebServerService<M extends Microservice = Microservice> ex |
|
|
|
|
|
|
|
// Register the routes
|
|
|
|
this.registerRoutes(this.fastify); |
|
|
|
|
|
|
|
if (this.REGISTER_SPA_ROUTES) { |
|
|
|
this.registerSpaRoutes(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -101,27 +88,4 @@ export abstract class WebServerService<M extends Microservice = Microservice> ex |
|
|
|
this.ROUTES[group](factory, this.app); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Register the routes required for the single-page application |
|
|
|
*/ |
|
|
|
protected registerSpaRoutes() { |
|
|
|
/** |
|
|
|
* If the app is in production mode, serve static assets. |
|
|
|
* If the app is in development mode, forward 404's to Vite. |
|
|
|
*/ |
|
|
|
if (process.env["NODE_ENV"] == "production") { |
|
|
|
this.fastify.register(fastifyStatic, { |
|
|
|
root: join(__dirname, "../../../public") |
|
|
|
}); |
|
|
|
this.fastify.setNotFoundHandler((request, reply) => { |
|
|
|
return reply.sendFile("index.html"); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
this.log("Using Vite proxy"); |
|
|
|
this.fastify.register(fastifyHttpProxy, { |
|
|
|
upstream: "http://localhost:3000" |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |