ink

package
v0.0.0-...-8375e82 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2016 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

gopic web framework

Index

Constants

View Source
const (
	CONTEXT_RENDERED = "context_rendered"
	CONTEXT_END      = "context_end"
	CONTEXT_SEND     = "context_send"
)
View Source
const (
	ROUTER_METHOD_GET    = "GET"
	ROUTER_METHOD_POST   = "POST"
	ROUTER_METHOD_PUT    = "PUT"
	ROUTER_METHOD_DELETE = "DELETE"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

App struct is top level application. It contains Router,View,Config and private fields.

func New

func New() *App

func NewApp

func NewApp(configPath string) *App

New creates an App instance. It loads config.json file if exist. Otherwise, set default config values to Config.

func (*App) Config

func (app *App) Config() *Config

Config returns global *Config instance.

func (*App) Delete

func (app *App) Delete(key string, fn ...Handler)

Register DELETE handlers to router.

func (*App) Get

func (app *App) Get(key string, fn ...Handler) string

Get app config value if only key string given, return string value. If fn slice given, register GET handlers to router with pattern string.

func (*App) NotFound

func (app *App) NotFound(h Handler)

Register NotFound handler. It's invoked after calling route handler but not matched.

func (*App) Post

func (app *App) Post(key string, fn ...Handler)

Register POST handlers to router.

func (*App) Put

func (app *App) Put(key string, fn ...Handler)

Register PUT handlers to router.

func (*App) Recover

func (app *App) Recover(h Handler)

Register panic recover handler. It's invoked when panic error in middleware and route handlers.

func (*App) Route

func (app *App) Route(method string, key string, fn ...Handler)

Register handlers to router with custom methods and pattern string. Support GET,POST,PUT and DELETE methods. Usage:

app.Route("GET,POST","/test",handler)

func (*App) Run

func (app *App) Run()

Run http server and listen on config value or 9001 by default.

func (*App) ServeHTTP

func (app *App) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP is HTTP server implement method. It makes App compatible to native http handler.

func (*App) Set

func (app *App) Set(key string, v interface{})

Set app config value.

func (*App) Static

func (app *App) Static(h Handler)

Register static file handler. It's invoked before route handler after middleware handler.

func (*App) Use

func (app *App) Use(h ...Handler)

Use adds middleware handlers. Middleware handlers invoke before route handler in the order that they are added.

func (*App) View

func (app *App) View() *View

View returns global *View instance.

type Config

type Config map[string]map[string]interface{}

Config instance. It's a two-level map.

func NewConfig

func NewConfig(fileAbsPath string) (*Config, error)

NewConfig creates new Config instance with json file.

func (*Config) Bool

func (cfg *Config) Bool(key string) bool

Bool returns config bool.

func (*Config) Float

func (cfg *Config) Float(key string) float64

Float returns config float64 by given string.

func (*Config) FloatOr

func (cfg *Config) FloatOr(key string, def float64) float64

FloatOr returns config float64 by given string. It returns def float64 if float 0.

func (*Config) Int

func (cfg *Config) Int(key string) int

Int returns config int by given string.

func (*Config) IntOr

func (cfg *Config) IntOr(key string, def int) int

IntOr returns config int by given string. It returns def int if 0.

func (*Config) Set

func (cfg *Config) Set(key string, value interface{})

Set value with given key string. The key need as "section.name".

func (*Config) String

func (cfg *Config) String(key string) string

String returns config string by given key string.

func (*Config) StringOr

func (cfg *Config) StringOr(key string, def string) string

StringOr returns config string by given key string. It returns def string if empty string.

type Context

type Context struct {
	// raw *http.Request
	Request *http.Request
	// Base url, as http://domain/
	Base string
	// Path url, as http://domain/path
	Url string
	// Request url, as http://domain/path?queryString#fragment
	RequestUrl string
	// Request method, GET,POST, etc
	Method string
	// Client Ip
	Ip string
	// Client user agent
	UserAgent string
	// Last visit refer url
	Referer string
	// Request host
	Host string
	// Request url suffix
	Ext string
	// Is https
	IsSSL bool
	// Is ajax
	IsAjax bool

	// native http.ResponseWriter
	Response http.ResponseWriter
	// Response status
	Status int
	// Response header map
	Header map[string]string
	// Response body bytes
	Body []byte

	// Response is sent or not
	IsSend bool
	// Response is end or not
	IsEnd bool
	// contains filtered or unexported fields
}

Context instance represents a request context. All request and response operations are defined in this instance.

func NewContext

func NewContext(app *App, res http.ResponseWriter, req *http.Request) *Context

NewContext creates new context instance by app instance, http request and response.

func (*Context) App

func (ctx *Context) App() *App

App returns *App instance in this context.

func (*Context) Bool

func (ctx *Context) Bool(key string) bool

Bool returns input value of given key.

func (*Context) ContentType

func (ctx *Context) ContentType(contentType string)

ContentType sets content-type string.

func (*Context) Cookie

func (ctx *Context) Cookie(key string, value ...string) string

Cookie gets cookie value by given key when give only string. Cookie sets cookie value by given key, value and expire time string.

func (*Context) Do

func (ctx *Context) Do(e string, args ...interface{}) [][]interface{}

Do invokes event functions of name string in order of that they are be on. If args are less than function args, print error and return nil. If args are more than function args, ignore extra args. It returns [][]interface{} after invoked event function.

func (*Context) Download

func (ctx *Context) Download(file string)

Download sends file download response by file path.

func (*Context) End

func (ctx *Context) End()

End does end for this context. If context is end, handlers are stopped. If context response is not sent, send response.

func (*Context) Flash

func (ctx *Context) Flash(key string, v ...interface{}) interface{}

Flash sets values to this context or gets by key string. The flash items are alive in this context only.

func (*Context) Float

func (ctx *Context) Float(key string) float64

Float returns input value of given key.

func (*Context) FloatOr

func (ctx *Context) FloatOr(key string, def float64) float64

FloatOr returns input value of given key instead of def float if empty.

func (*Context) Func

func (ctx *Context) Func(name string, fn interface{})

Func adds template function to view. It will affect global *View instance.

func (*Context) GetHeader

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

GetHeader returns header string by given key.

func (*Context) Input

func (ctx *Context) Input() map[string]string

Input returns all input data map.

func (*Context) Int

func (ctx *Context) Int(key string) int

Int returns input value of given key.

func (*Context) IntOr

func (ctx *Context) IntOr(key string, def int) int

IntOr returns input value of given key instead of def int if empty.

func (*Context) Json

func (ctx *Context) Json(data interface{})

Json set json response with data and proper header.

func (*Context) Layout

func (ctx *Context) Layout(str string)

Layout sets layout string.

func (*Context) On

func (ctx *Context) On(e string, fn interface{})

On registers event function to event name string.

func (*Context) Param

func (ctx *Context) Param(key string) string

Param returns route param by key string which is defined in router pattern string.

func (*Context) Redirect

func (ctx *Context) Redirect(url string, status ...int)

Redirect does redirection response to url string and status int optional.

func (*Context) Render

func (ctx *Context) Render(tpl string, data map[string]interface{})

Render does template and layout rendering with data. The result bytes are assigned to context.Body. If error, panic.

func (*Context) Send

func (ctx *Context) Send()

Send does response sending. If response is sent, do not sent again.

func (*Context) String

func (ctx *Context) String(key string) string

String returns input value of given key.

func (*Context) StringOr

func (ctx *Context) StringOr(key string, def string) string

StringOr returns input value of given key instead of def string if empty.

func (*Context) Strings

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

Strings returns string slice of given key.

func (*Context) Throw

func (ctx *Context) Throw(status int, message ...interface{})

Throw throws http status error and error message. It call event named as status. The context will be end.

func (*Context) Tpl

func (ctx *Context) Tpl(tpl string, data map[string]interface{}) string

Tpl returns string of rendering template with data. If error, panic.

type Handler

type Handler func(context *Context)

Handler defines route handler, middleware handler type.

type Route

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

Route struct defines route pattern rule item.

type Router

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

Router instance provides router pattern and handlers.

func NewRouter

func NewRouter() *Router

NewRouter returns new router instance.

func (*Router) Delete

func (rt *Router) Delete(pattern string, fn ...Handler)

Delete registers DELETE handlers with pattern string.

func (*Router) Find

func (rt *Router) Find(url string, method string) (params map[string]string, fn []Handler)

Find does find matched rule and parse route url, returns route params and matched handlers.

func (*Router) Get

func (rt *Router) Get(pattern string, fn ...Handler)

Get registers GET handlers with pattern string.

func (*Router) Post

func (rt *Router) Post(pattern string, fn ...Handler)

Post registers POST handlers with pattern string.

func (*Router) Put

func (rt *Router) Put(pattern string, fn ...Handler)

Put registers PUT handlers with pattern string.

type View

type View struct {
	// template directory
	Dir string
	// view functions map
	FuncMap template.FuncMap
	// Cache Flag
	IsCache bool
	// contains filtered or unexported fields
}

View instance provides simple template render.

func NewView

func NewView(dir string) *View

NewView returns view instance with directory. It contains bundle template function HTML(convert string to template.HTML).

func (*View) Has

func (v *View) Has(tpl string) bool

Has checks the template file existing.

func (*View) NoCache

func (v *View) NoCache()

NoCache sets view cache off and clean cached data.

func (*View) Render

func (v *View) Render(tpl string, data map[string]interface{}) ([]byte, error)

Render renders template with data. Tpl is the file names under template directory, like tpl1,tpl2,tpl3.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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