Bun Hono Backend API โ
HaloLight Bun Backend API is built on Bun + Hono + Drizzle ORM, providing ultra-high-performance backend service.
API Documentation: https://halolight-api-bun.h7ml.cn/docs
GitHub: https://github.com/halolight/halolight-api-bun
Features โ
- ๐ JWT Dual Token - Access Token + Refresh Token, automatic renewal
- ๐ก๏ธ RBAC Authorization - Role-based access control with wildcard matching
- ๐ก RESTful API - Standardized interface design, OpenAPI documentation
- ๐๏ธ Drizzle ORM - Type-safe database operations
- โ Data Validation - Request parameter validation, error handling
- ๐ Logging System - Request logging, error tracking
- ๐ณ Docker Support - Containerized deployment
- โก Extreme Performance - 4x faster than Node.js
Tech Stack โ
| Technology | Version | Description |
|---|---|---|
| Bun | 1.1+ | Runtime |
| Hono | 4.x | Web Framework |
| Drizzle ORM | 0.36+ | Database ORM |
| PostgreSQL | 15+ | Data Storage |
| Zod | 3.x | Data Validation |
| JWT | - | Authentication |
| Swagger | - | API Documentation |
Quick Start โ
Requirements โ
- Bun >= 1.1
- pnpm >= 8.0
- PostgreSQL (optional, defaults to SQLite)
Installation โ
bash
# Clone repository
git clone https://github.com/halolight/halolight-api-bun.git
cd halolight-api-bun
# Install dependencies
pnpm installEnvironment Variables โ
bash
cp .env.example .envenv
# Database
DATABASE_URL=postgresql://user:password@localhost:5432/halolight
# JWT Secrets
JWT_SECRET=your-super-secret-key
JWT_ACCESS_EXPIRES=15m
JWT_REFRESH_EXPIRES=7d
# Service Config
PORT=3002
NODE_ENV=development
CORS_ORIGIN=http://localhost:3000
API_PREFIX=/apiDatabase Initialization โ
bash
bun run db:push
bun run db:seedStart Service โ
bash
# Development mode
bun run dev
# Production mode
bun run build
bun run startVisit http://localhost:3002
Project Structure โ
halolight-api-bun/
โโโ src/
โ โโโ routes/ # Controllers/Route handlers
โ โโโ services/ # Business logic layer
โ โโโ db/ # Data models
โ โโโ middleware/ # Middleware
โ โโโ utils/ # Utility functions
โ โโโ index.ts # Application entry
โโโ test/ # Test files
โโโ Dockerfile # Docker configuration
โโโ docker-compose.yml
โโโ package.jsonAPI Modules โ
Authentication Endpoints โ
| Method | Path | Description | Auth |
|---|---|---|---|
| POST | /api/auth/login | User login | Public |
| POST | /api/auth/register | User registration | Public |
| POST | /api/auth/refresh | Refresh token | Public |
| POST | /api/auth/logout | User logout | Required |
| POST | /api/auth/forgot-password | Forgot password | Public |
| POST | /api/auth/reset-password | Reset password | Public |
User Management Endpoints โ
| Method | Path | Description | Auth |
|---|---|---|---|
| GET | /api/users | Get user list | users:view |
| GET | /api/users/:id | Get user details | users:view |
| POST | /api/users | Create user | users:create |
| PUT | /api/users/:id | Update user | users:update |
| DELETE | /api/users/:id | Delete user | users:delete |
| GET | /api/users/me | Get current user | Required |
Complete Endpoint List โ
Document Management (Documents) - 5 endpoints โ
| Method | Path | Description |
|---|---|---|
| GET | /api/documents | Get document list |
| GET | /api/documents/:id | Get document details |
| POST | /api/documents | Create document |
| PUT | /api/documents/:id | Update document |
| DELETE | /api/documents/:id | Delete document |
File Management (Files) - 5 endpoints โ
| Method | Path | Description |
|---|---|---|
| GET | /api/files | Get file list |
| GET | /api/files/:id | Get file details |
| POST | /api/files/upload | Upload file |
| PUT | /api/files/:id | Update file info |
| DELETE | /api/files/:id | Delete file |
Message Management (Messages) - 5 endpoints โ
| Method | Path | Description |
|---|---|---|
| GET | /api/messages | Get message list |
| GET | /api/messages/:id | Get message details |
| POST | /api/messages | Send message |
| PUT | /api/messages/:id/read | Mark as read |
| DELETE | /api/messages/:id | Delete message |
Notification Management (Notifications) - 4 endpoints โ
| Method | Path | Description |
|---|---|---|
| GET | /api/notifications | Get notification list |
| PUT | /api/notifications/:id/read | Mark as read |
| PUT | /api/notifications/read-all | Mark all as read |
| DELETE | /api/notifications/:id | Delete notification |
Calendar Management (Calendar) - 5 endpoints โ
| Method | Path | Description |
|---|---|---|
| GET | /api/calendar/events | Get event list |
| GET | /api/calendar/events/:id | Get event details |
| POST | /api/calendar/events | Create event |
| PUT | /api/calendar/events/:id | Update event |
| DELETE | /api/calendar/events/:id | Delete event |
Dashboard (Dashboard) - 6 endpoints โ
| Method | Path | Description |
|---|---|---|
| GET | /api/dashboard/stats | Statistics data |
| GET | /api/dashboard/visits | Visit trends |
| GET | /api/dashboard/sales | Sales data |
| GET | /api/dashboard/pie | Pie chart data |
| GET | /api/dashboard/tasks | Pending tasks |
| GET | /api/dashboard/calendar | Today's events |
Authentication Mechanism โ
JWT Dual Token โ
Access Token: 15 minutes validity, for API requests
Refresh Token: 7 days validity, for refreshing Access TokenRequest Header โ
http
Authorization: Bearer <access_token>Refresh Flow โ
typescript
// Refresh token example
const response = await fetch('/api/auth/refresh', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
refreshToken: 'your_refresh_token'
})
});
const { accessToken, refreshToken } = await response.json();Authorization System โ
Role Definitions โ
| Role | Description | Permissions |
|---|---|---|
super_admin | Super Administrator | * (all permissions) |
admin | Administrator | users:*, documents:*, ... |
user | Regular User | documents:view, files:view, ... |
guest | Guest | dashboard:view |
Permission Format โ
{resource}:{action}
Examples:
- users:view # View users
- users:create # Create users
- users:* # All user operations
- * # All permissionsError Handling โ
Error Response Format โ
json
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "Request validation failed",
"details": [
{ "field": "email", "message": "Invalid email format" }
]
}
}Error Codes โ
| Status | Error Code | Description |
|---|---|---|
| 400 | VALIDATION_ERROR | Validation failed |
| 401 | UNAUTHORIZED | Unauthorized |
| 403 | FORBIDDEN | Forbidden |
| 404 | NOT_FOUND | Resource not found |
| 409 | CONFLICT | Resource conflict |
| 500 | INTERNAL_ERROR | Server error |
Common Commands โ
bash
# Development
bun run dev # Start development server
bun run build # Production build
bun run start # Run production build
# Build
bun run build # Build production version
# Testing
bun test # Run unit tests
bun test --coverage # Generate coverage report
# Database
bun run db:push # Push schema to database
bun run db:generate # Generate migration files
bun run db:migrate # Run database migrations
bun run db:seed # Seed test data
bun run db:studio # Open Drizzle Studio
# Code Quality
bun run lint # ESLint check
bun run lint:fix # ESLint auto-fix
bun run type-check # TypeScript type checkDeployment โ
Docker โ
bash
docker build -t halolight-api-bun .
docker run -p 3002:3002 halolight-api-bunDocker Compose โ
bash
docker-compose up -dyaml
# docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "3002:3002"
environment:
- NODE_ENV=production
- DATABASE_URL=${DATABASE_URL}
- JWT_SECRET=${JWT_SECRET}
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
POSTGRES_DB: halolight
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:Production Configuration โ
env
NODE_ENV=production
DATABASE_URL=postgresql://user:pass@host:5432/db
JWT_SECRET=your-production-secretTesting โ
Running Tests โ
bash
bun test # Run all tests
bun test --coverage # Generate coverage reportTest Examples โ
typescript
// Authentication test example
import { describe, test, expect } from 'bun:test';
describe('Auth API', () => {
test('should login successfully', async () => {
const response = await fetch('http://localhost:3002/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'admin@example.com',
password: 'admin123'
})
});
const data = await response.json();
expect(data.success).toBe(true);
expect(data.data.accessToken).toBeDefined();
});
});Performance Metrics โ
Benchmarks โ
| Metric | Value | Condition |
|---|---|---|
| Request Throughput | ~50,000 req/s | Single core, simple route |
| Average Response Time | <5ms | Local database |
| Memory Usage | ~30MB | Cold start |
| CPU Usage | <10% | Idle state |
Observability โ
Logging System โ
typescript
// Logging configuration example
import { logger } from './utils/logger';
logger.info('User logged in', { userId: user.id });
logger.error('Database error', { error: err.message });Health Check โ
typescript
// GET /health
app.get('/health', (c) => {
return c.json({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});Monitoring Metrics โ
typescript
// Prometheus metrics endpoint
app.get('/metrics', async (c) => {
return c.text(await register.metrics());
});FAQ โ
Q: How to configure database connection? โ
A: Set DATABASE_URL in .env file:
env
DATABASE_URL=postgresql://user:password@localhost:5432/halolightQ: How to use Bun's built-in password hashing? โ
A: Use Bun.password API:
typescript
// Hash password
const hash = await Bun.password.hash(password, {
algorithm: 'bcrypt',
cost: 10
});
// Verify password
const isValid = await Bun.password.verify(password, hash, 'bcrypt');Development Tools โ
Recommended Tools โ
- Drizzle Studio - Visual database management tool
- Hoppscotch/Postman - API testing tools
- ESLint + Prettier - Code formatting
- Bun VSCode Extension - Bun syntax support
Comparison with Other Backends โ
| Feature | Bun + Hono | NestJS | FastAPI | Spring Boot |
|---|---|---|---|---|
| Language | TypeScript | TypeScript | Python | Java |
| ORM | Drizzle | Prisma | SQLAlchemy | JPA |
| Performance | โญโญโญโญโญ | โญโญโญโญ | โญโญโญโญ | โญโญโญ |
| Learning Curve | โญโญโญโญ | โญโญโญ | โญโญโญโญ | โญโญ |