flow

package module
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: MIT Imports: 26 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 (
	HttpMethodGet     = "GET"
	HttpMethodHead    = "HEAD"
	HttpMethodOptions = "OPTIONS"
	HttpMethodPost    = "POST"
	HttpMethodPut     = "PUT"
	HttpMethodPatch   = "PATCH"
	HttpMethodDelete  = "DELETE"
)
View Source
const (
	HttpHeaderContentType             = "Content-Type"
	HttpHeaderContentLength           = "Content-Length"
	HttpHeaderTransferEncoding        = "Transfer-Encoding"
	HttpHeaderContentDisposition      = "Content-Disposition"
	HttpHeaderContentTransferEncoding = "Content-Transfer-Encoding"
	HttpHeaderExpires                 = "Expires"
	HttpHeaderCacheControl            = "Cache-Control"
	HttpHeaderEtag                    = "Etag"
	HttpHeaderXForwardedHost          = "X-Forwarded-Host"
	HttpHeaderXForwardedProto         = "X-Forwarded-Proto"
	HttpHeaderIfModifiedSince         = "If-Modified-Since"
	HttpHeaderIfNoneMatch             = "If-None-Match"
	HttpHeaderLastModified            = "Last-Modified"
	HttpHeaderXContentTypeOptions     = "X-Content-Type-Options"
	HttpHeaderXPoweredBy              = "X-Powered-By"
	HttpHeaderCorsOrigin              = "Access-Control-Allow-Origin"
	HttpHeaderCorsMethods             = "Access-Control-Allow-Methods"
	HttpHeaderCorsHeaders             = "Access-Control-Allow-Headers"
)
View Source
const Nil = NotExistError("flow-redis: key not exist")

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 SetCorsConfig added in v1.3.3

func SetCorsConfig(corsConfig *CorsConfig)

func SetCurlConfig added in v1.3.3

func SetCurlConfig(curlConfig *CurlConfig)

func SetLoggerConfig added in v1.2.0

func SetLoggerConfig(loggerConfig *LoggerConfig)

func SetNotFoundHandle added in v1.0.3

func SetNotFoundHandle(nfh NotFoundHandle)

func SetOrmConfig added in v1.2.0

func SetOrmConfig(ormConfig *OrmConfig)

func SetPanicHandler added in v1.0.3

func SetPanicHandler(ph PanicHandler)

func SetRedisConfig added in v1.3.3

func SetRedisConfig(redisConfig *RedisConfig)

func SetServerConfig added in v1.2.0

func SetServerConfig(serverConfig *ServerConfig)

func Use added in v1.0.3

func Use(m Middleware)

Types

type Application

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

type Context

type Context struct {
	Logger *log.Logger

	Orm   *Orm
	Redis *RedisClient
	Curl  *Curl
	// 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) GetApp added in v1.2.0

func (c *Context) GetApp() *Application

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 CorsConfig added in v1.3.3

type CorsConfig struct {
	Enable         bool
	AllowOrigin    string
	AllowedHeaders string
	AllowedMethod  string
}

type Curl added in v1.3.3

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

func (*Curl) Get added in v1.3.3

func (c *Curl) Get(url string, data interface{}, headers map[string]string) (CurlResult, error)

func (*Curl) Post added in v1.3.3

func (c *Curl) Post(url string, data interface{}, headers map[string]string) (CurlResult, error)

type CurlConfig added in v1.3.3

type CurlConfig struct {
	Enable  bool
	Timeout time.Duration
	Headers map[string]string
}

type CurlResult added in v1.3.3

type CurlResult string

func (CurlResult) Parse added in v1.3.3

func (cr CurlResult) Parse(v interface{}) error

func (CurlResult) Raw added in v1.3.3

func (cr CurlResult) Raw(v interface{}) string

type Handler

type Handler func(ctx *Context)

type LoggerConfig added in v1.2.0

type LoggerConfig struct {
	LoggerLevel string
	LoggerPath  string
}

type Middleware

type Middleware func(ctx *Context, next Next)

type Next

type Next func()

type NotExistError added in v1.3.3

type NotExistError string

func (NotExistError) Error added in v1.3.3

func (e NotExistError) Error() string

func (NotExistError) NotExistError added in v1.3.3

func (NotExistError) NotExistError()

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 Orm added in v1.2.0

type Orm struct {
	Op       *OrmOp
	JoinType *OrmJoinType
	// contains filtered or unexported fields
}

func (*Orm) Count added in v1.2.6

func (orm *Orm) Count(count *int64, fromTable *OrmFromTable, conditions []*OrmWhere) error

func (*Orm) DB added in v1.2.6

func (orm *Orm) DB() *gorm.DB

func (*Orm) Query added in v1.2.6

func (orm *Orm) Query(dest interface{}, fields []*OrmColumn, fromTable *OrmFromTable, conditions []*OrmWhere, orderBy []*OrmOrderBy, limit *OrmLimit, groupBy *OrmGroupBy) error

type OrmColumn added in v1.2.6

type OrmColumn struct {
	Table  string
	Column string
	Alias  string
}

type OrmConfig added in v1.2.0

type OrmConfig struct {
	Enable   bool
	UserName string
	Password string
	DbName   string
	Host     string
	Port     int
	Pool     *OrmPool
}

type OrmFromTable added in v1.2.6

type OrmFromTable struct {
	Table string
	Alias string
	Joins []*OrmJoin
}

type OrmGroupBy added in v1.2.6

type OrmGroupBy struct {
	Columns []*OrmColumn
	Having  []*OrmWhere
}

type OrmJoin added in v1.2.6

type OrmJoin struct {
	Type  string
	Table string
	Alias string
	ON    []*OrmWhere
}

type OrmJoinType added in v1.2.7

type OrmJoinType struct {
	CrossJoin string
	InnerJoin string
	LeftJoin  string
	RightJoin string
}

type OrmLimit added in v1.2.6

type OrmLimit struct {
	Limit  int
	Offset int
}

type OrmOp added in v1.2.6

type OrmOp struct {
	Eq   string
	Gt   string
	Gte  string
	Lt   string
	Lte  string
	IN   string
	Like string
	Or   string
}

type OrmOrderBy added in v1.2.6

type OrmOrderBy struct {
	Column *OrmColumn
	Desc   bool
}

type OrmPool added in v1.2.0

type OrmPool struct {
	MaxIdle         int
	MaxOpen         int
	ConnMaxLifeTime int64
	ConnMaxIdleTime int64
}

type OrmWhere added in v1.2.6

type OrmWhere struct {
	Column *OrmColumn
	Opt    string
	Value  interface{}
}

type PanicHandler added in v1.0.2

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

type RedisClient added in v1.3.3

type RedisClient struct {
	NotExist NotExistError
	// contains filtered or unexported fields
}

func (*RedisClient) Get added in v1.3.3

func (rd *RedisClient) Get(key string, v interface{}) error

func (*RedisClient) GetRaw added in v1.3.3

func (rd *RedisClient) GetRaw(key string) (string, error)

func (*RedisClient) Set added in v1.3.3

func (rd *RedisClient) Set(key string, value interface{}, expiration time.Duration) error

func (*RedisClient) SetRaw added in v1.3.3

func (rd *RedisClient) SetRaw(key string, value string, expiration time.Duration) error

type RedisConfig added in v1.3.3

type RedisConfig struct {
	Enable   bool
	Password string
	DbNum    int
	Host     string
	Port     int
}

type ServerConfig added in v1.2.0

type ServerConfig struct {
	AppName    string
	Proxy      bool
	Host       string
	Port       int
	ViewPath   string
	StaticPath string
}

Directories

Path Synopsis
utils

Jump to

Keyboard shortcuts

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