Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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"]
ENTRYPOINT ["/opt/delivr/api/app/delivr-api"]
36 changes: 20 additions & 16 deletions scripts/compile/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AppConstants } from "../../src/utils/constants";

export enum Platforms {
"linux-x64" = "bun-linux-x64-modern",
Expand All @@ -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") {}

Expand Down Expand Up @@ -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);
}
}

Expand Down
25 changes: 20 additions & 5 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 });
Expand Down
17 changes: 17 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -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}\" <noreply@${AppConstants.DEFAULT_EMAIL_FROM_HOST}>`;

export const BINARY_NAME = "delivr-api";
}
Loading