hono-template

๐Ÿ”ฅ Honoko Template

Production-ready TypeScript API template built with modern tooling and best practices.

โœจ Features

๐Ÿš€ Quick Start

# Clone the repository
git clone https://github.com/vlhsmylv/hono-template.git
cd hono-template

# Install dependencies
bun install

# Set up environment variables
cp .env.example .env
# Edit .env with your database URL and JWT secrets

# Set up database
bun run db:push    # Push schema to database
bun run db:generate # Generate Prisma client

# Set up git hooks (optional)
bun run prepare

# Run in development
bun dev

# Open in browser
open http://localhost:3000

๐Ÿ“š API Documentation

๐Ÿ”„ API Versioning - Third Form: Blazing Universe

The API uses URL path versioning for backward compatibility (like mastering different flame forms):

/api/v1/users     # First Form: Unknowing Fire
/api/v2/users     # Second Form: Rising Scorching Sun (future)

๐Ÿ› ๏ธ Tech Stack - Fourth Form: Blooming Flame Undulation

๐Ÿ“ Project Structure

src/
โ”œโ”€โ”€ modules/              # API modules with clean architecture
โ”‚   โ”œโ”€โ”€ auth/            # Authentication (login, register, logout)
โ”‚   โ”œโ”€โ”€ users/           # User management (CRUD operations)
โ”‚   โ”œโ”€โ”€ profile/         # User profile management
โ”‚   โ””โ”€โ”€ README.md        # Module development guide
โ”œโ”€โ”€ middleware/          # Custom middleware
โ”‚   โ”œโ”€โ”€ auth.ts          # JWT authentication with refresh tokens
โ”‚   โ”œโ”€โ”€ logger.ts        # Production logging system
โ”‚   โ””โ”€โ”€ user-injection.ts # Automatic user context injection
โ”œโ”€โ”€ prisma/             # Database client & utilities
โ”‚   โ””โ”€โ”€ index.ts        # Prisma client singleton
โ”œโ”€โ”€ public/             # Static files & landing page
โ”œโ”€โ”€ shared/             # Shared utilities & schemas
โ”œโ”€โ”€ utils/              # Helper functions & configurations
โ”œโ”€โ”€ versions/           # API versioning (v1, v2, etc.)
โ””โ”€โ”€ index.ts            # Application entry point

๐Ÿ”ง Development

# Development with hot reload
bun dev

# Database operations
bun run db:generate  # Generate Prisma client
bun run db:push      # Push schema changes
bun run db:migrate   # Create and run migrations
bun run db:studio    # Open Prisma Studio

# Code quality
bun run lint         # Lint TypeScript code
bun run lint:fix     # Fix auto-fixable lint errors
bun run format       # Format code with Prettier
bun run format:check # Check formatting
bun run type-check   # TypeScript type checking

# Testing
bun test            # Run tests
bun test:watch      # Run tests in watch mode

๐Ÿณ Docker

# Build image
docker build -t hono-template .

# Run container
docker run -p 3000:3000 hono-template

# With environment file
docker run -p 3000:3000 --env-file .env hono-template

๐Ÿ”— Environment Variables

Required environment variables (see .env.example):

# Database Connection
DATABASE_URL="postgresql://user:password@localhost:5432/hono_template"

# JWT Authentication (generate strong secrets!)
ACCESS_TOKEN_SECRET="your-super-secret-access-token-key"
REFRESH_TOKEN_SECRET="your-super-secret-refresh-token-key"

# Application Settings
NODE_ENV="development"
PORT=3000

๐Ÿ”’ Security Note: Generate strong, unique secrets for production environments. Never commit real secrets to version control.

๐Ÿ“ Git Hooks

๐Ÿ“‹ Logging

The template includes a production-ready logging system:

Features

Usage

import { logger } from "@/middleware/logger";

// Basic logging
logger.info("User registered", { userId: "123", email: "user@example.com" });
logger.warn("Rate limit approaching", { attempts: 8 });
logger.error("Database connection failed", { error: "Connection timeout" });
logger.debug("Debug info", { data: processedData }); // Dev only

// HTTP request logging
logger.request("POST", "/api/users", 201, 145, {
  ip: "192.168.1.1",
  userAgent: "Mozilla/5.0...",
  requestId: "req-abc123",
});

Log Files

logs/
โ”œโ”€โ”€ 2025-01-14-info.log    # All logs (combined)
โ”œโ”€โ”€ 2025-01-14-warn.log    # Warning logs only
โ”œโ”€โ”€ 2025-01-14-error.log   # Error logs only
โ””โ”€โ”€ 2025-01-14-debug.log   # Debug logs only

๐Ÿ” Authentication & Authorization

The template includes a comprehensive auth system:

Features

Usage

// Apply auth middleware to protected routes
controller.use("/*", authMiddleware);
controller.use("/*", injectUser);

// Use decorator pattern for clean user access
controller.openapi(
  route,
  withUser(async (user, c) => {
    // User automatically injected!
    return c.json({ profile: user });
  })
);

๐Ÿ—„๏ธ Database

Setup

  1. Install PostgreSQL or use a cloud provider
  2. Configure DATABASE_URL in .env
  3. Push schema: bun run db:push
  4. Generate client: bun run db:generate

Schema

Current models: User with authentication fields. Easily extendable for your needs.

Migrations

# Create migration
bun run db:migrate

# Reset database (dev only)
bun run db:push --force-reset

๐Ÿค Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feat/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feat/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ‘จโ€๐Ÿ’ป Author

Built with โค๏ธ by Valeh Ismayilov

Hono no Kokyu ;)


โญ Star this repository if you find it helpful!