diff --git a/docker/Dockerfile b/docker/Dockerfile index 28e3f90..7c308fd 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -6,7 +6,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certifi WORKDIR /opt/delivr/api COPY ./build/bin/delivr-api-linux-x64-baseline /opt/delivr/api/app/delivr-api -COPY ./drizzle/migrations ./app/drizzle/migrations RUN chmod u+x /opt/delivr/api/app/delivr-api @@ -19,7 +18,7 @@ ENV DLA_API_PORT=14123 ENV DLA_DB_CONNECTION_URL=/opt/delivr/api/data/db.sqlite ENV DLA_DB_AUTO_MIGRATE=true -ENV DLA_DB_MIGRATION_DIR=/opt/delivr/api/app/drizzle/migrations/sqlite +ENV DLA_DB_MIGRATION_DIR=sqlite ENV DLA_LOG_DIR=/opt/delivr/api/data/logs ENV DLA_CONFIG_BASE_DIR=/opt/delivr/api/config @@ -30,4 +29,4 @@ VOLUME /opt/delivr/api/config HEALTHCHECK --interval=30s --timeout=5s --start-period=20s \ CMD curl -f http://localhost:14123/health || exit 1 -ENTRYPOINT ["/opt/delivr/api/app/delivr-api"] \ No newline at end of file +ENTRYPOINT ["/opt/delivr/api/app/delivr-api"] diff --git a/scripts/compile/compiler.ts b/scripts/compile/compiler.ts index cd5dbe3..c9524ae 100644 --- a/scripts/compile/compiler.ts +++ b/scripts/compile/compiler.ts @@ -1,3 +1,4 @@ +import { AppConstants } from "../../src/utils/constants"; export enum Platforms { "linux-x64" = "bun-linux-x64-modern", @@ -9,13 +10,14 @@ export type PlatformArg = keyof typeof Platforms | "auto"; class CompilerCommand { - public sourcemap = true; - public minify = true; - public entrypoint = "./scripts/entrypoint.ts"; - public outfile = "./build/bin/delivr-api"; - public platform: PlatformArg = "auto"; - public env: NodeJS.ProcessEnv = {}; - private additionalArgs: string[] = []; + public sourcemap = true; + public minify = true; + public bytecode = true; + public entrypoint = "./scripts/entrypoint.ts"; + public outfile = `./build/bin/${AppConstants.BINARY_NAME}`; + public platform: PlatformArg = "auto"; + public env: NodeJS.ProcessEnv = {}; + private additionalArgs: string[] = []; constructor(private baseCommand = "bun build --compile") {} @@ -48,30 +50,32 @@ export class Compiler { versionInFileName: boolean ) { if (versionInFileName) { - this.command.outfile += `-v${version}`; + this.command.outfile += `-v${this.version}`; } this.command.platform = platform; if (platform !== "auto") { - if (Object.keys(Platforms).some(p => p === platform) === false) { + if (!Object.keys(Platforms).some((p) => p === platform)) { throw new Error(`Invalid platform: ${platform}`); } this.command.outfile += `-${platform}`; } - this.command.env.APP_VERSION = version; - } + this.command.env.APP_VERSION = this.version; + + this.command.addArg("--asset ./drizzle/migrations"); + } async build() { try { - const output = await Bun.$` - echo "Building from sources. Version: ${this.version} Platform: ${this.platform}"; - ${{ raw: this.command.getCommand() }} - `.text(); + console.log(`Building from sources. Version: ${this.version} Platform: ${this.platform}`); + + const output = await Bun.$`${{ raw: this.command.getCommand() }}`.text(); + console.log(output); } catch (err: any) { - console.log(`Failed: ${err.message}`); + console.log(`Compiling Failed:\n`, err); } } diff --git a/src/db/index.ts b/src/db/index.ts index 740e0a4..c22dc2e 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -6,7 +6,7 @@ import { Logger } from '../utils/logger'; import { ConfigHandler } from '../utils/config'; import { migrate } from 'drizzle-orm/bun-sqlite/migrator'; import { mkdir as fs_mkdir } from 'fs/promises'; -import { dirname as path_dirname, isAbsolute as path_isAbsolute, resolve as path_resolve } from "path"; +import { dirname as path_dirname, join as path_join } from "path"; export class DB { @@ -25,10 +25,25 @@ export class DB { if (autoMigrate) { Logger.info("Running database migrations..."); - // In a compiled binary, import.meta.dir is Bun's virtual /$bunfs/root, but the - // migrations ship on disk next to the executable — resolve relative paths there. - if (Bun?.isStandaloneExecutable && !path_isAbsolute(migrationsFolder)) { - migrationsFolder = path_resolve(path_dirname(process.execPath), migrationsFolder); + //@ts-ignore + if (Bun?.isStandaloneExecutable) { + // `bun build --compile --asset ./drizzle/migrations` embeds the files as `migrations/...` + // next to the entry (`/$bunfs/root/migrations` on Linux) — without the `drizzle/` segment. + + let useDefaultMigrationsFolder = true; + + for (const backend of ["sqlite", "postgresql", "mysql"]) { + if (migrationsFolder.includes(backend)) { + migrationsFolder = path_join(import.meta.dir, "migrations", backend); + useDefaultMigrationsFolder = false; + break; + } + } + + if (useDefaultMigrationsFolder) { + Logger.info("No backend-specific migrations folder found in the provided path. Using default SQLite migrations folder."); + migrationsFolder = path_join(import.meta.dir, "migrations", "sqlite"); + } } await migrate(this.db, { migrationsFolder }); diff --git a/src/utils/constants.ts b/src/utils/constants.ts new file mode 100644 index 0000000..4dde16b --- /dev/null +++ b/src/utils/constants.ts @@ -0,0 +1,17 @@ +export namespace AppConstants { + export const APP_NAME = "Delivr-API"; + + export const APP_ENV_PREFIX = "DLA"; + + export const APP_KEYS_PREFIX = "dla"; + + export const APP_API_DEFAULT_PORT = 14123; + + export const APP_API_DEFAULT_PROD_URL = `https://api.${AppConstants.APP_NAME.toLowerCase()}.is-on.net`; + + export const DEFAULT_EMAIL_FROM_HOST = "appname.local"; + + export const DEFAULT_SMTP_FROM = `\"${AppConstants.APP_NAME}\" `; + + export const BINARY_NAME = "delivr-api"; +}