https://github.com/farseer-go/webapi
用于快速构建api服务,带来极简、优雅的开发体验。编写api服务时,不需要使用httpRequest、httpResponse等数据结构。
https://github.com/farseer-go/webapi
action api controller model mvc webapi
Last synced: 5 months ago
JSON representation
用于快速构建api服务,带来极简、优雅的开发体验。编写api服务时,不需要使用httpRequest、httpResponse等数据结构。
- Host: GitHub
- URL: https://github.com/farseer-go/webapi
- Owner: farseer-go
- License: mit
- Created: 2022-08-28T12:57:00.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-09-29T15:18:15.000Z (9 months ago)
- Last Synced: 2025-09-29T16:16:20.593Z (9 months ago)
- Topics: action, api, controller, model, mvc, webapi
- Language: Go
- Homepage: https://farseer-go.gitee.io/
- Size: 322 KB
- Stars: 76
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# webapi 概述
> 包:`"github.com/farseer-go/webapi"`
>
> 模块:`webapi.Module`
- `Document`
- [English](https://farseer-go.github.io/doc/#/en-us/)
- [中文](https://farseer-go.github.io/doc/)
- [English](https://farseer-go.github.io/doc/#/en-us/)
- Source
- [github](https://github.com/farseer-go/fs)




[](https://codecov.io/gh/farseer-go/webapi)

[](https://github.com/farseer-go/webapi/actions/workflows/test.yml)

> 用于快速构建api服务,带来极简、优雅的开发体验。编写api服务时,不需要使用httpRequest、httpResponse等数据结构。
webapi使用了中间件的管道模型编写,让我们加入非业务逻辑时非常简单。
包含两种风格来提供API服务:
- `MinimalApi`:动态API风格(直接绑定到逻辑层)
- `Mvc`:Controller、Action风格
> 使用minimalApi时,甚至不需要UI层来提供API服务。
## webapi有哪些功能
- 支持中间件
- 入参、出参隐式绑定
- 支持静态目录绑定
- ActionFilter过虑器
- ActionResult抽象结果
- Area区域设置
- MinimalApi模式
- Mvc模式
- HttpContext上下文
- Header隐式绑定
大部份情况下,除了main需要配置webapi路由外,在你的api handle中就是一个普通的func函数,不需要依赖webapi组件。webapi会根据`func函数`的`出入参`来`隐式绑定数据`。
```go
func main() {
fs.Initialize[webapi.Module]("FOPS")
webapi.RegisterPOST("/mini/hello1", Hello1)
webapi.RegisterPOST("/mini/hello3", Hello3, "pageSize", "pageIndex")
webapi.Run()
}
// 使用结构(DTO)来接收入参
// 返回string
func Hello1(req pageSizeRequest) string {
return fmt.Sprintf("hello world pageSize=%d,pageIndex=%d", req.PageSize, req.PageIndex)
}
// 使用基础参数来接收入参
// 返回pageSizeRequest结构(会自动转成json)
func Hello3(pageSize int, pageIndex int) pageSizeRequest {
return pageSizeRequest{
PageSize: pageSize,
PageIndex: pageIndex,
}
}
// 也可以定义一个结构,用于接收参数
type pageSizeRequest struct {
PageSize int
PageIndex int
}
```
函数中,`出入参都会自动绑定数据`
> 如果是`application/json`,则会自动被反序列化成model,如果是`x-www-form-urlencoded`,则会将每一项的key/value匹配到model字段中
可以看到,整个过程,`不需要`做`json序列化`、`httpRequest`、`httpResponse`的操作。