|
1 | | -# gen |
| 1 | +# gs-http-gen |
| 2 | + |
| 3 | +<div> |
| 4 | + <img src="https://img.shields.io/github/license/go-spring/gs-http-gen" alt="license"/> |
| 5 | + <img src="https://img.shields.io/github/go-mod/go-version/go-spring/gs-http-gen" alt="go-version"/> |
| 6 | + <img src="https://img.shields.io/github/v/release/go-spring/gs-http-gen?include_prereleases" alt="release"/> |
| 7 | + <a href="https://codecov.io/gh/go-spring/gs-http-gen" > |
| 8 | + <img src="https://codecov.io/gh/go-spring/gs-http-gen/graph/badge.svg?token=SX7CV1T0O8" alt="test-coverage"/> |
| 9 | + </a> |
| 10 | + <a href="https://deepwiki.com/go-spring/gs-http-gen"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a> |
| 11 | +</div> |
| 12 | + |
| 13 | +[English](README.md) | [中文](README_CN.md) |
| 14 | + |
| 15 | +## Project Overview |
| 16 | + |
| 17 | +**gs-http-gen** is an HTTP code generation tool based on **IDL (Interface Definition Language)**, designed for the * |
| 18 | +*Go-Spring** framework. It can automatically generate HTTP server and client code from interface definitions, including |
| 19 | +data models, validation logic, and route bindings. |
| 20 | + |
| 21 | +The tool aims to simplify the development workflow of Go web services. By using declarative IDL definitions, it |
| 22 | +generates boilerplate code automatically, improving development efficiency and reducing human errors. |
| 23 | + |
| 24 | +More importantly, IDL is not only used for code generation—it also serves as a unified contract and documentation for |
| 25 | +APIs across frontend-backend teams and departments. With standardized IDL definitions, key details such as request |
| 26 | +parameters, response formats, and validation rules become clear, reducing communication costs and ensuring API |
| 27 | +consistency and correctness. |
| 28 | + |
| 29 | +## Features |
| 30 | + |
| 31 | +- **IDL-driven**: Define service interfaces and data models using a simple interface definition language. |
| 32 | +- **Automatic code generation**: Generate Go code from IDL files, including: |
| 33 | + - Data model structs |
| 34 | + - Data validation logic |
| 35 | + - HTTP route bindings |
| 36 | + - Server interface definitions |
| 37 | + - Client call code |
| 38 | +- **Rich type support**: Supports basic types, structs, enums, lists, optional types, etc. |
| 39 | +- **Data validation**: Built-in validation rules with support for custom validators. |
| 40 | +- **HTTP parameter binding**: Automatically bind HTTP request parameters (path, query, header, body) to data models. |
| 41 | +- **Type embedding**: Supports type inheritance and field reuse to reduce redundancy. |
| 42 | +- **Flexible configuration**: Generate server code, client code, or both. |
| 43 | +- **Enum support**: Enum types with optional string serialization. |
| 44 | +- **Streaming support**: Generate streaming RPC interfaces. |
| 45 | +- **Annotation support**: Add Markdown-style comments in IDL (not yet implemented). |
| 46 | + |
| 47 | +## Installation |
| 48 | + |
| 49 | +**Recommended**: Install via the **gs** integrated development tool, |
| 50 | +see [https://github.com/go-spring/gs](https://github.com/go-spring/gs). |
| 51 | + |
| 52 | +To install this tool separately: |
| 53 | + |
| 54 | +```bash |
| 55 | +go install github.com/go-spring/gs-http-gen@latest |
| 56 | +``` |
| 57 | + |
| 58 | +## Usage |
| 59 | + |
| 60 | +### Step 1: Define an IDL file |
| 61 | + |
| 62 | +First, create an IDL file to define service interfaces and data models: |
| 63 | + |
| 64 | +```idl |
| 65 | +// Define constants |
| 66 | +const int MAX_AGE = 150 |
| 67 | +const int MIN_AGE = 18 |
| 68 | +
|
| 69 | +// Define enums |
| 70 | +enum ErrCode { |
| 71 | + ERR_OK = 0 |
| 72 | + PARAM_ERROR = 1003 |
| 73 | +} |
| 74 | +
|
| 75 | +enum Department { |
| 76 | + ENGINEERING = 1 |
| 77 | + MARKETING = 2 |
| 78 | + SALES = 3 |
| 79 | +} |
| 80 | +
|
| 81 | +// Define data structures |
| 82 | +type Manager { |
| 83 | + string id |
| 84 | + string name (validate="len($) > 0 && len($) <= 64") |
| 85 | + int? age (validate="$ >= MIN_AGE && $ <= MAX_AGE") |
| 86 | + Department dept |
| 87 | +} |
| 88 | +
|
| 89 | +type Response<T> { |
| 90 | + ErrCode errno = ErrCode.ERR_OK (validate="OneOfErrCode($)") |
| 91 | + string errmsg |
| 92 | + T data |
| 93 | +} |
| 94 | +
|
| 95 | +// Define request and response |
| 96 | +type ManagerReq { |
| 97 | + string id (path="id") |
| 98 | +} |
| 99 | +
|
| 100 | +type GetManagerResp Response<Manager?> |
| 101 | +
|
| 102 | +// Define streaming types |
| 103 | +type StreamReq { |
| 104 | + string id |
| 105 | +} |
| 106 | +
|
| 107 | +type StreamResp { |
| 108 | + string id |
| 109 | + string data |
| 110 | +} |
| 111 | +
|
| 112 | +// Define RPC interface |
| 113 | +rpc GetManager(ManagerReq) GetManagerResp { |
| 114 | + method="GET" |
| 115 | + path="/managers/{id}" |
| 116 | + summary="Get manager info by ID" |
| 117 | +} |
| 118 | +
|
| 119 | +// Example of streaming interface |
| 120 | +rpc Stream(StreamReq) stream<StreamResp> { |
| 121 | + method="GET" |
| 122 | + path="/stream/{id}" |
| 123 | + summary="Stream data transfer" |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### Step 2: Generate code |
| 128 | + |
| 129 | +Use the CLI tool to generate code: |
| 130 | + |
| 131 | +```bash |
| 132 | +# Generate server code only (default) |
| 133 | +gs-http-gen --server --output ./generated --package myservice |
| 134 | + |
| 135 | +# Generate both server and client code |
| 136 | +gs-http-gen --server --client --output ./generated --package myservice |
| 137 | +``` |
| 138 | + |
| 139 | +Command-line options: |
| 140 | + |
| 141 | +* `--server`: Generate server code (HTTP handlers, route bindings, etc.) |
| 142 | +* `--client`: Generate client code (HTTP call wrappers) |
| 143 | +* `--output`: Output directory for generated code (default: current directory) |
| 144 | +* `--package`: Go package name (default: "proto") |
| 145 | +* `--language`: Target language (currently only `"go"` supported) |
| 146 | + |
| 147 | +### Step 3: Use the generated code |
| 148 | + |
| 149 | +The generated code includes data models, validation logic, and HTTP handlers: |
| 150 | + |
| 151 | +```go |
| 152 | +// Implement service interface |
| 153 | +type MyManagerServer struct{} |
| 154 | + |
| 155 | +func (m *MyManagerServer) GetManager(ctx context.Context, req *proto.ManagerReq) *proto.GetManagerResp { |
| 156 | + // Business logic |
| 157 | + data := proto.NewManager() |
| 158 | + data.SetName("Jim") |
| 159 | + res := proto.NewGetManagerResp() |
| 160 | + res.SetData(data) |
| 161 | + return res |
| 162 | +} |
| 163 | + |
| 164 | +func (m *MyManagerServer) Stream(ctx context.Context, req *proto.StreamReq, resp chan<- *proto.StreamResp) { |
| 165 | + // Streaming logic |
| 166 | + for i := 0; i < 5; i++ { |
| 167 | + resp <- &proto.StreamResp{ |
| 168 | + Id: strconv.Itoa(i), |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// Register routes |
| 174 | +mux := http.NewServeMux() |
| 175 | +proto.InitRouter(mux, &MyManagerServer{}) |
| 176 | +``` |
| 177 | + |
| 178 | +## License |
| 179 | + |
| 180 | +This project is licensed under the **Apache License 2.0**. See the [LICENSE](LICENSE) file for details. |
0 commit comments