A full-stack web application built with SvelteKit and Bun, featuring user authentication and a simple counter functionality.
svelte-bun is a modern full-stack web application that demonstrates best practices for building web apps with cutting-edge technologies. It showcases:
This project serves as a reference implementation for building modern web applications and can be used as a starting point for new projects.
@neondatabase/serverless (for Cloudflare Workers/Pages)postgres (for local development)git clone https://github.com/mbe24/svelte-bun.git
cd svelte-bun
bun install
# or
npm install
Copy the example environment file and configure your credentials:
cp .env.example .env
Edit the .env file and update the credentials (use secure passwords in production):
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_secure_password_here
POSTGRES_DB=sveltekit_db
DATABASE_URL=postgresql://postgres:your_secure_password_here@localhost:5432/sveltekit_db
⚠️ Security Note: Never commit the .env file to version control. It contains sensitive credentials and is already excluded via .gitignore.
For more details on security configuration, see docs/SECURITY.md.
Make sure PostgreSQL is running, then generate and run migrations:
# Generate migrations from schema
bun run db:generate
# Run migrations
bun run db:push
bun run dev
# or
npm run dev
Open http://localhost:5173 in your browser.
bun run dev - Start the development serverbun run build - Build the application for productionbun run preview - Preview the production buildbun run check - Run TypeScript and Svelte checksbun run db:generate - Generate Drizzle migrationsbun run db:push - Push schema changes to the databasebun run db:studio - Open Drizzle Studio (database GUI)bun test - Run unit testsThis application is optimized for deployment on Cloudflare Pages with automatic edge runtime compatibility.
Key Features:
Prerequisites:
Quick Start:
Create a free Neon PostgreSQL database
Get your connection string from Neon dashboard
Follow the detailed guide in docs/DEPLOY.md
Critical: Add DATABASE_URL to Cloudflare Pages Settings → Environment variables (both Production and Preview)
Push to GitHub - automatic deployment via GitHub Actions
⭐ Run migrations by visiting this URL in your browser:
https://your-app.pages.dev/api/admin/migrate
Click the "Run Database Migration" button - that's it!
Alternative methods:
curl -X POST https://your-app.pages.dev/api/admin/migratenpm run db:push with DATABASE_URL set⚠️ Important:
/api/admin/migrate in your browser and click the button to fix this!See docs/DEPLOY.md for complete step-by-step instructions, including separate database setup for preview/production environments.
Important: Before running Docker Compose, create a .env file with your credentials:
cp .env.example .env
# Edit .env with your actual credentials
Make sure your .env file includes:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_secure_password_here
POSTGRES_DB=sveltekit_db
DATABASE_URL=postgresql://postgres:your_secure_password_here@db:5432/sveltekit_db
Note: Use @db:5432 (not @localhost:5432) in DATABASE_URL for Docker Compose.
Then start the services:
# Build and start all services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
The application will be available at http://localhost:3000.
For more details on security configuration, see docs/SECURITY.md.
# Build the image
docker build -t svelte-bun-app .
# Run the container (replace with your actual credentials)
docker run -p 3000:3000 \
-e DATABASE_URL=postgresql://username:password@host:5432/sveltekit_db \
svelte-bun-app
svelte-bun/
├── src/
│ ├── lib/
│ │ ├── auth/ # Authentication utilities
│ │ └── db/ # Database schema and connection
│ ├── routes/
│ │ ├── api/ # API endpoints
│ │ │ ├── auth/ # Authentication endpoints
│ │ │ └── counter/ # Counter endpoints
│ │ ├── login/ # Login page
│ │ ├── register/ # Registration page
│ │ ├── counter/ # Counter page (protected)
│ │ └── +page.svelte # Home page
│ ├── app.d.ts # TypeScript definitions
│ ├── app.html # HTML template
│ └── hooks.server.ts # Server hooks for authentication
├── drizzle/ # Database migrations
├── drizzle.config.ts # Drizzle configuration
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Docker image configuration
└── package.json # Project dependencies
id (serial, primary key)username (text, unique, not null)password (text, not null) - bcrypt hashedcreated_at (timestamp, default now)id (text, primary key)user_id (integer, foreign key to users)expires_at (timestamp, not null)id (serial, primary key)user_id (integer, foreign key to users, unique)value (integer, default 0)updated_at (timestamp, default now)POST /api/auth/register - Register a new userPOST /api/auth/login - Login with username and passwordPOST /api/auth/logout - Logout and delete sessionGET /api/counter - Get current counter value (requires authentication)POST /api/counter - Increment or decrement counter (requires authentication){ "action": "increment" | "decrement" }bun test
# Install Playwright browsers
bunx playwright install
# Run E2E tests
bun run test:e2e
For detailed information about security configuration:
DATABASE_URL in your .env file is correctsveltekit_db existsvite.config.ts or use the --port flag:bun run dev -- --port 3001
docker system prune -aContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Apache License 2.0