Introduction
A Lean Framework for Enterprise Scalability.
GamanJS is a lightweight, fast HTTP framework built on top of Bun runtime. Designed for building structured REST APIs with a clean Controller-Service-Middleware architecture thatβs easy to maintain.
β¨ Key Features
Section titled ββ¨ Key Featuresβ- β‘ Blazing Fast β Built on Bunβs native
Bun.serve()for maximum performance - ποΈ Structured Architecture β Built-in Controller / Service / Middleware pattern
- π¦ Fluent Response API β Clean and consistent response handling via
ctx.send() - π Declarative Router β Clean route definition with
composeRouter - π§© Type-Safe DI β Simple dependency injection via destructuring and TypeScript
- π‘οΈ Exception Handling β Global & per-route exception handlers
- π― Middleware Priority β Control middleware execution order
π Table of Contents
Section titled βπ Table of Contentsβ| Page | Description |
|---|---|
| Getting Started | Installation & creating a new project |
| Bootstrap | defineBootstrap & server configuration |
| Router | Route definition, grouping, & methods |
| Controller | Creating controllers with composeController |
| Service | Business logic with composeService |
| Middleware | Middleware & priority system |
| Response | Fluent response handling with ctx.send |
| Context | Request context (ctx) in detail |
| Exception Handler | Global & per-route error handling |
π Quick Start
Section titled βπ Quick Startβbun create gaman@latestDefault Project Structure
Section titled βDefault Project StructureβGamanJS embraces the Feature-Based Modularity philosophy. For better scalability, we recommend placing your routes, controllers, and services within feature modules:
src/βββ index.ts # The Orchestrator (Entry Point)βββ modules/ βββ app/ # Main Application Module βββ controllers/ β βββ AppController.ts βββ services/ β βββ AppService.ts βββ middlewares/ β βββ AppMiddleware.ts βββ AppRoutes.ts # Routes for this moduleEnterprise Project Architecture
Section titled βEnterprise Project ArchitectureβFor large-scale production applications, you can add more feature modules:
src/βββ index.ts # The Orchestrator (Entry Point)βββ database/ # Database Configurationβββ modules/ # Powerhouse of your Application βββ app/ # Infrastructure Module (Global/Shared) β βββ controllers/ # Handlers for Global Requests (Health, Index) β β βββ AppController.ts β βββ services/ # Shared Utilities & System Services β β βββ AppService.ts β βββ AppRoutes.ts # Global Middleware & Base Routing β βββ user/ # Feature Module (Example: User Management) βββ controllers/ # Request Handlers (Login, Register, Profile) β βββ UserController.ts βββ services/ # Business Logic (Auth Logic, User CRUD) β βββ UserService.ts βββ models/ # Data Access Layer (Powered by @gaman/orm) β βββ UserModel.ts βββ UserRoutes.ts # Scoped Routes & Feature Middlewaresrc/index.ts
Section titled βsrc/index.tsβimport { defineBootstrap } from 'gaman';import AppRoutes from './modules/app/AppRoutes';
defineBootstrap(async (app) => { app.mount(AppRoutes); app.mountServer({ http: 3431 });});src/modules/app/AppRoutes.ts
Section titled βsrc/modules/app/AppRoutes.tsβimport { composeRouter } from 'gaman/compose';import { AppService } from './services/AppService';import AppController from './controllers/AppController';
export default composeRouter((r) => { // Inject services r.mountService({ appService: AppService(), });
r.get('/', [AppController, 'HelloWorld']);});src/modules/app/controllers/AppController.ts
Section titled βsrc/modules/app/controllers/AppController.tsβimport { composeController } from 'gaman/compose';import { AppService } from '../services/AppService';
export type Deps = { appService: AppService;};
export default composeController(({ appService }: Deps) => ({ HelloWorld(ctx) { return ctx.send(appService.WelcomeMessage()).ok(); },}));src/modules/app/services/AppService.ts
Section titled βsrc/modules/app/services/AppService.tsβimport { composeService } from 'gaman/compose';import type { RT } from 'gaman/types';
export const AppService = composeService(() => ({ WelcomeMessage() { return 'β€οΈ Welcome to GamanJS'; },}));
export type AppService = RT<typeof AppService>;src/modules/app/middlewares/AppMiddleware.ts
Section titled βsrc/modules/app/middlewares/AppMiddleware.tsβimport { composeMiddleware } from 'gaman/compose';
export default composeMiddleware(async (ctx, next) => { console.log(`[${ctx.request.method}] ${ctx.path}`); return next();});π License
Section titled βπ LicenseβMIT Β© Angga7Togk