cwgo Code Generation Tool

repository·main·Indexed 41 days ago

https://github.com/cloudwego/cwgo

An all-in-one code generation tool for the CloudWeGo ecosystem. cwgo integrates Kitex and Hertz toolsets to automate the generation of RPC and HTTP servers, clients, database models, documentation models, and job code, while providing utilities to analyze router code using Go AST.

Tokens
6.1K
Snippets
26
Records
30
Agent score
50%

What's inside cwgo

  1. Overview of cwgo features

    main

    cwgo is an all-in-one code generation tool for CloudWeGo. It integrates the capabilities of Kitex and Hertz to improve development efficiency. Key features include:

    • Engineering Templates: Generates MVC project layouts so developers can focus on business logic.
    • Server & Client Generation: Generates Kitex and Hertz Server and Client code, providing encapsulated Clients for easy downstream calls.
    • Database CURD Generation: Supports generating CURD (Create, Read, Update, Delete) code for both relational databases and document-based databases (currently supporting MongoDB via IDL like Thrift/Protobuf).
    • Hertz Route Analysis: Analyzes Hertz projects to map the relationship between routes and route registration code.
    • CLI Autocompletion: Generates scripts for command-line autocompletion.
    • Backward Compatibility: Can be used as a fallback for standard kitex and hz tools.
  2. Install the cwgo tool

    main

    Install cwgo using go install depending on your Go version. It is recommended to use a proxy like https://goproxy.cn/ for faster installation in certain regions.

    Note: This repository has been archived. For Kitex and Hertz users, it is recommended to use the original tools directly. For custom layouts and templates, refer to the Kitex Custom Scaffold Template or Hz custom template use.

    # Go 1.15 and earlier version
    GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get github.com/cloudwego/cwgo@latest
    
    # Go 1.16 and later version
    GOPROXY=https://goproxy.cn/,direct go install github.com/cloudwego/cwgo@latest
  3. Use the cwgo CLI to generate code and analyze projects

    main

    The cwgo command-line tool is an all-in-one utility for CloudWeGo development. It provides subcommands to generate RPC/HTTP servers and clients, database models, documentation models, job code, and to analyze router code using Go AST.

    Global Flags:

    • --verbose, -vv: Turn on verbose mode.

    Available Commands:

    • server: Generate RPC or HTTP server code.
    • client: Generate RPC or HTTP client code.
    • model: Generate DB model code.
    • doc: Generate doc model code.
    • job: Generate job code.
    • api-list: Analyze router codes by Go AST.
    • fallback: Fallback to hz or kitex.
    • completion: Generate autocompletion scripts for various shells.
    # Example: Generate an RPC server
    cwgo server --type RPC --idl ./path/to/service.thrift --service my_service
    
    # Example: Generate a MySQL model
    cwgo model --db_type mysql --dsn "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
    
    # Example: Analyze router code
    cwgo api --project_path ./
  4. Understand the Kitex standard server template structure

    main

    The kitex/server/standard template is designed for use with the Kitex framework. It provides a basic MVC (Model-View-Controller) code hierarchy, configuration functions, and base code generation for unit tests.

    Key directory responsibilities:

    • conf: Contains configuration files.
    • main.go: The application startup file.
    • handler.go: Handles request processing and response returns.
    • kitex_gen: Contains code generated by the Kitex framework.
    • biz/service: Contains the actual business logic.
    • biz/dal: Contains the Data Access Layer (DAL) logic for operating the storage layer.
  5. Important notice: cwgo project is archived

    main

    The cwgo project has been archived.

    Due to the widespread use of built-in Kitex and Hertz tools within ByteDance and a lack of new internal scenarios, the project is no longer being actively maintained.

    Recommendations for users:

    • Kitex users: Use the original Kitex tools for project generation. For custom layouts and templates, refer to the Kitex Custom Scaffold guide.
    • Hertz users: Use the original Hertz tools. For custom templates, refer to the Hertz Custom Scaffold guide.
    • Note: Support for generating GORM and MongoDB code is no longer being actively maintained.
  6. Generate DB models with `cwgo model`

    main

    Use the model command to generate database model code based on a DSN and database type.

    Usage Example:

    cwgo model --db_type mysql --dsn "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
  7. Generate RPC or HTTP client with `cwgo client`

    main

    Use the client command to generate boilerplate code for RPC or HTTP clients.

    Usage Examples:

    # Generate RPC client code 
    cwgo client --type RPC --idl {{path/to/IDL_file.thrift}} --service {{svc_name}}
    
    # Generate HTTP client code 
    cwgo client --type HTTP --idl {{path/to/IDL_file.thrift}} --service {{svc_name}}
    cwgo client --type RPC --idl ./api.thrift --service MyService
  8. Generate RPC or HTTP server with `cwgo server`

    main

    Use the server command to generate boilerplate code for RPC or HTTP servers.

    Usage Examples:

    # Generate RPC server code 
    cwgo server --type RPC --idl {{path/to/IDL_file.thrift}} --service {{svc_name}}
    
    # Generate HTTP server code 
    cwgo server --type HTTP --idl {{path/to/IDL_file.thrift}} --service {{svc_name}}
    cwgo server --type RPC --idl ./api.thrift --service MyService