|
2 years ago | |
---|---|---|
.. | ||
api | 2 years ago | |
bug | 3 years ago | |
cmd | 2 years ago | |
compare | 3 years ago | |
config | 3 years ago | |
docker | 2 years ago | |
env | 3 years ago | |
internal | 2 years ago | |
kube | 2 years ago | |
migrate | 3 years ago | |
model | 2 years ago | |
pkg | 2 years ago | |
plugin | 3 years ago | |
quickstart | 2 years ago | |
rpc | 2 years ago | |
test | 2 years ago | |
tpl | 3 years ago | |
update | 3 years ago | |
upgrade | 3 years ago | |
util | 2 years ago | |
vars | 3 years ago | |
.gitignore | 3 years ago | |
Dockerfile | 3 years ago | |
Makefile | 3 years ago | |
go.mod | 2 years ago | |
go.sum | 2 years ago | |
goctl.go | 3 years ago | |
readme-cn.md | 2 years ago | |
readme.md | 2 years ago |
English | 简体中文
goctl api [go/java/ts] [-api user/user.api] [-dir ./src]
api 后面接生成的语言,现支持go/java/typescript
-api 自定义api所在路径
-dir 自定义生成目录
type int userType
type user {
name string `json:"user"` // 用户姓名
}
type student {
name string `json:"name"` // 学生姓名
}
type teacher {
}
type (
address {
city string `json:"city"` // 城市
}
innerType {
image string `json:"image"`
}
createRequest {
innerType
name string `form:"name"`
age int `form:"age,optional"`
address []address `json:"address,optional"`
}
getRequest {
name string `path:"name"`
age int `form:"age,optional"`
}
getResponse {
code int `json:"code"`
desc string `json:"desc,omitempty"`
address address `json:"address"`
service int `json:"service"`
}
)
service user-api {
@server(
handler: GetUserHandler
group: user
)
get /api/user/:name(getRequest) returns(getResponse)
@server(
handler: CreateUserHandler
group: user
)
post /api/users/create(createRequest)
}
@server(
jwt: Auth
group: profile
)
service user-api {
@handler GetProfileHandler
get /api/profile/:name(getRequest) returns(getResponse)
@handler CreateProfileHandler
post /api/profile/create(createRequest)
}
service user-api {
@handler PingHandler
head /api/ping()
}
get /api/profile/:name(getRequest) returns(getResponse)
中get代表api的请求方式(get/post/put/delete), /api/profile/:name
描述了路由path,:name
通过
请求getRequest里面的属性赋值,getResponse为返回的结构体,这两个类型都定义在2描述的类型中。开发者可以在vscode中搜索goctl的api插件,它提供了api语法高亮,语法检测和格式化相关功能。
命令如下:
goctl api go -api user/user.api -dir user
.
├── internal
│ ├── config
│ │ └── config.go
│ ├── handler
│ │ ├── pinghandler.go
│ │ ├── profile
│ │ │ ├── createprofilehandler.go
│ │ │ └── getprofilehandler.go
│ │ ├── routes.go
│ │ └── user
│ │ ├── createuserhandler.go
│ │ └── getuserhandler.go
│ ├── logic
│ │ ├── pinglogic.go
│ │ ├── profile
│ │ │ ├── createprofilelogic.go
│ │ │ └── getprofilelogic.go
│ │ └── user
│ │ ├── createuserlogic.go
│ │ └── getuserlogic.go
│ ├── svc
│ │ └── servicecontext.go
│ └── types
│ └── types.go
└── user.go
生成的代码可以直接跑,有几个地方需要改:
servicecontext.go
里面增加需要传递给logic的一些资源,比如mysql, redis,rpc等goctl api java -api user/user.api -dir ./src
goctl api ts -api user/user.api -dir ./src -webapi ***
ts需要指定webapi所在目录
goctl api dart -api user/user.api -dir ./src