Firmeve
1.0.0
1.0.0
  • Firmeve
  • [核心组件]
    • 应用
    • 容器
    • 配置
    • 提供者
    • 事件
    • 日志
    • 命令
  • [基础组件]
    • Http服务
    • 数据验证
    • 数据库
    • 缓存
    • JWT
    • Redis
    • 辅助函数
    • 队列
    • 计划任务
Powered by GitBook
On this page
  • 路由定义
  • 路由中间件
  • 路由分组
  • 启动Http服务
  • Http服务
  • Https服务
  • Http2服务
  • Context
  • 可用方法

Was this helpful?

  1. [基础组件]

Http服务

基础的Http服务和Router以及Context处理

路由定义

firmeve路由是基于httprouter进行扩展,更多httprouter用法参见其文档

//基础示例
router := http.New(firmeve.New())
router.GET("/ping", func(ctx contract.Context) {
    ctx.RenderWith(200,render2.Plain,"pong")
    ctx.Next()
})

路由中间件

router.GET("/ping", func(ctx contract.Context) {
    ctx.RenderWith(200,render2.Plain,"pong")
    ctx.Next()
}).Use(func(ctx contract.Context) {
    ctx.RenderWith(200,render2.Plain,"Before")
  ctx.Next()
}).Use(func(ctx contract.Context) {
    ctx.RenderWith(200,render2.Plain,"After")
   ctx.Next()
})

路由分组

v1 := router.Group("/api/v1").Use(func(ctx *http.Context) {
                                ctx.RenderWith(200,render2.Plain,"Before")
                               ctx.Next()
                             },func(ctx *http.Context) {
                               ctx.RenderWith(200,render2.Plain,"After")
                               ctx.Next()
                             })
{
    v1.Get("/ping", func(ctx *http.Context) {
       ctx.RenderWith(200,render.JSON,map[string]string{
               "message": "something"
       })
       ctx.Next()
   })
}

启动Http服务

Http服务

go run main.go http:serve --host=0.0.0.0:22182

Https服务

go run main.go http:serve --host=0.0.0.0:22182 --key-file=server.key --cert-file=server.crt

Http2服务

go run main.go http:serve --host=0.0.0.0:22182 --key-file=server.key --cert-file=server.crt --http2

假设server.key,server.crt和main在同一目录

Context

可用方法

Firmeve() Application

// 获取当前协议
Protocol() Protocol

Next()

Handlers() []ContextHandler

AddEntity(key string, value interface{})

Entity(key string) *ContextEntity

Abort()

Error(status int, err error)

Bind(v interface{}) error

BindWith(b Binding, v interface{}) error

Get(key string) interface{}

Render(status int, v interface{}) error

RenderWith(status int, r Render, v interface{}) error

Clone() Context

Resolve(abstract interface{}, params ...interface{}) interface{}
Previous[基础组件]Next数据验证

Last updated 4 years ago

Was this helpful?