webapi

package module
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 29 Imported by: 2

README

webapi 概述

包:"github.com/farseer-go/webapi"

模块:webapi.Module

codecov Build

用于快速构建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函数出入参隐式绑定数据

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序列化httpRequesthttpResponse的操作。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Area

func Area(area string, f func())

Area 设置区域

func GetHttpContext added in v0.8.0

func GetHttpContext() *context.HttpContext

GetHttpContext 在minimalApi模式下也可以获取到上下文

func NewApplicationBuilder added in v0.3.0

func NewApplicationBuilder() *applicationBuilder

func PrintRoute added in v0.9.0

func PrintRoute()

PrintRoute 打印所有路由信息到控制台

func RegisterController

func RegisterController(c controller.IController)

RegisterController 自动注册控制器下的所有Action方法

func RegisterDELETE

func RegisterDELETE(route string, actionFunc any, params ...string)

RegisterDELETE 注册单个Api(支持占位符,例如:/{cateId}/{Id})

func RegisterGET

func RegisterGET(route string, actionFunc any, params ...string)

RegisterGET 注册单个Api(支持占位符,例如:/{cateId}/{Id})

func RegisterMiddleware

func RegisterMiddleware(m context.IMiddleware)

func RegisterPOST

func RegisterPOST(route string, actionFunc any, params ...string)

RegisterPOST 注册单个Api(支持占位符,例如:/{cateId}/{Id})

func RegisterPUT

func RegisterPUT(route string, actionFunc any, params ...string)

RegisterPUT 注册单个Api(支持占位符,例如:/{cateId}/{Id})

func RegisterRoutes added in v0.2.0

func RegisterRoutes(routes ...Route)

RegisterRoutes 批量注册路由

func Run

func Run(params ...string)

Run 运行Web服务(默认8888端口)

func UseApiDoc added in v0.9.0

func UseApiDoc()

UseApiDoc 是否开启Api文档

func UseApiResponse

func UseApiResponse()

UseApiResponse 让所有的返回值,包含在core.ApiResponse中

func UseCors

func UseCors()

UseCors 使用CORS中间件

func UseHealthCheck added in v0.12.0

func UseHealthCheck(routes ...string)

UseHealthCheck 【GET】开启健康检查(默认route = "/healthCheck")

func UsePprof added in v0.3.0

func UsePprof()

UsePprof 是否同时开启pprof

func UseSession added in v0.8.0

func UseSession()

UseSession 开启Session

func UseStaticFiles

func UseStaticFiles()

UseStaticFiles 支持静态目录,在根目录./wwwroot中的文件,直接以静态文件提供服务

func UseTLS added in v0.3.0

func UseTLS(certFile, keyFile string)

UseTLS 使用https

func UseValidate added in v0.9.0

func UseValidate()

UseValidate 使用字段验证器

func UseWebApi

func UseWebApi()

Types

type ISessionMiddlewareCreat added in v0.11.0

type ISessionMiddlewareCreat interface {
	// Create 创建Session中间件
	Create() context.IMiddleware
}

type Module

type Module struct {
}

func (Module) DependsModule

func (module Module) DependsModule() []modules.FarseerModule

func (Module) PostInitialize

func (module Module) PostInitialize()

func (Module) PreInitialize

func (module Module) PreInitialize()

type Route added in v0.2.0

type Route struct {
	Method  string            // Method类型(GET|POST|PUT|DELETE)
	Url     string            // 路由地址
	Action  any               // Handle
	Message string            // api返回的StatusMessage
	Filters []context.IFilter // 过滤器(对单个路由的执行单元)
	Params  []string          // Handle的入参
}

Route 路由配置

func (Route) Filter added in v0.9.0

func (receiver Route) Filter(filter context.IFilter) Route

Filter 添加过滤器

func (Route) GET added in v0.9.0

func (receiver Route) GET() Route

GET 使用GET

func (Route) POST added in v0.9.0

func (receiver Route) POST() Route

POST 使用POST

func (Route) UseJwt added in v0.9.0

func (receiver Route) UseJwt() Route

UseJwt 使用Jwt

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL