### Generating API Files Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md Provides an example of using the `jzero ivm add api` command to generate new API files and configure handlers. ```APIDOC ## Generating API Files ### Description This section details how to use the `jzero` command-line tool to automatically generate API files and configure associated handlers. ### Method Command Line ### Endpoint N/A ### Parameters #### Command Line Arguments - **jzero ivm add api --name user**: Command to add a new API file named 'user'. - **jzero ivm add api --name user --handlers get:List,get:Get,post:Edit,get:Delete**: Command to add an API file named 'user' with predefined handlers for GET, POST requests. ### Request Example ```shell jzero ivm add api --name user ``` ```shell jzero ivm add api --name user --handlers get:List,get:Get,post:Edit,get:Delete ``` ``` -------------------------------- ### API go_package 分组配置 Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md 演示了如何使用 `go_package` 选项来根据 proto 文件的模式对生成的结构体进行分组。这有助于避免 `types/types.go` 文件过于庞大,并解决命名冲突问题。 ```shell syntax = "v1" info ( go_package: "version" ) ``` -------------------------------- ### 使用 etcd 作为注册中心替换 fsnotify Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/config/configcenter.md 演示如何使用 etcd 作为配置中心的注册中心。需要 go-zero 核心库和 etcd 订阅者。配置 etcd 地址和 key。 ```go package main import ( "fmt" subscriber "github.com/zeromicro/go-zero/core/configcenter/subscriber" configurator "github.com/zeromicro/go-zero/core/configcenter" ) type Config struct { Name string `json:"name"` DatabaseType string `json:"databaseType"` } func main() { cc := configurator.MustNewConfigCenter[Config](configurator.Config{ Type: "yaml", },subscriber.MustNewEtcdSubscriber(subscriber.EtcdConf{ Hosts: []string{"localhost:2379"}, // etcd 地址 Key: "test1", // 配置key })) config , err := cc.GetConfig() if err != nil { panic(err) } fmt.Println(config) } ``` -------------------------------- ### 使用 fsnotify 实现配置中心 Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/config/configcenter.md 演示如何使用 fsnotify 作为配置中心,加载 YAML 文件。需要 go-zero 核心库和 fsnotify 订阅者。 ```go package main import ( "fmt" "github.com/jzero-io/jzero/core/configcenter/subscriber" configurator "github.com/zeromicro/go-zero/core/configcenter" ) type Config struct { Name string `json:"name"` DatabaseType string `json:"databaseType"` } func main() { cfgFile := "config.yaml" cc := configurator.MustNewConfigCenter[Config](configurator.Config{ Type: "yaml", }, subscriber.MustNewFsnotifySubscriber(cfgFile, subscriber.WithUseEnv(true))) config , err := cc.GetConfig() if err != nil { panic(err) } fmt.Println(config) } ``` ```yaml name: jzero databaseType: mysql ``` -------------------------------- ### 自动生成 API 文件命令 Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md 提供了使用 `jzero ivm add api` 命令自动生成 API 文件的示例。包括基本命令和带有指定 HTTP 方法的完整命令。 ```shell jzero ivm add api --name user ``` ```shell jzero ivm add api --name user --handlers get:List,get:Get,post:Edit,get:Delete ``` -------------------------------- ### 合并 API Group Handler 和 Logic Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md 展示了如何通过 `compact_handler` 和 `compact_logic` 选项将同一个 group 下的 handler 和 logic 合并到同一个文件中。这可以简化项目结构并提高开发效率。 ```shell @server ( prefix: /api/v1 group: system/user compact_handler: true compact_logic: true ) service simpleapi { @handler GetUserHandler get /system/user/getUser (GetUser2Request) returns (GetUserResponse) @handler DeleteUserHandler get /system/user/deleteUser (DeleteUserRequest) returns (DeleteUserResponse) } ``` -------------------------------- ### Consolidating Handlers and Logic within a Group Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md Shows how to combine handlers and logic for the same group into a single file using `compact_handler` and `compact_logic` options. ```APIDOC ## Consolidating Handlers and Logic within a Group ### Description This configuration allows consolidating multiple handlers and their corresponding logic into a single file for a given group. This can simplify project structure and improve developer experience. ### Method N/A (This is a definition example) ### Endpoint N/A ### Parameters #### Request Body - **@server** (annotation) - Specifies server-level configurations. - **prefix** (string) - The base URL prefix for the API group. - **group** (string) - The logical grouping name for related endpoints. - **compact_handler** (boolean) - If true, consolidates handlers. - **compact_logic** (boolean) - If true, consolidates logic. - **service simpleapi** (service definition) - Defines the API service. - **@handler GetUserHandler** (annotation) - Defines a handler for getting user information. - **get /system/user/getUser (GetUser2Request) returns (GetUserResponse)** - GET request for user retrieval. - **@handler DeleteUserHandler** (annotation) - Defines a handler for deleting user information. - **get /system/user/deleteUser (DeleteUserRequest) returns (DeleteUserResponse)** - GET request for user deletion. ### Request Example ```shell {"4","5"} @server ( prefix: /api/v1 group: system/user compact_handler: true compact_logic: true ) service simpleapi { @handler GetUserHandler get /system/user/getUser (GetUser2Request) returns (GetUserResponse) @handler DeleteUserHandler get /system/user/deleteUser (DeleteUserRequest) returns (DeleteUserResponse) } ``` ``` -------------------------------- ### API 字段校验示例 (go-playground/validator) Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md 展示了如何使用 go-zero 集成的 validator 库进行 API 请求的字段校验。通过 `validate` 标签为字段定义校验规则,例如最小和最大长度。 ```shell syntax = "v1" type CreateRequest { name string `json:"name" validate:"gte=2,lte=30"` // 名称 } ``` -------------------------------- ### Grouping Types by go_package Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md Explains how to use the `go_package` option to group generated structs based on Go package, preventing naming conflicts and improving organization. ```APIDOC ## Grouping Types by go_package ### Description The `go_package` option, similar to proto files, allows grouping of generated structs based on a specified package name. This helps in organizing types and avoiding naming collisions, especially in large projects. ### Method N/A (This is a definition example) ### Endpoint N/A ### Parameters #### Request Body - **info** (type) - Optional - Configuration for grouping. - **go_package** (string) - Specifies the Go package for generated types. ### Request Example ```shell {"3","4","5","6"} syntax = "v1" info ( go_package: "version" ) ``` ``` -------------------------------- ### API Field Validation Source: https://github.com/jzero-io/jzero/blob/main/docs/src/guide/develop/api.md Demonstrates how go-zero integrates with go-playground/validator for field validation within API definitions. ```APIDOC ## API Field Validation ### Description Go-zero integrates with `go-playground/validator` for field validation. This example shows how to define validation rules for a request field. ### Method N/A (This is a definition example) ### Endpoint N/A ### Parameters #### Request Body - **name** (string) - Required - The name field with validation rules: minimum length 2, maximum length 30. ### Request Example ```shell {"4"} syntax = "v1" type CreateRequest { name string `json:"name" validate:"gte=2,lte=30"` // 名称 } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.