0. ๐ ์ โBun & ElysiaJSโ ์กฐํฉ์ธ๊ฐ์?
Node.js ๊ธฐ๋ฐ ์๋ฒ์์๋ ๋น๋ ๋๊ตฌ(tsc/tsx), ํ
์คํธ ๋ฌ๋(vitest), SQLite ๋๋ผ์ด๋ฒ, OpenAPI ๋ฌธ์ ์์ฑ๊ธฐ๋ฅผ ๊ฐ๊ฐ ์กฐ๋ฆฝํด์ผ ํฉ๋๋ค. Bun v1.4.5๋ ์ด ๋ชจ๋ ๊ฒ์ ๋จ์ผ ์ด๊ณ ์ ๋ฐ์ด๋๋ฆฌ์ ๋ด์ฅํ๊ณ , ElysiaJS v1.4.32๋ Bun์ ๋ค์ดํฐ๋ธ Bun.serve ์์์ ๋์ํ๋ฉฐ ์ฝ๋ ์์ฑ ์์ด ํ๋ก ํธ์๋๊น์ง ํ์
์ ๊ณต์ (Eden Treaty) ํ ์ ์๋ ํ์
์์ API ํ๋ ์์ํฌ์
๋๋ค.
1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ๋ฐํ์ / ํจํค์ง | Bun v1.4.0+ | Bun v1.4.5 + ElysiaJS v1.4.32 | TypeScript 5.6+ ๋ค์ดํฐ๋ธ ์คํ |
| CPU / RAM | 1 vCPU / 512 MB RAM | 2~4 vCPU / 2 GB+ RAM | ์ด์ ๋ฉ๋ชจ๋ฆฌยท์ด๊ณ ์ ์ฝ๋ ์คํํธ |
# 1. Bun ์ต์ ๋ฒ์ ์ค์น (macOS / Linux)
curl -fsSL https://bun.sh/install | bash
# Windows PowerShell: powershell -c "irm bun.sh/install.ps1 | iex"
bun --version
# 1.4.5
# 2. ElysiaJS ํ๋ก์ ํธ ์์ฑ
bun create elysia elysia-api && cd elysia-api
bun add @elysiajs/openapi @elysiajs/eden
[1๊ฐ] ElysiaJS ์คํค๋ง ๊ฒ์ฆ(t.Object) & ๋ด์ฅ bun:sqlite ์ฐ๋
Elysia์ t (TypeBox ๊ธฐ๋ฐ) ์คํค๋ง๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐํ์ ์ ํจ์ฑ ๊ฒ์ฌ์ TypeScript ์ ์ ํ์
์ถ๋ก , OpenAPI ์คํ์ด ๋จ ํ ๋ฒ์ ์ ์ธ์ผ๋ก ๋์์ ์์ฑ๋ฉ๋๋ค.
// src/index.ts
import { Elysia, t } from "elysia";
import { Database } from "bun:sqlite";
import { openapi } from "@elysiajs/openapi";
const db = new Database("app.sqlite", { create: true });
db.run(`
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
done INTEGER DEFAULT 0
)
`);
const app = new Elysia()
.use(openapi()) // http://localhost:3000/openapi ์๋ ๋ฌธ์ ์์ฑ
.get("/health", () => ({ ok: true, runtime: `Bun ${Bun.version}` }))
.get("/tasks", () => db.query("SELECT * FROM tasks ORDER BY id DESC").all())
.post(
"/tasks",
({ body, set }) => {
const stmt = db.query("INSERT INTO tasks (title) VALUES (?) RETURNING *");
const created = stmt.get(body.title);
set.status = 201;
return created;
},
{
body: t.Object({
title: t.String({ minLength: 2, maxLength: 100 }),
}),
}
)
.listen(3000);
export type App = typeof app;
console.log(`๐ฆ Elysia running at http://localhost:${app.server?.port}`);
[2๊ฐ] ์๋ช
์ฃผ๊ธฐ ํ
(derive, onError) & ๋ชจ๋ํ ๋ผ์ฐํฐ ๋ถ๋ฆฌ
์์ฒญ ์ปจํ
์คํธ์ ์ธ์ฆ๋ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์ฃผ์
(derive)ํ๊ณ , ๋๋ฉ์ธ ์๋ฌ๋ฅผ ์ ์ญ onError ํธ๋ค๋ฌ๋ก ๊น๋ํ๊ฒ ๋ณํํฉ๋๋ค.
// src/auth-plugin.ts
import { Elysia } from "elysia";
export const authPlugin = new Elysia({ name: "auth-plugin" })
.derive({ as: "global" }, ({ headers }) => {
const auth = headers.authorization;
if (!auth?.startsWith("Bearer ")) {
return { currentUser: null };
}
return { currentUser: { id: "u-101", role: "admin" } };
})
.onBeforeHandle(({ currentUser, set, path }) => {
if (path.startsWith("/admin") && !currentUser) {
set.status = 401;
return { error: "UNAUTHORIZED", message: "๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค." };
}
});
[3๊ฐ] Eden Treaty: ์ ๋ก ์ฝ๋์ End-to-End ํ์ ์์ ํด๋ผ์ด์ธํธ
๋ฐฑ์๋์์ export type App = typeof app๋ง ๋ด๋ณด๋ด๋ฉด, ํ๋ก ํธ์๋๋ ๋ง์ดํฌ๋ก์๋น์ค ํด๋ผ์ด์ธํธ์์ tRPC์ฒ๋ผ ์๋์์ฑ๊ณผ ์ปดํ์ผ ํ์ ํ์
์ฒดํฌ๋ฅผ ๋๋ฆด ์ ์์ต๋๋ค.
// client.ts (ํ๋ก ํธ์๋ ๋๋ BFF ์๋น์ค)
import { treaty } from "@elysiajs/eden";
import type { App } from "./src/index";
const api = treaty<App>("localhost:3000");
async function main() {
// POST /tasks์ body ํ์
({ title: string })์ด ์๋ ์ถ๋ก ๋จ!
const { data, error } = await api.tasks.post({
title: "Elysia Eden Treaty ์ค์ ๊ฒ์ฆ",
});
if (error) {
console.error("์์ฒญ ์คํจ:", error.status, error.value);
return;
}
console.log("์์ฑ๋ Task:", data);
}
main();
[4๊ฐ] Bun ๋ค์ดํฐ๋ธ WebSocket Pub/Sub ์ค์๊ฐ ์ฑํ & ๋จ์ผ ๋ฐ์ด๋๋ฆฌ ๋น๋
Elysia๋ Bun์ ๋ด์ฅ uWebSockets ํธํ ์์ง์ ๊ทธ๋๋ก ๊ฐ์ธ ์ด๋น ์์ญ๋ง ๋ฉ์์ง๋ฅผ ์ฒ๋ฆฌํ๋ Pub/Sub ํ ํฝ ๊ตฌ๋
์ ์ ๊ณตํฉ๋๋ค.
// src/ws-chat.ts
import { Elysia, t } from "elysia";
export const wsRoutes = new Elysia().ws("/ws/chat/:room", {
body: t.Object({ sender: t.String(), text: t.String() }),
open(ws) {
const room = `room:${ws.data.params.room}`;
ws.subscribe(room);
ws.publish(room, { system: true, text: "์ ์ฌ์ฉ์๊ฐ ์
์ฅํ์ต๋๋ค." });
},
message(ws, message) {
const room = `room:${ws.data.params.room}`;
ws.publish(room, { sender: message.sender, text: message.text });
},
});
# ํ๋ก๋์
๋จ์ผ ์คํ ๋ฐ์ด๋๋ฆฌ๋ก ์ปดํ์ผ (Docker scratch/distroless ๋ฐฐํฌ ์ต์ ํ)
bun build --compile --minify --sourcemap ./src/index.ts --outfile server
./server
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์