Skip to content

Commit

Permalink
feat: build / compile, swagger support & cluster mode
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 6, 2024
1 parent 05c214d commit 69d01b5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ package-lock.json
**/*.bun

# env files
.env
.env

# ignore build
dist/
Binary file modified bun.lockb
Binary file not shown.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"version": "1.0.50",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun run --watch src/index.ts",
"dev": "bun run --watch src/server.ts",
"build": "bun build --minify --target bun --outdir dist src/index.ts",
"compile": "bun build --compile --minify --target bun --outfile server src/index.ts",
"makemigrations": "npx drizzle-kit generate",
"migrate": "npx drizzle-kit migrate"
},
"dependencies": {
"@elysiajs/static": "^1.1.1",
"@elysiajs/swagger": "^1.1.1",
"drizzle-orm": "^0.33.0",
"elysia": "latest",
"postgres": "^3.4.4"
Expand Down
10 changes: 9 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import os from "node:os";

export const APP_PORT = parseInt(process.env.APP_PORT || "3000")
export const APP_WORKERS = parseInt(process.env.APP_WORKERS || os.availableParallelism().toString());

// Database configuration
export const DB_HOST = process.env.DB_HOST
export const DB_PORT = parseInt(process.env.DB_PORT || "5432")
export const DB_USER = process.env.DB_USER
export const DB_PASSWORD = process.env.DB_PASSWORD
export const DB_NAME = process.env.DB_NAME
export const DB_NAME = process.env.DB_NAME

// Swagger configuration
export const SWAGGER_PATH = "/documents"
30 changes: 17 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Elysia } from "elysia";
import exampleApp from "./example_app/routers";
import { APP_PORT } from "./config";
import staticPlugin from "@elysiajs/static";
import cluster from "node:cluster";
import process from "node:process";
import { APP_WORKERS } from "./config";

const app = new Elysia()
// static plugin
// more information: https://elysiajs.com/plugins/static
.use(staticPlugin())
.use(exampleApp)
.listen(APP_PORT);
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);

for (let i = 0; i < APP_WORKERS; i++) {
cluster.fork();
}

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
cluster.on("exit", (worker) => {
console.log(`worker ${worker.process.pid} died`);
process.exit(1);
});
} else {
await import("./server");
console.log(`Worker ${process.pid} started`);
}
20 changes: 20 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Elysia } from "elysia";
import staticPlugin from "@elysiajs/static";
import { swagger } from '@elysiajs/swagger'
import { APP_PORT, SWAGGER_PATH } from "./config";
import exampleApp from "./example_app/routers";

const app = new Elysia()
// static plugin
// more information: https://elysiajs.com/plugins/static
.use(staticPlugin())
// swagger plugin
.use(swagger({
path: SWAGGER_PATH
}))
.use(exampleApp)
.listen(APP_PORT);

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

0 comments on commit 69d01b5

Please sign in to comment.