Codegen π

Generate Clean Architecture for REST API with support for Fiber Web Framework and gRPC in Golang. Speed up your development with automatic code generation.
β¨ Features
- ποΈ Clean Architecture - Automatically generates layered architecture structure
- π Fiber Framework Support - Optimized for the Fiber web framework
- π gRPC Support - Built-in support for gRPC services
- π CRUD Generation - Generate CRUD operations from JSON specifications
- π οΈ Open API Generation - Generate Open API without configuration
- π§© Modular Design - Feature-based modules for better organization
- π§ Wire Integration - Dependency injection with Google Wire
- β‘ Fast Development - Speed up your development workflow
π¦ Installation
Latest version:
go install github.com/prongbang/codegen@v1.5.3
π Quick Start
Generate OpenAPI spec from a Fiber codebase:
codegen openapi -framework fiber ./... > docs/openapi.json
1. Create a New Project
Generate a new project with module structure:
codegen -new test_project -mod github.com/prongbang
Parameters:
-new: Project name
-mod: Module name
This creates the following structure:
.
βββ Makefile
βββ cmd
β βββ api
β βββ main.go
βββ configuration
β βββ configuration.go
β βββ development.yml
β βββ environment.go
β βββ production.yml
βββ deployments
β βββ Dockerfile
β βββ api-prod.yml
βββ docs
β βββ apispec
β βββ docs.go
β βββ swagger.json
β βββ swagger.yaml
βββ go.mod
βββ go.sum
βββ internal
β βββ app
β β βββ api
β β β βββ api.go
β β β βββ example
β β β β βββ datasource.go
β β β β βββ example.go
β β β β βββ handler.go
β β β β βββ permission.go
β β β β βββ provider.go
β β β β βββ repository.go
β β β β βββ router.go
β β β β βββ usecase.go
β β β βββ routers.go
β β βββ app.go
β βββ database
β β βββ db.go
β β βββ drivers.go
β β βββ mariadb.go
β β βββ mongodb.go
β β βββ wire.go
β β βββ wire_gen.go
β βββ middleware
β β βββ api_key.go
β β βββ jwt.go
β β βββ on_request.go
β βββ pkg
β β βββ casbinx
β β β βββ casbinx.go
β β βββ response
β β β βββ response.go
β β βββ validator
β β βββ validator.go
β βββ shared
β βββ example
β βββ datasource.go
β βββ example.go
β βββ provider.go
β βββ repository.go
β
βββ pkg
β βββ core
β β βββ common.go
β β βββ flag.go
β β βββ handler.go
β β βββ header.go
β β βββ jwt.go
β β βββ paging.go
β β βββ params.go
β β βββ request.go
β β βββ response.go
β β βββ router.go
β β βββ sorting.go
β βββ multipartx
β β βββ multipartx.go
β βββ requestx
β β βββ request.go
β βββ schema
β β βββ sql.go
β βββ streamx
β β βββ streamx.go
β βββ structx
β β βββ structx.go
β βββ typex
β βββ typex.go
βββ policy
β βββ model.conf
β βββ policy.csv
βββ spec
β βββ promotion.json
βββ wire.go
βββ wire_gen.go
1.1 Initial gRPC
codegen grpc init
This creates the initial gRPC scaffold under internal/app/grpc and generates the default health service.
1.2 Generate gRPC Server
codegen grpc server --new device
This creates:
internal/app/grpc/device/v1
βββ device.proto
βββ provider.go
βββ server.go
It also updates:
internal/app/grpc/servers.go
wire.go
Then it runs:
make gen service=device version=v1
wire
1.3 Generate gRPC Client
Use the format <thirdparty>/<service>:
codegen grpc client --new core/device
This creates:
internal/thirdparty
βββ Makefile
βββ core
βββ clients.go
βββ device
βββ v1
βββ client.go
βββ device.proto
It also updates:
internal/thirdparty/core/clients.go
internal/thirdparty/Makefile
Then it runs:
make gen service=device version=v1 thirdparty=core
2. Generate Features Prototype
Generate a new feature module:
codegen -f user
This creates:
test-project/internal/app/api/promotion
βββ datasource.go
βββ handler.go
βββ permission.go
βββ promotion.go
βββ provider.go
βββ repository.go
βββ router.go
βββ usecase.go
3. Generate Features CRUD and Swagger
Generate CRUD operations from JSON specifications:
1. Define Spec File
Create spec/auth.json:
{
"accessToken": "JWT",
"expired": 1234567,
"date": "2024-10-15T14:30:00Z"
}
2. Generate CRUD
codegen -f auth -s spec/auth.json -d mariadb -orm sqlbuilder
codegen -f auth -s spec/auth.json -d mariadb -orm bun
This generates complete CRUD operations based on your JSON structure.
test-project/internal/app/api/promotion
βββ datasource.go
βββ handler.go
βββ permission.go
βββ promotion.go
βββ provider.go
βββ repository.go
βββ router.go
βββ usecase.go
4. Generate Shared Prototype
codegen -sh promotion
This generates shared prototype
test-project/internal/shared/promotion
βββ datasource.go
βββ promotion.go
βββ provider.go
βββ repository.go
5. Generate Shared CRUD
codegen -sh promotion -s spec/promotion.json -d maridb -orm sqlbuilder
codegen -sh promotion -s spec/promotion.json -d maridb -orm bun
This generates shared CRUD operations based on your JSON structure.
test-project/internal/shared/promotion
βββ datasource.go
βββ promotion.go
βββ provider.go
βββ repository.go
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Support
If you find this tool helpful, please consider buying me a coffee:

- Fiber - Express-inspired web framework
- Wire - Compile-time dependency injection
- gRPC - High performance RPC framework