This project serves as a practical guide to developing and deploying web applications using Bun, a modern JavaScript runtime, along with containerization through Docker and deployment via Fly.io. Whether you're a developer looking to adopt these technologies or an engineer focusing on DevOps practices, this demo offers an end-to-end example to kickstart your journey.
Clone the Repository
git clone https://github.com/jellydn/elysia-demo-app.git
Install Dependencies
bun install
Run Dev Locally
bun run dev
Open http://localhost:3000/ with your browser to see the result.
Run Test Locally
bun run test
This project uses pre-commit to enforce code quality. To install pre-commit hooks, run:
pre-commit install --hook-type pre-commit --hook-type pre-push
End-to-end type-safety is a robust approach that ensures type consistency across every component of your system. By leveraging this, you can catch type mismatches early in the development cycle, reducing runtime errors and ensuring predictable system behavior.
Elysia's Eden plugin is specifically designed to achieve end-to-end type safety between the client and the server.
To utilize Eden's capabilities, you need to install it on your frontend:
bun add elysia @elysiajs/eden
TIP: Eden requires Elysia for type inference. Install Elysia as a dev dependency.
// server.ts
import { Elysia, t } from "elysia";
export type App = typeof app;
Once you've installed and configured Eden, you can consume the Elysia API on the client-side as follows:
// client.ts
import { edenTreaty } from "@elysiajs/eden";
import type { App } from "./server";
const client = edenTreaty<App>("http://localhost:8080");
Elysia promotes the use of method chaining for better type inference and cleaner code.
// Example
const app = new Elysia()
.state("build", 1)
.get("/", ({ store: { build } }) => build)
.listen(3000);
With method chaining, you maintain type safety throughout your code, ensuring that each method you chain is aware of the types introduced by the preceding methods.
Before running the application with Docker, make sure you have Docker and Docker Compose installed.
docker build -t elysia-demo-app .
Create a network named web
if you haven't already:
docker network create web
Start the services:
docker-compose up
You should now be able to access the application at http://elysia-demo-app.local
.
Make sure you have added the following to your /etc/hosts
file.
127.0.0.1 elysia-demo-app.local
If you have your project wrapped in a Docker container, deploying to Fly.io is straightforward.
Run the following command to initiate deployment:
fly launch
During this process, you'll be prompted to:
The fly.toml
configuration file will be generated automatically.
Most Docker applications require some environment variables. You can add these in the [env]
block in the fly.toml
file:
[env]
MY_SPECIAL_ENV = "some_value"
MAX_PLAYER_COUNT = "15"
For sensitive data, use flyctl
to set up secrets:
flyctl secrets set MY_SECRET=romance
To list existing secrets, use:
flyctl secrets list
If you didn't deploy your application during the initial setup, you can deploy it now:
fly deploy
If you prefer to build the Docker image locally, use:
fly deploy --local-only
To open your deployed app in a browser, run:
fly apps open
For applications that require persistent storage, consider:
DATABASE_URL
environment variable when attached to your app.👤 Huynh Duc Dung
If this guide has been helpful, please give it a ⭐️.