• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ryouaki/koa: A golang framework like koa.js

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

ryouaki/koa

开源软件地址:

https://github.com/ryouaki/koa

开源编程语言:

Go 100.0%

开源软件介绍:

koa.go

Expressive HTTP middleware framework for Golang to make web applications and APIs more enjoyable to write like Koa.js. Koa's middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.

Koa is not bundled with any middleware.

Installation

  $ go get github.com/ryouaki/koa

Hello Koa

  package main

  import (
    "fmt"

    "github.com/ryouaki/koa"
  )

  func main() {
    app := koa.New() // 初始化服务对象

    // 设置api路由,其中var为url传参
    app.Get("/", func(ctx *koa.Context, next koa.Next) {
      ctx.SetBody([]byte("Hello Koa"))
    })

    err := app.Run(8080) // 启动
    if err != nil {      // 是否发生错误
      fmt.Println(err)
    }
  }

Middleware

Koa is a middleware framework, Here is an example of logger middleware with each of the different functions:

  package plugin

  import (
    "fmt"
    "time"

    "github.com/ryouaki/koa"
  )

  // Log out about the duration for request.
  func Duration(ctx *koa.Context, next koa.Next) {
    startTime := time.Now() // 开始计时
    next(nil) // 执行后续操作
    d := time.Now().Sub(startTime) // 计算耗时
    // 打印结果
    fmt.Println(time.Date(startTime.Year(),
      startTime.Month(),
      startTime.Day(),
      startTime.Hour(),
      startTime.Minute(),
      startTime.Second(), 0, time.Local),
      ctx.URL,
      "Request cost: ",
      float64(d)/float64(time.Millisecond), "ms")
  }

Koa Application

  type Application struct {
    handles []Handle
    _cb     Handler
  }

  type Handle struct {
    path     string
    method  string
    handler Handler
  }

  // Handler Func
  type Handler func(ctx *Context, n Next)

  // Next Func
  type Next func()

  // New a Koa instance
  func New() *Application
  // Add a middleware for koa application
  /**
  * params: path<string|option> path for request
  * params: callback<koa.Handler|option> cb for request
  * params: callback ...
  */
  func (app *Application) Use(argus ...interface{})
  // Get func
  func (app *Application) Get(path string, cbFunc ...Handler) error
  // Post func
  func (app *Application) Post(path string, cbFunc ...Handler) error
  // Delete func
  func (app *Application) Delete(path string, cbFunc ...Handler) error
  // Patch func
  func (app *Application) Patch(path string, cbFunc ...Handler) error 
  // Put func
  func (app *Application) Put(path string, cbFunc ...Handler) error
  // Options func
  func (app *Application) Options(path string, cbFunc ...Handler) error
  // Head func
  func (app *Application) Head(path string, cbFunc ...Handler) error
  // Run func
  func (app *Application) Run(port int) error 
  // RunTLS func
  func (app *Application) RunTLS(port int, certFile string, keyFile string) error 

About Context

  type Context struct {
    Res      http.ResponseWriter    // Res the response object
    Req      *http.Request          // Req the request object from client
    Url      string                 // Url
    Path     string                 // Rqeust path
    Method   string                 // Method like Get, Post and others
    Status   int                    // Status you want to let client konw for the request
    MatchURL string                 // For the router path
    Body     []uint8                // The body from client
    Query    map[string]([]string)  // The Query from request's path
    Params   map[string](string)    // The Params from request's path
  }

  // New a context for request
  func NewContext(res http.ResponseWriter, req *http.Request) *Context
  // Get Header from request func
  func (ctx *Context) GetHeader(key string) []string 
  // Set header item for Response func
  func (ctx *Context) SetHeader(key string, value string)
  // Get cookie from request func
  func (ctx *Context) GetCookie(key string) *http.Cookie
  // Set cookie for response func
  func (ctx *Context) SetCookie(cookie *http.Cookie)
  // Set data for context func
  func (ctx *Context) GetData(key string) interface{}
  // Get data from contexxt  func
  func (ctx *Context) SetData(key string, value interface{})
  // Set data for response
  func (ctx *Context) SetBody(data []byte)

Components




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap