flow

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2019 License: MIT Imports: 17 Imported by: 2

README

flow

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

安装

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

示例1

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

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

示例2

func main() {
	flow.GET("/test/:name", func(ctx *flow.Context) {
		fmt.Println("name===", ctx.GetParam("name"))
		ctx.Json(map[string]interface{}{
			"name": ctx.GetParam("name"),
		})
	})
	flow.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(flow.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() {
	flow.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(flow.Run())
}

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

更多例子

模板

使用的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 SetLoggerLevel added in v1.0.6

func SetLoggerLevel(lv 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) GetStatusCode added in v1.0.8

func (c *Context) GetStatusCode() int

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