flow

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 27, 2019 License: MIT Imports: 14 Imported by: 2

README

flow

golang web frame as like koajs(洋葱圈模型)

安装

  • go get -u github.com/funswe/flow

示例1

func main() {
	app := flow.New()
	app.ALL("/hello", func(ctx *flow.Context) {
		ctx.Body("hello,world")
	})
	fmt.Println("启动...")
	log.Fatal(app.Run())
}

启动程序,在浏览器里访问http://localhost:9505/hello,可以看到浏览器返回hello,world

示例2

func main() {
	app := flow.New()
	app.GET("/test/:name", func(ctx *flow.Context) {
		fmt.Println("name===", ctx.GetParam("name"))
		ctx.Json(map[string]interface{}{
			"name": ctx.GetParam("name"),
		})
	})
	app.Use(func(ctx *flow.Context, next flow.Next) {
		fmt.Println("mid1->start,time==", time.Now().UnixNano())
		next()
		fmt.Println("mid1->end,time===", time.Now().UnixNano())
	})
	fmt.Println("启动...")
	log.Fatal(app.Run())
}

启动程序,在浏览器里访问http://localhost:9505/test/hello,可以看到浏览器返回{"name":"hello"},终端打印

mid1->start,time== 1550045289462400763
name=== hello
mid1->end,time=== 1550045289462472332

示例3

type request struct {
	Age  int    `json:"age,string"`
	Name string `json:"name"`
}

func main() {
	app := flow.New()
	app.GET("/test/:name", func(ctx *flow.Context) {
       	req := &request{}
       	ctx.Parse(req)
       	ctx.Json(map[string]interface{}{
       		"name": req.Name,
       		"age":  req.Age,
       	})
    })
	fmt.Println("启动...")
	log.Fatal(app.Run())
}

启动程序,在浏览器里访问http://localhost:9505/test/hello?age=30,可以看到浏览器返回{"age":30,"name":"hello"}

API

flow.New

创建一个app实例

app.Run

启动服务

app.Use

添加中间件

app.GET

添加GET路由

app.HEAD

添加HEAD路由

app.OPTIONS

添加OPTIONS路由

app.POST

添加POST路由

app.PUT

添加PUT路由

app.PATCH

添加PATCH路由

app.DELETE

添加DELETE路由

app.ALL

添加以上每种方法的路由

app.StaticFiles

设置静态文件路径

ctx.GetParam

获取请求参数,包括通配路由字段,json字段,query字段,form表单字段

ctx.Parse

将请求参数赋值到定义的结构体中,如示例3所示,方便管理请求数据

ctx.GetHeaders

获取请求的所有头信息

ctx.GetHeader

获取给定的头信息

ctx.GetUri

获取请求的uri,如:http://localhost:9505/test/hello,返回/test/hello

ctx.GetHost

获取请求的主机,如:http://localhost:9505/test/hello,返回localhost:12345

ctx.GetProtocol

获取请求的协议类型,http|https

ctx.IsSecure

判断是不是安全的连接,当Protocol是https返回true

ctx.GetOrigin

获取请求的源,如:http://localhost:9505/test/hello,返回http://localhost:12345

ctx.GetHref

获取请求的连接,如:http://localhost:9505/test/hello,返回http://localhost:12345/test/hello

ctx.GetMethod

获取请求的方法

ctx.GetQuery

获取请求的query参数,以map方式返回

ctx.GetQuerystring

获取请求的query参数,以字符串方法返回

ctx.GetHostname

获取请求的主机名,如:http://localhost:9505/test/hello,返回localhost

ctx.GetLength

获取请求体的长度

ctx.SetHeader

设置返回的头信息

ctx.SetStatus

设置http返回码

ctx.SetLength

设置返回体的长度

ctx.Redirect

重定向

ctx.Download

文件下载

ctx.Logger

获取日志对象

ctx.Json

以json方式返回

ctx.Body

以文本方式返回

ctx.Render

渲染html,使用的HTML模板pongo2

Documentation

Index

Constants

View Source
const (
	HTTP_METHOD_GET     = "GET"
	HTTP_METHOD_HEAD    = "HEAD"
	HTTP_METHOD_OPTIONS = "OPTIONS"
	HTTP_METHOD_POST    = "POST"
	HTTP_METHOD_PUT     = "PUT"
	HTTP_METHOD_PATCH   = "PATCH"
	HTTP_METHOD_DELETE  = "DELETE"
)

Variables

This section is empty.

Functions

func ALL added in v1.0.3

func ALL(path string, handler Handler)

func DELETE added in v1.0.3

func DELETE(path string, handler Handler)

func GET added in v1.0.3

func GET(path string, handler Handler)
func HEAD(path string, handler Handler)

func OPTIONS added in v1.0.3

func OPTIONS(path string, handler Handler)

func PATCH added in v1.0.3

func PATCH(path string, handler Handler)

func POST added in v1.0.3

func POST(path string, handler Handler)

func PUT added in v1.0.3

func PUT(path string, handler Handler)

func Run added in v1.0.3

func Run() error

func SetAddress added in v1.0.3

func SetAddress(addr string)

func SetAppName added in v1.0.3

func SetAppName(a string)

func SetLogPath added in v1.0.3

func SetLogPath(lp string)

func SetNotFoundHandle added in v1.0.3

func SetNotFoundHandle(nfh NotFoundHandle)

func SetPanicHandler added in v1.0.3

func SetPanicHandler(ph PanicHandler)

func SetProxy added in v1.0.3

func SetProxy(p bool)

func SetViewPath added in v1.0.3

func SetViewPath(vp string)

func StaticFiles added in v1.0.3

func StaticFiles(prefix, path string)

func Use added in v1.0.3

func Use(m Middleware)

Types

type Context

type Context struct {
	Logger *log.Logger
	// contains filtered or unexported fields
}

func (*Context) Body

func (c *Context) Body(body string)

func (*Context) Download

func (c *Context) Download(filePath string)

func (*Context) GetHeader

func (c *Context) GetHeader(key string) string

func (*Context) GetHeaders

func (c *Context) GetHeaders() map[string][]string

func (*Context) GetHost

func (c *Context) GetHost() string

func (*Context) GetHostname

func (c *Context) GetHostname() string

func (*Context) GetHref

func (c *Context) GetHref() string

func (*Context) GetLength

func (c *Context) GetLength() int

func (*Context) GetMethod

func (c *Context) GetMethod() string

func (*Context) GetOrigin

func (c *Context) GetOrigin() string

func (*Context) GetParam

func (c *Context) GetParam(key string) (value string)

func (*Context) GetParamDefault

func (c *Context) GetParamDefault(key, defaultValue string) (value string)

func (*Context) GetProtocol

func (c *Context) GetProtocol() string

func (*Context) GetQuery

func (c *Context) GetQuery() url.Values

func (*Context) GetQuerystring

func (c *Context) GetQuerystring() string

func (*Context) GetUri

func (c *Context) GetUri() string

func (*Context) GetUserAgent added in v1.0.2

func (c *Context) GetUserAgent() string

func (*Context) IsSecure

func (c *Context) IsSecure() bool

func (*Context) Json added in v1.0.2

func (c *Context) Json(data map[string]interface{})

func (*Context) Parse added in v1.0.2

func (c *Context) Parse(object interface{}) error

func (*Context) Redirect

func (c *Context) Redirect(url string, code int)

func (*Context) Render added in v1.0.2

func (c *Context) Render(tmpFile string, data map[string]interface{})

func (*Context) SetHeader

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

func (*Context) SetLength

func (c *Context) SetLength(length int) *Context

func (*Context) SetStatus

func (c *Context) SetStatus(code int) *Context

type Handler

type Handler func(ctx *Context)

type Middleware

type Middleware func(ctx *Context, next Next)

type Next

type Next func()

type NotFoundHandle added in v1.0.2

type NotFoundHandle func(w http.ResponseWriter, r *http.Request)

func (NotFoundHandle) ServeHTTP added in v1.0.2

func (f NotFoundHandle) ServeHTTP(w http.ResponseWriter, r *http.Request)

type PanicHandler added in v1.0.2

type PanicHandler func(http.ResponseWriter, *http.Request, interface{})

Directories

Path Synopsis
utils

Jump to

Keyboard shortcuts

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