koa

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2021 License: MIT Imports: 14 Imported by: 4

README

koa.go

Expressive HTTP middleware framework for Golang to make web applications and APIs more enjoyable to write like Koa.js for Nodejs. 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(err error, ctx *koa.Context, next koa.NextCb) {
      ctx.Write([]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.go"
  )

  // Log out about the duration for request.
  func Duration(err error, ctx *koa.Context, next koa.NextCb) {
    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")
  }

About Context

  type Context struct {
    Header   map[string]([]string)  // Header which you get from client
    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 url
    Params   map[string](string)    // The Params from request's path
    IsFinish bool                   // One request only can be done by one time
    data     map[string]interface{} // ...
  }

  // Get the information from request's header 
  func (ctx *Context) GetHeader(key string) []string 
  // Set the information to response's header
  func (ctx *Context) SetHeader(key string, value string)
  // Get the information from request's cookie 
  func (ctx *Context) GetCookie(key string) *http.Cookie
  // Set the information to response's cookie
  func (ctx *Context) SetCookie(cookie *http.Cookie)
  // Get the information from context
  func (ctx *Context) GetData(key string) interface{}
  // Set the information to context
  func (ctx *Context) SetData(key string, value interface{})
  // Set the information to session, but you should use session middleware first
  func (ctx *Context) SetSession(key string, value interface{}) 
  // Update the session, but you should use session middleware first
  func (ctx *Context) UpdateSession(sess map[string]interface{})
  // Get the information from session, but you should use session middleware first
  func (ctx *Context) GetSession() map[string]interface{}
  // Send the data for response
  func (ctx *Context) Write(data []byte) (int, error)
  // Check if the response is done, it's very important for middleware.
  func (ctx *Context) IsFinished() bool

Components

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes2String

func Bytes2String(bs []uint8) string

Bytes2String func

func GetGoroutineID added in v1.0.1

func GetGoroutineID() uint64

GetGoroutineID func

func GetIPAddr added in v1.0.1

func GetIPAddr() string

GetIPAddr func

func GetMD5ID added in v1.0.1

func GetMD5ID(b []byte) string

GetMD5ID func

func StructAssign added in v1.0.6

func StructAssign(binding interface{}, value interface{})

StructAssign func

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

Application Object

func New

func New() *Application

New for a koa instance

func (*Application) Delete

func (app *Application) Delete(path string, cbFunc ...Handler) error

Delete func

func (*Application) Get

func (app *Application) Get(path string, cbFunc ...Handler) error

Get func

func (*Application) Head

func (app *Application) Head(path string, cbFunc ...Handler) error

Head func

func (*Application) Options

func (app *Application) Options(path string, cbFunc ...Handler) error

Options func

func (*Application) Patch

func (app *Application) Patch(path string, cbFunc ...Handler) error

Patch func

func (*Application) Post

func (app *Application) Post(path string, cbFunc ...Handler) error

Post func

func (*Application) Put

func (app *Application) Put(path string, cbFunc ...Handler) error

Put func

func (*Application) Run

func (app *Application) Run(port int) error

Run func

func (*Application) RunTLS

func (app *Application) RunTLS(port int, certFile string, keyFile string) error

RunTLS func

func (*Application) ServeHTTP

func (app *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP interface func

func (*Application) Use

func (app *Application) Use(argus ...interface{})

Use func

type Context

type Context struct {
	Header          map[string]([]string)
	Res             http.ResponseWriter
	Req             *http.Request
	URL             string
	Path            string
	Method          string
	Status          int
	MatchURL        string
	Body            []uint8
	Query           map[string]([]string)
	Params          map[string](string)
	IsFinish        bool
	RequestNotFound bool
	// contains filtered or unexported fields
}

Context Request

func (*Context) GetCookie

func (ctx *Context) GetCookie(key string) *http.Cookie

GetCookie func

func (*Context) GetData

func (ctx *Context) GetData(key string) interface{}

GetData func

func (*Context) GetHeader

func (ctx *Context) GetHeader(key string) []string

GetHeader func

func (*Context) GetSession

func (ctx *Context) GetSession() map[string]interface{}

GetSession func

func (*Context) IsFinished

func (ctx *Context) IsFinished() bool

IsFinished func

func (*Context) SetCookie

func (ctx *Context) SetCookie(cookie *http.Cookie)

SetCookie func

func (*Context) SetData

func (ctx *Context) SetData(key string, value interface{})

SetData func

func (*Context) SetHeader

func (ctx *Context) SetHeader(key string, value string)

SetHeader func

func (*Context) SetSession

func (ctx *Context) SetSession(key string, value interface{})

SetSession func

func (*Context) UpdateSession

func (ctx *Context) UpdateSession(sess map[string]interface{})

UpdateSession func

func (*Context) Write

func (ctx *Context) Write(data []byte) (int, error)

type Handler

type Handler func(err error, ctx *Context, next NextCb)

Handler Func

type MiddlewareHandler

type MiddlewareHandler struct {
	// contains filtered or unexported fields
}

MiddlewareHandler struct

type NextCb

type NextCb func(err error)

NextCb Func

type RouterHandler

type RouterHandler struct {
	// contains filtered or unexported fields
}

RouterHandler struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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