codegen

command module
v1.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 13 Imported by: 0

README ΒΆ

Codegen πŸš€

Coverage Go Report Card Go Reference Version

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
  • SQL Builder
codegen -f auth -s spec/auth.json -d mariadb -orm sqlbuilder
  • Bun
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
  • SQL Builder
codegen -sh promotion -s spec/promotion.json -d maridb -orm sqlbuilder
  • Bun
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

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
internal
pkg
mod

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL