2021-04-08 18:39:46 +02:00
|
|
|
// need to load environment first
|
2021-06-24 19:16:48 +02:00
|
|
|
import { ExtendableContext } from "koa"
|
2022-01-24 11:48:59 +01:00
|
|
|
import * as env from "./environment"
|
2022-03-29 17:03:44 +02:00
|
|
|
import db from "./db"
|
|
|
|
db.init()
|
2020-02-03 10:24:25 +01:00
|
|
|
const Koa = require("koa")
|
2020-12-04 13:09:02 +01:00
|
|
|
const destroyable = require("server-destroy")
|
2020-02-03 10:24:25 +01:00
|
|
|
const koaBody = require("koa-body")
|
2021-05-28 11:09:32 +02:00
|
|
|
const pino = require("koa-pino-logger")
|
2020-05-14 16:12:30 +02:00
|
|
|
const http = require("http")
|
2020-05-18 16:33:29 +02:00
|
|
|
const api = require("./api")
|
2020-05-31 18:12:52 +02:00
|
|
|
const eventEmitter = require("./events")
|
2020-09-21 14:49:34 +02:00
|
|
|
const automations = require("./automations/index")
|
2020-07-14 17:00:58 +02:00
|
|
|
const Sentry = require("@sentry/node")
|
2021-03-23 19:04:53 +01:00
|
|
|
const fileSystem = require("./utilities/fileSystem")
|
2021-05-07 13:24:51 +02:00
|
|
|
const bullboard = require("./automations/bullboard")
|
2022-05-31 11:16:22 +02:00
|
|
|
const { logAlert } = require("@budibase/backend-core/logging")
|
2022-08-04 21:23:45 +02:00
|
|
|
const { pinoSettings } = require("@budibase/backend-core")
|
2022-05-30 22:22:06 +02:00
|
|
|
const { Thread } = require("./threads")
|
2022-03-03 14:37:04 +01:00
|
|
|
import redis from "./utilities/redis"
|
2022-01-24 11:48:59 +01:00
|
|
|
import * as migrations from "./migrations"
|
2022-06-30 12:28:52 +02:00
|
|
|
import { events, installation, tenancy } from "@budibase/backend-core"
|
|
|
|
import { createAdminUser, getChecklist } from "./utilities/workerRequests"
|
2020-05-07 15:04:32 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const app = new Koa()
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-05-07 15:04:32 +02:00
|
|
|
// set up top level koa middleware
|
2021-02-09 19:49:12 +01:00
|
|
|
app.use(
|
|
|
|
koaBody({
|
|
|
|
multipart: true,
|
|
|
|
formLimit: "10mb",
|
|
|
|
jsonLimit: "10mb",
|
|
|
|
textLimit: "10mb",
|
|
|
|
enableTypes: ["json", "form", "text"],
|
2021-06-11 19:56:30 +02:00
|
|
|
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
|
2021-02-09 19:49:12 +01:00
|
|
|
})
|
|
|
|
)
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2022-08-04 21:23:45 +02:00
|
|
|
app.use(pino(pinoSettings()))
|
2020-05-04 18:13:57 +02:00
|
|
|
|
2021-05-07 13:24:51 +02:00
|
|
|
if (!env.isTest()) {
|
2021-11-17 17:30:06 +01:00
|
|
|
const plugin = bullboard.init()
|
|
|
|
app.use(plugin)
|
2021-05-07 13:24:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:12:52 +02:00
|
|
|
app.context.eventEmitter = eventEmitter
|
2020-10-12 14:32:52 +02:00
|
|
|
app.context.auth = {}
|
2020-05-24 23:54:08 +02:00
|
|
|
|
2020-05-07 15:04:32 +02:00
|
|
|
// api routes
|
2022-05-30 22:22:06 +02:00
|
|
|
app.use(api.router.routes())
|
2020-05-04 18:13:57 +02:00
|
|
|
|
2021-05-06 20:27:24 +02:00
|
|
|
if (env.isProd()) {
|
2020-10-28 21:35:06 +01:00
|
|
|
env._set("NODE_ENV", "production")
|
2020-07-14 17:00:58 +02:00
|
|
|
Sentry.init()
|
|
|
|
|
2021-06-24 19:16:48 +02:00
|
|
|
app.on("error", (err: any, ctx: ExtendableContext) => {
|
|
|
|
Sentry.withScope(function (scope: any) {
|
|
|
|
scope.addEventProcessor(function (event: any) {
|
2020-07-14 17:00:58 +02:00
|
|
|
return Sentry.Handlers.parseRequest(event, ctx.request)
|
|
|
|
})
|
|
|
|
Sentry.captureException(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:27:27 +02:00
|
|
|
const server = http.createServer(app.callback())
|
2020-12-04 13:09:02 +01:00
|
|
|
destroyable(server)
|
2020-07-16 15:27:27 +02:00
|
|
|
|
2022-05-31 11:16:22 +02:00
|
|
|
let shuttingDown = false,
|
|
|
|
errCode = 0
|
2021-05-24 18:05:46 +02:00
|
|
|
server.on("close", async () => {
|
2022-05-30 22:22:06 +02:00
|
|
|
// already in process
|
|
|
|
if (shuttingDown) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shuttingDown = true
|
|
|
|
if (!env.isTest()) {
|
2021-03-10 18:55:42 +01:00
|
|
|
console.log("Server Closed")
|
|
|
|
}
|
2022-05-30 22:22:06 +02:00
|
|
|
await automations.shutdown()
|
2021-05-24 18:05:46 +02:00
|
|
|
await redis.shutdown()
|
2022-05-10 11:33:59 +02:00
|
|
|
await events.shutdown()
|
2022-05-30 22:22:06 +02:00
|
|
|
await Thread.shutdown()
|
|
|
|
api.shutdown()
|
2022-05-31 11:16:22 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
process.exit(errCode)
|
|
|
|
}
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
2020-07-15 18:25:08 +02:00
|
|
|
|
2021-02-03 17:09:48 +01:00
|
|
|
module.exports = server.listen(env.PORT || 0, async () => {
|
2020-07-16 15:27:27 +02:00
|
|
|
console.log(`Budibase running on ${JSON.stringify(server.address())}`)
|
2021-02-03 17:09:48 +01:00
|
|
|
env._set("PORT", server.address().port)
|
|
|
|
eventEmitter.emitPort(env.PORT)
|
2021-03-23 19:04:53 +01:00
|
|
|
fileSystem.init()
|
2021-05-12 18:37:09 +02:00
|
|
|
await redis.init()
|
2022-05-30 22:46:08 +02:00
|
|
|
|
|
|
|
// run migrations on startup if not done via http
|
|
|
|
// not recommended in a clustered environment
|
|
|
|
if (!env.HTTP_MIGRATIONS && !env.isTest()) {
|
|
|
|
try {
|
|
|
|
await migrations.migrate()
|
|
|
|
} catch (e) {
|
2022-06-06 10:50:06 +02:00
|
|
|
logAlert("Error performing migrations. Exiting.", e)
|
2022-05-30 22:46:08 +02:00
|
|
|
shutdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 12:28:52 +02:00
|
|
|
// check and create admin user if required
|
|
|
|
if (
|
|
|
|
env.SELF_HOSTED &&
|
|
|
|
!env.MULTI_TENANCY &&
|
|
|
|
env.BB_ADMIN_USER_EMAIL &&
|
|
|
|
env.BB_ADMIN_USER_PASSWORD
|
|
|
|
) {
|
|
|
|
const checklist = await getChecklist()
|
|
|
|
if (!checklist?.adminUser?.checked) {
|
2022-06-30 12:40:52 +02:00
|
|
|
try {
|
2022-07-04 19:11:40 +02:00
|
|
|
const tenantId = tenancy.getTenantId()
|
2022-06-30 12:40:52 +02:00
|
|
|
await createAdminUser(
|
|
|
|
env.BB_ADMIN_USER_EMAIL,
|
|
|
|
env.BB_ADMIN_USER_PASSWORD,
|
2022-07-04 19:11:40 +02:00
|
|
|
tenantId
|
2022-06-30 12:40:52 +02:00
|
|
|
)
|
|
|
|
console.log(
|
|
|
|
"Admin account automatically created for",
|
|
|
|
env.BB_ADMIN_USER_EMAIL
|
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
logAlert("Error creating initial admin user. Exiting.", e)
|
|
|
|
shutdown()
|
|
|
|
}
|
2022-06-30 12:28:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 22:46:08 +02:00
|
|
|
// check for version updates
|
|
|
|
await installation.checkInstallVersion()
|
|
|
|
|
|
|
|
// done last - this will never complete
|
2021-09-07 20:06:20 +02:00
|
|
|
await automations.init()
|
2020-07-16 16:19:46 +02:00
|
|
|
})
|
2020-12-04 13:09:02 +01:00
|
|
|
|
2022-01-24 11:48:59 +01:00
|
|
|
const shutdown = () => {
|
2020-12-04 13:09:02 +01:00
|
|
|
server.close()
|
|
|
|
server.destroy()
|
2022-01-24 11:48:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on("uncaughtException", err => {
|
2022-05-30 22:54:24 +02:00
|
|
|
// @ts-ignore
|
|
|
|
// don't worry about this error, comes from zlib isn't important
|
|
|
|
if (err && err["code"] === "ERR_INVALID_CHAR") {
|
|
|
|
return
|
|
|
|
}
|
2022-05-31 11:16:22 +02:00
|
|
|
errCode = -1
|
|
|
|
logAlert("Uncaught exception.", err)
|
2022-01-24 11:48:59 +01:00
|
|
|
shutdown()
|
2020-12-04 13:09:02 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
2022-01-24 11:48:59 +01:00
|
|
|
shutdown()
|
2020-12-04 13:09:02 +01:00
|
|
|
})
|