|
@ -3,6 +3,7 @@ import Application from "@server/Application"; |
|
|
import { handleMiddleware, MiddlewareMethod } from "../middleware"; |
|
|
import { handleMiddleware, MiddlewareMethod } from "../middleware"; |
|
|
import fastifyHttpProxy from "fastify-http-proxy"; |
|
|
import fastifyHttpProxy from "fastify-http-proxy"; |
|
|
import { auth } from "../middleware/auth"; |
|
|
import { auth } from "../middleware/auth"; |
|
|
|
|
|
import WebServer from ".."; |
|
|
|
|
|
|
|
|
export type RouteFactory = ((factory: RouteRegisterFactory) => void) |
|
|
export type RouteFactory = ((factory: RouteRegisterFactory) => void) |
|
|
| ((factory: RouteRegisterFactory, app: Application) => void); |
|
|
| ((factory: RouteRegisterFactory, app: Application) => void); |
|
@ -19,6 +20,11 @@ export default class RouteRegisterFactory |
|
|
*/ |
|
|
*/ |
|
|
protected readonly fastify: FastifyInstance; |
|
|
protected readonly fastify: FastifyInstance; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* The webserver instance |
|
|
|
|
|
*/ |
|
|
|
|
|
protected readonly webserver: WebServer; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* The list of middleware |
|
|
* The list of middleware |
|
|
*/ |
|
|
*/ |
|
@ -32,9 +38,10 @@ export default class RouteRegisterFactory |
|
|
/** |
|
|
/** |
|
|
* Create a new route factory |
|
|
* Create a new route factory |
|
|
*/ |
|
|
*/ |
|
|
public constructor(fastify: FastifyInstance, app: Application) { |
|
|
|
|
|
|
|
|
public constructor(webserver: WebServer, fastify: FastifyInstance, app: Application) { |
|
|
this.app = app; |
|
|
this.app = app; |
|
|
this.fastify = fastify; |
|
|
this.fastify = fastify; |
|
|
|
|
|
this.webserver = webserver; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -63,9 +70,9 @@ export default class RouteRegisterFactory |
|
|
public get(path: string, handler: RouteHandlerMethod): void; |
|
|
public get(path: string, handler: RouteHandlerMethod): void; |
|
|
public get(path: string, middleware: MiddlewareMethod[], handler: RouteHandlerMethod): void; |
|
|
public get(path: string, middleware: MiddlewareMethod[], handler: RouteHandlerMethod): void; |
|
|
public get(path: string, middleware: RouteHandlerMethod|MiddlewareMethod[], handler?: RouteHandlerMethod) { |
|
|
public get(path: string, middleware: RouteHandlerMethod|MiddlewareMethod[], handler?: RouteHandlerMethod) { |
|
|
|
|
|
this.log(`Registering GET: ${this.pathPrefix}${path}`); |
|
|
handler = (handler ?? <RouteHandlerMethod>middleware); |
|
|
handler = (handler ?? <RouteHandlerMethod>middleware); |
|
|
middleware = (middleware instanceof Array) ? this.middleware.concat(middleware) : this.middleware; |
|
|
middleware = (middleware instanceof Array) ? this.middleware.concat(middleware) : this.middleware; |
|
|
console.log("Registering route:", `${this.pathPrefix}${path}`); |
|
|
|
|
|
this.fastify.get(`${this.pathPrefix}${path}`, handleMiddleware(middleware, handler)); |
|
|
this.fastify.get(`${this.pathPrefix}${path}`, handleMiddleware(middleware, handler)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -84,10 +91,18 @@ export default class RouteRegisterFactory |
|
|
* Register a proxy route |
|
|
* Register a proxy route |
|
|
*/ |
|
|
*/ |
|
|
public proxy(path: string, upstream: string, middleware?: MiddlewareMethod[]) { |
|
|
public proxy(path: string, upstream: string, middleware?: MiddlewareMethod[]) { |
|
|
|
|
|
this.log(`Registering proxy: ${this.pathPrefix}${path} -> ${upstream}`); |
|
|
this.fastify.register(fastifyHttpProxy, { |
|
|
this.fastify.register(fastifyHttpProxy, { |
|
|
prefix: path, |
|
|
|
|
|
|
|
|
prefix: `${this.pathPrefix}${path}`, |
|
|
beforeHandler: middleware ? handleMiddleware(middleware) : undefined, |
|
|
beforeHandler: middleware ? handleMiddleware(middleware) : undefined, |
|
|
upstream |
|
|
upstream |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Log under the WebServer service |
|
|
|
|
|
*/ |
|
|
|
|
|
public log(...args: any[]) { |
|
|
|
|
|
this.webserver.log(...args); |
|
|
|
|
|
} |
|
|
} |
|
|
} |