From d9df2d34c88498067f86d1b89e7929bbdf738fbd Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 13 Dec 2023 11:23:46 +0000 Subject: [PATCH] Adding Koa API back in when automation feature only enabled, so that health check can still be provided, but rest of API is disabled. --- packages/server/src/api/index.ts | 94 +++++++++++++++++--------------- packages/server/src/app.ts | 10 +--- packages/server/src/features.ts | 7 +++ packages/server/src/startup.ts | 21 ++++--- 4 files changed, 73 insertions(+), 59 deletions(-) diff --git a/packages/server/src/api/index.ts b/packages/server/src/api/index.ts index a01e3764f0..56c61ce85a 100644 --- a/packages/server/src/api/index.ts +++ b/packages/server/src/api/index.ts @@ -4,6 +4,7 @@ import currentApp from "../middleware/currentapp" import zlib from "zlib" import { mainRoutes, staticRoutes, publicRoutes } from "./routes" import { middleware as pro } from "@budibase/pro" +import { apiEnabled } from "../features" import migrations from "../middleware/appMigrations" export { shutdown } from "./routes/public" @@ -16,50 +17,53 @@ router.get("/version", ctx => (ctx.body = envCore.VERSION)) router.use(middleware.errorHandling) -router - .use( - compress({ - threshold: 2048, - gzip: { - flush: zlib.constants.Z_SYNC_FLUSH, - }, - deflate: { - flush: zlib.constants.Z_SYNC_FLUSH, - }, - br: false, - }) - ) - // re-direct before any middlewares occur - .redirect("/", "/builder") - .use( - auth.buildAuthMiddleware([], { - publicAllowed: true, - }) - ) - // nothing in the server should allow query string tenants - // the server can be public anywhere, so nowhere should throw errors - // if the tenancy has not been set, it'll have to be discovered at application layer - .use( - auth.buildTenancyMiddleware([], [], { - noTenancyRequired: true, - }) - ) - .use(pro.licensing()) - // @ts-ignore - .use(currentApp) - .use(auth.auditLog) - // @ts-ignore - .use(migrations) +// only add the routes if they are enabled +if (apiEnabled()) { + router + .use( + compress({ + threshold: 2048, + gzip: { + flush: zlib.constants.Z_SYNC_FLUSH, + }, + deflate: { + flush: zlib.constants.Z_SYNC_FLUSH, + }, + br: false, + }) + ) + // re-direct before any middlewares occur + .redirect("/", "/builder") + .use( + auth.buildAuthMiddleware([], { + publicAllowed: true, + }) + ) + // nothing in the server should allow query string tenants + // the server can be public anywhere, so nowhere should throw errors + // if the tenancy has not been set, it'll have to be discovered at application layer + .use( + auth.buildTenancyMiddleware([], [], { + noTenancyRequired: true, + }) + ) + .use(pro.licensing()) + // @ts-ignore + .use(currentApp) + .use(auth.auditLog) + // @ts-ignore + .use(migrations) -// authenticated routes -for (let route of mainRoutes) { - router.use(route.routes()) - router.use(route.allowedMethods()) + // authenticated routes + for (let route of mainRoutes) { + router.use(route.routes()) + router.use(route.allowedMethods()) + } + + router.use(publicRoutes.routes()) + router.use(publicRoutes.allowedMethods()) + + // WARNING - static routes will catch everything else after them this must be last + router.use(staticRoutes.routes()) + router.use(staticRoutes.allowedMethods()) } - -router.use(publicRoutes.routes()) -router.use(publicRoutes.allowedMethods()) - -// WARNING - static routes will catch everything else after them this must be last -router.use(staticRoutes.routes()) -router.use(staticRoutes.allowedMethods()) diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 4c0068be89..f6f1780030 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -9,7 +9,6 @@ import { ServiceType } from "@budibase/types" import { env as coreEnv } from "@budibase/backend-core" coreEnv._set("SERVICE_TYPE", ServiceType.APPS) -import { apiEnabled } from "./features" import createKoaApp from "./koa" import Koa from "koa" import { Server } from "http" @@ -18,12 +17,9 @@ import { startup } from "./startup" let app: Koa, server: Server async function start() { - // if API disabled, could run automations instead - if (apiEnabled()) { - const koa = createKoaApp() - app = koa.app - server = koa.server - } + const koa = createKoaApp() + app = koa.app + server = koa.server // startup includes automation runner - if enabled await startup(app, server) } diff --git a/packages/server/src/features.ts b/packages/server/src/features.ts index e12260ea32..f040cf82a2 100644 --- a/packages/server/src/features.ts +++ b/packages/server/src/features.ts @@ -22,3 +22,10 @@ export function automationsEnabled() { export function apiEnabled() { return featureList.includes(AppFeature.API) } + +export function printFeatures() { + if (!env.APP_FEATURES) { + return + } + console.log(`**** APP FEATURES SET: ${featureList.join(", ")} ****`) +} diff --git a/packages/server/src/startup.ts b/packages/server/src/startup.ts index 9144ff2b36..2db6e5ae6a 100644 --- a/packages/server/src/startup.ts +++ b/packages/server/src/startup.ts @@ -19,11 +19,14 @@ import * as pro from "@budibase/pro" import * as api from "./api" import sdk from "./sdk" import { initialise as initialiseWebsockets } from "./websockets" -import { automationsEnabled } from "./features" +import { automationsEnabled, printFeatures } from "./features" +import Koa from "koa" +import { Server } from "http" +import { AddressInfo } from "net" let STARTUP_RAN = false -async function initRoutes(app: any) { +async function initRoutes(app: Koa) { if (!env.isTest()) { const plugin = await bullboard.init() app.use(plugin) @@ -48,27 +51,31 @@ async function initPro() { }) } -function shutdown(server?: any) { +function shutdown(server?: Server) { if (server) { server.close() server.destroy() } } -export async function startup(app?: any, server?: any) { +export async function startup(app?: Koa, server?: Server) { if (STARTUP_RAN) { return } + printFeatures() STARTUP_RAN = true - if (server && !env.CLUSTER_MODE) { + if (app && server && !env.CLUSTER_MODE) { console.log(`Budibase running on ${JSON.stringify(server.address())}`) - env._set("PORT", server.address().port) + const address = server.address() as AddressInfo + env._set("PORT", address.port) } eventEmitter.emitPort(env.PORT) fileSystem.init() await redis.init() eventInit() - initialiseWebsockets(app, server) + if (app && server) { + initialiseWebsockets(app, server) + } // run migrations on startup if not done via http // not recommended in a clustered environment