NestGo (NG)
A lightweight and modular Go framework inspired by NestJS, designed to simplify the development of scalable and maintainable web applications.
Tech Stack
Installation
Add NestGo to your project:
Overview
NestGo (NG) brings the familiar, modular architecture of NestJS to the Go ecosystem. It provides a structured approach to building web applications with support for:
- Controllers for organizing routes and business logic
- Middleware for cross-cutting concerns like logging and authentication
- Guards for authorization and access control
- Interceptors for request/response transformation
- Dynamic Adapters for seamless integration with popular Go HTTP frameworks
Whether you're building a simple REST API or a complex microservice, NG helps you write clean, maintainable, and testable code.
Features
- ✨ NestJS-Inspired Architecture - Familiar patterns for TypeScript developers transitioning to Go
- 🔌 Dynamic HTTP Adapters - Native support for Echo, Fiber, Gin, Chi, and standard
http.ServeMux - 🛡️ Guards & Interceptors - Built-in support for authentication, rate limiting, and request transformation
- 🎯 Type-Safe Context Management - Generic-based context storage with
ng.Storeandng.Load - 📦 Modular Design - Organize code into controllers, services, and modules
- 🔧 Metadata System - Attach configuration and behavior to routes dynamically
- Comprehensive Examples - Learn from basic to advanced use cases
Architecture
Request Flow
NestGo processes incoming HTTP requests through a well-defined pipeline:
Pipeline Stages:
- Middleware - Execute first; handle logging, CORS, request parsing
- Guards - Enforce access control and rate limits
- Interceptors (Pre) - Transform or validate requests
- Handler - Execute core business logic
- Interceptors (Post) - Transform responses
- Response - Send final response to client
Installation
Add NestGo to your project:
Requirements:
- Go 1.18 or higher (for generics support)
Quick Start
Basic Application
Create a simple health check API in minutes:
Test it:
Using Adapters
NestGo supports multiple HTTP frameworks through adapters. Here's an example using Echo:
Supported Adapters:
Note: Typically, you'll use one adapter per application. Multiple adapters are useful for migration scenarios or serving the same routes on different ports.
Examples
Explore real-world examples in the examples directory:
| Example | Description | Key Features |
|---|---|---|
| basic | Simple CRUD application | Controllers, Middleware, Multiple adapters |
| advanced | Production-ready structure | Guards, Interceptors, DAL, DTOs, Swagger |
| chi | Chi router integration | Chi adapter usage |
| echo | Echo framework integration | Echo adapter, middleware |
| fiber | Fiber framework integration | Fiber adapter, high performance |
| gin | Gin framework integration | Gin adapter, JSON handling |
| http | Standard library only | Native http.ServeMux, zero dependencies |
Each example includes:
- Complete
main.gowith setup - Controllers and route definitions
- Middleware and guard implementations
- Adapter configurations
- README with running instructions
Core Concepts
Controllers
Controllers organize related routes and encapsulate business logic. They provide a clean, modular structure for your application.
Creating a Controller:
Key Points:
- Controllers group related routes under a common prefix
- Use dependency injection for services
- Each method returns a
ng.Route - Routes can have their own middleware, guards, and interceptors
Middleware
Middleware functions execute before guards and interceptors. They're perfect for logging, CORS, authentication, and request parsing.
Creating Middleware:
Guards
Guards determine whether a request should proceed. They're executed after middleware and are ideal for authentication, authorization, and rate limiting.
Creating a Guard:
Rate Limiting Guard:
Interceptors
Interceptors transform requests before they reach handlers and responses after handlers execute. They're perfect for validation, transformation, and logging.
Creating an Interceptor:
Response Wrapper Interceptor:
Metadata
Metadata allows dynamic configuration of routes, controllers, and features. It's inspired by NestJS decorators.
Using Metadata:
Metadata Hierarchy:
Metadata can be set at three levels:
- Application Level - Applies to all routes
- Controller Level - Applies to all routes in the controller
- Route Level - Applies to specific route
Route-level metadata overrides controller-level, which overrides application-level.
Helper Functions:
Context Management
NestGo provides type-safe context utilities for storing and retrieving request-scoped data.
Storing Data:
Loading Data:
Available Functions:
Skippers
Skippers allow you to conditionally bypass middleware, guards, or interceptors for specific routes.
Using DefaultID:
Skip All Guards:
Custom ID Implementation:
Key Points:
- Use
DefaultID[T]for automatic ID generation - Use
WithSkip()to skip specific features - Use
SkipAllGuards()for public endpoints - Skippers work with middleware, guards, and interceptors
Advanced Topics
Sub-Applications
Organize large projects by mounting sub-applications:
Custom Adapters
Create adapters for other HTTP frameworks:
Contributing
Contributions 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.
Development:
Third party packages
- Rate-limit: A rate-limiting guard
License
This project is licensed under the MIT License. See the LICENSE file for details.