feat: complete API-driven projects with infrastructure and business acceptance
This commit is contained in:
15
examples/contracts/README.md
Normal file
15
examples/contracts/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# 接口定义示例
|
||||
|
||||
`user.api` 是入口,导入 `shared.api`。包含 JSON/path/query/header 混合请求、分页与重复 query 参数、嵌套对象、optional 指针、user 与 admin/user 分组以及 Auth/Audit 命名中间件。
|
||||
|
||||
在 go-omni 根目录执行:
|
||||
|
||||
```sh
|
||||
go run ./cmd/go-omni api validate --api examples/contracts/user.api
|
||||
go run ./cmd/go-omni api generate --api examples/contracts/user.api \
|
||||
--dir ../userService --module example.com/userService --framework-dir .
|
||||
```
|
||||
|
||||
项目中补充 `internal/service` 的业务实现和 `internal/middleware` 的 Auth/Audit hook,再启动服务。初始占位实现返回 501。
|
||||
|
||||
需要立即能返回成功结果的最小项目,用 `go-omni api new` 创建问候示例。完整语法、目录、更新策略见 [API.md](../../API.md)。
|
||||
11
examples/contracts/shared.api
Normal file
11
examples/contracts/shared.api
Normal file
@@ -0,0 +1,11 @@
|
||||
syntax = "v1"
|
||||
|
||||
type Address {
|
||||
City string `json:"city" binding:"required,max=50"`
|
||||
}
|
||||
|
||||
type UserResp {
|
||||
UserID int64 `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
58
examples/contracts/user.api
Normal file
58
examples/contracts/user.api
Normal file
@@ -0,0 +1,58 @@
|
||||
syntax = "v1"
|
||||
|
||||
info (
|
||||
title: "User API"
|
||||
version: "1.0.0"
|
||||
)
|
||||
|
||||
import "shared.api"
|
||||
|
||||
type (
|
||||
UpdateUserReq {
|
||||
UserID int64 `path:"userId,range=[1:1000000]"`
|
||||
Notify bool `form:"notify,default=false"`
|
||||
Client string `header:"X-Client,optional"`
|
||||
Name string `json:"name" binding:"required,max=50"`
|
||||
Active bool `json:"active"`
|
||||
Age *int `json:"age,optional,range=[0:150]"`
|
||||
Address *Address `json:"address,optional"`
|
||||
}
|
||||
ListUsersReq {
|
||||
Page int `form:"page,default=1,range=[1:1000]"`
|
||||
PageSize int `form:"pageSize,default=20,range=[1:100]"`
|
||||
Tags []string `form:"tag,optional" binding:"max=10"`
|
||||
}
|
||||
ListUsersResp {
|
||||
Items []UserResp `json:"items"`
|
||||
Page int `json:"page"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
DeleteUserReq {
|
||||
UserID int64 `path:"userId,range=[1:1000000]"`
|
||||
}
|
||||
)
|
||||
|
||||
@server (
|
||||
group: user
|
||||
prefix: /api/v1
|
||||
)
|
||||
service user-api {
|
||||
@doc "Update a user"
|
||||
@handler updateUser
|
||||
put /users/:userId (UpdateUserReq) returns (UserResp)
|
||||
|
||||
@doc "List users"
|
||||
@handler listUsers
|
||||
get /users (ListUsersReq) returns (ListUsersResp)
|
||||
}
|
||||
|
||||
@server (
|
||||
group: admin/user
|
||||
prefix: /admin/v1
|
||||
middleware: Auth,Audit
|
||||
)
|
||||
service user-api {
|
||||
@doc "Delete a user; business-owned authentication and auditing required"
|
||||
@handler deleteUser
|
||||
delete /users/:userId (DeleteUserReq)
|
||||
}
|
||||
Reference in New Issue
Block a user