Documentation ¶
Overview ¶
gopic web framework
Index ¶
- Constants
- type App
- func (app *App) Config() *Config
- func (app *App) Delete(key string, fn ...Handler)
- func (app *App) Get(key string, fn ...Handler) string
- func (app *App) NotFound(h Handler)
- func (app *App) Post(key string, fn ...Handler)
- func (app *App) Put(key string, fn ...Handler)
- func (app *App) Recover(h Handler)
- func (app *App) Route(method string, key string, fn ...Handler)
- func (app *App) Run()
- func (app *App) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (app *App) Set(key string, v interface{})
- func (app *App) Static(h Handler)
- func (app *App) Use(h ...Handler)
- func (app *App) View() *View
- type Config
- func (cfg *Config) Bool(key string) bool
- func (cfg *Config) Float(key string) float64
- func (cfg *Config) FloatOr(key string, def float64) float64
- func (cfg *Config) Int(key string) int
- func (cfg *Config) IntOr(key string, def int) int
- func (cfg *Config) Set(key string, value interface{})
- func (cfg *Config) String(key string) string
- func (cfg *Config) StringOr(key string, def string) string
- type Context
- func (ctx *Context) App() *App
- func (ctx *Context) Bool(key string) bool
- func (ctx *Context) ContentType(contentType string)
- func (ctx *Context) Cookie(key string, value ...string) string
- func (ctx *Context) Do(e string, args ...interface{}) [][]interface{}
- func (ctx *Context) Download(file string)
- func (ctx *Context) End()
- func (ctx *Context) Flash(key string, v ...interface{}) interface{}
- func (ctx *Context) Float(key string) float64
- func (ctx *Context) FloatOr(key string, def float64) float64
- func (ctx *Context) Func(name string, fn interface{})
- func (ctx *Context) GetHeader(key string) string
- func (ctx *Context) Input() map[string]string
- func (ctx *Context) Int(key string) int
- func (ctx *Context) IntOr(key string, def int) int
- func (ctx *Context) Json(data interface{})
- func (ctx *Context) Layout(str string)
- func (ctx *Context) On(e string, fn interface{})
- func (ctx *Context) Param(key string) string
- func (ctx *Context) Redirect(url string, status ...int)
- func (ctx *Context) Render(tpl string, data map[string]interface{})
- func (ctx *Context) Send()
- func (ctx *Context) String(key string) string
- func (ctx *Context) StringOr(key string, def string) string
- func (ctx *Context) Strings(key string) []string
- func (ctx *Context) Throw(status int, message ...interface{})
- func (ctx *Context) Tpl(tpl string, data map[string]interface{}) string
- type Handler
- type Route
- type Router
- func (rt *Router) Delete(pattern string, fn ...Handler)
- func (rt *Router) Find(url string, method string) (params map[string]string, fn []Handler)
- func (rt *Router) Get(pattern string, fn ...Handler)
- func (rt *Router) Post(pattern string, fn ...Handler)
- func (rt *Router) Put(pattern string, fn ...Handler)
- type View
Constants ¶
const ( CONTEXT_RENDERED = "context_rendered" CONTEXT_END = "context_end" CONTEXT_SEND = "context_send" )
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 NewApp ¶
New creates an App instance. It loads config.json file if exist. Otherwise, set default config values to Config.
func (*App) Get ¶
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 ¶
Register NotFound handler. It's invoked after calling route handler but not matched.
func (*App) Recover ¶
Register panic recover handler. It's invoked when panic error in middleware and route handlers.
func (*App) Route ¶
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) Static ¶
Register static file handler. It's invoked before route handler after middleware handler.
type Config ¶
Config instance. It's a two-level map.
func (*Config) FloatOr ¶
FloatOr returns config float64 by given string. It returns def float64 if float 0.
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 ¶
NewContext creates new context instance by app instance, http request and response.
func (*Context) ContentType ¶
ContentType sets content-type string.
func (*Context) Cookie ¶
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 ¶
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) 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 ¶
Flash sets values to this context or gets by key string. The flash items are alive in this context only.
func (*Context) Json ¶
func (ctx *Context) Json(data interface{})
Json set json response with data and proper header.
func (*Context) Param ¶
Param returns route param by key string which is defined in router pattern string.
func (*Context) Redirect ¶
Redirect does redirection response to url string and status int optional.
func (*Context) Render ¶
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) StringOr ¶
StringOr returns input value of given key instead of def string if empty.
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 (*Router) Find ¶
Find does find matched rule and parse route url, returns route params and matched handlers.
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.