1
0
Fork 0
mirror of synced 2024-07-08 07:46:10 +12:00

Adding Koa API back in when automation feature only enabled, so that health check can still be provided, but rest of API is disabled.

This commit is contained in:
mike12345567 2023-12-13 11:23:46 +00:00
parent a828881f87
commit d9df2d34c8
4 changed files with 73 additions and 59 deletions

View file

@ -4,6 +4,7 @@ import currentApp from "../middleware/currentapp"
import zlib from "zlib" import zlib from "zlib"
import { mainRoutes, staticRoutes, publicRoutes } from "./routes" import { mainRoutes, staticRoutes, publicRoutes } from "./routes"
import { middleware as pro } from "@budibase/pro" import { middleware as pro } from "@budibase/pro"
import { apiEnabled } from "../features"
import migrations from "../middleware/appMigrations" import migrations from "../middleware/appMigrations"
export { shutdown } from "./routes/public" export { shutdown } from "./routes/public"
@ -16,50 +17,53 @@ router.get("/version", ctx => (ctx.body = envCore.VERSION))
router.use(middleware.errorHandling) router.use(middleware.errorHandling)
router // only add the routes if they are enabled
.use( if (apiEnabled()) {
compress({ router
threshold: 2048, .use(
gzip: { compress({
flush: zlib.constants.Z_SYNC_FLUSH, threshold: 2048,
}, gzip: {
deflate: { flush: zlib.constants.Z_SYNC_FLUSH,
flush: zlib.constants.Z_SYNC_FLUSH, },
}, deflate: {
br: false, flush: zlib.constants.Z_SYNC_FLUSH,
}) },
) br: false,
// re-direct before any middlewares occur })
.redirect("/", "/builder") )
.use( // re-direct before any middlewares occur
auth.buildAuthMiddleware([], { .redirect("/", "/builder")
publicAllowed: true, .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 // nothing in the server should allow query string tenants
.use( // the server can be public anywhere, so nowhere should throw errors
auth.buildTenancyMiddleware([], [], { // if the tenancy has not been set, it'll have to be discovered at application layer
noTenancyRequired: true, .use(
}) auth.buildTenancyMiddleware([], [], {
) noTenancyRequired: true,
.use(pro.licensing()) })
// @ts-ignore )
.use(currentApp) .use(pro.licensing())
.use(auth.auditLog) // @ts-ignore
// @ts-ignore .use(currentApp)
.use(migrations) .use(auth.auditLog)
// @ts-ignore
.use(migrations)
// authenticated routes // authenticated routes
for (let route of mainRoutes) { for (let route of mainRoutes) {
router.use(route.routes()) router.use(route.routes())
router.use(route.allowedMethods()) 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())

View file

@ -9,7 +9,6 @@ import { ServiceType } from "@budibase/types"
import { env as coreEnv } from "@budibase/backend-core" import { env as coreEnv } from "@budibase/backend-core"
coreEnv._set("SERVICE_TYPE", ServiceType.APPS) coreEnv._set("SERVICE_TYPE", ServiceType.APPS)
import { apiEnabled } from "./features"
import createKoaApp from "./koa" import createKoaApp from "./koa"
import Koa from "koa" import Koa from "koa"
import { Server } from "http" import { Server } from "http"
@ -18,12 +17,9 @@ import { startup } from "./startup"
let app: Koa, server: Server let app: Koa, server: Server
async function start() { async function start() {
// if API disabled, could run automations instead const koa = createKoaApp()
if (apiEnabled()) { app = koa.app
const koa = createKoaApp() server = koa.server
app = koa.app
server = koa.server
}
// startup includes automation runner - if enabled // startup includes automation runner - if enabled
await startup(app, server) await startup(app, server)
} }

View file

@ -22,3 +22,10 @@ export function automationsEnabled() {
export function apiEnabled() { export function apiEnabled() {
return featureList.includes(AppFeature.API) return featureList.includes(AppFeature.API)
} }
export function printFeatures() {
if (!env.APP_FEATURES) {
return
}
console.log(`**** APP FEATURES SET: ${featureList.join(", ")} ****`)
}

View file

@ -19,11 +19,14 @@ import * as pro from "@budibase/pro"
import * as api from "./api" import * as api from "./api"
import sdk from "./sdk" import sdk from "./sdk"
import { initialise as initialiseWebsockets } from "./websockets" 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 let STARTUP_RAN = false
async function initRoutes(app: any) { async function initRoutes(app: Koa) {
if (!env.isTest()) { if (!env.isTest()) {
const plugin = await bullboard.init() const plugin = await bullboard.init()
app.use(plugin) app.use(plugin)
@ -48,27 +51,31 @@ async function initPro() {
}) })
} }
function shutdown(server?: any) { function shutdown(server?: Server) {
if (server) { if (server) {
server.close() server.close()
server.destroy() server.destroy()
} }
} }
export async function startup(app?: any, server?: any) { export async function startup(app?: Koa, server?: Server) {
if (STARTUP_RAN) { if (STARTUP_RAN) {
return return
} }
printFeatures()
STARTUP_RAN = true STARTUP_RAN = true
if (server && !env.CLUSTER_MODE) { if (app && server && !env.CLUSTER_MODE) {
console.log(`Budibase running on ${JSON.stringify(server.address())}`) 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) eventEmitter.emitPort(env.PORT)
fileSystem.init() fileSystem.init()
await redis.init() await redis.init()
eventInit() eventInit()
initialiseWebsockets(app, server) if (app && server) {
initialiseWebsockets(app, server)
}
// run migrations on startup if not done via http // run migrations on startup if not done via http
// not recommended in a clustered environment // not recommended in a clustered environment