Documentation ¶
Overview ¶
Package app provides a full featured framework for any web app.
Example ¶
package main import ( "log" "net/http" "github.com/gowww/app" "github.com/gowww/i18n" "golang.org/x/text/language" ) func main() { var locales = i18n.Locales{ language.English: { "hello": "Hello!", }, language.French: { "hello": "Bonjour !", }, } app.Localize(locales, language.English) app.Get("/", func(c *app.Context) { c.View("home") }) app.Post("/user/:id/files/", func(c *app.Context) { c.Status(http.StatusCreated) c.JSON(map[string]interface{}{ "id": c.PathValue("id"), "filepath": c.PathValue("*"), }) }) v1 := app.Group("/v1") { v1.Get("/user", func(c *app.Context) { c.Text("User for V1") }) v1.Get("/item", func(c *app.Context) { c.Text("Item for V1") }) } v2 := app.Group("/v2") { v2.Get("/user", func(c *app.Context) { c.Text("User for V2") }) v2.Get("/item", func(c *app.Context) { c.Text("Item for V2") }) } if !app.EnvProduction() { log.Printf("Developing app on %s\n", app.Address()) } app.Run() }
Output:
Index ¶
- func Address() string
- func Delete(path string, handler Handler, middlewares ...Middleware)
- func Encrypter() crypto.Encrypter
- func EnvProduction() bool
- func Error(handler Handler)
- func Get(path string, handler Handler, middlewares ...Middleware)
- func GlobalViewData(data ViewData)
- func GlobalViewFuncs(funcs ViewFuncs)
- func Localize(locs i18n.Locales, fallback language.Tag, parsers ...i18n.Parser)
- func NotFound(handler Handler)
- func Patch(path string, handler Handler, middlewares ...Middleware)
- func Post(path string, handler Handler, middlewares ...Middleware)
- func Put(path string, handler Handler, middlewares ...Middleware)
- func Route(method, path string, handler Handler, middlewares ...Middleware)
- func Run(mm ...Middleware)
- func Secret(key string)
- func Secure(o *secure.Options)
- type Context
- func (c *Context) BadRequest(checker check.Checker, view string, data ...ViewData) bool
- func (c *Context) Bytes(b []byte)
- func (c *Context) Check(checker check.Checker) check.Errors
- func (c *Context) Cookie(name string) string
- func (c *Context) DeleteCookie(name string)
- func (c *Context) Error() error
- func (c *Context) FmtNumber(n interface{}) string
- func (c *Context) FormValue(key string) string
- func (c *Context) Get(key interface{}) interface{}
- func (c *Context) HasFormValue(key string) bool
- func (c *Context) JSON(v interface{})
- func (c *Context) Locale() language.Tag
- func (c *Context) Log(msg string)
- func (c *Context) NotFound()
- func (c *Context) Panic(err error)
- func (c *Context) PathValue(key string) string
- func (c *Context) Push(target string, opts *http.PushOptions)
- func (c *Context) Redirect(url string, status int)
- func (c *Context) Set(key, val interface{})
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) Status(code int) *Context
- func (c *Context) T(key string, a ...interface{}) string
- func (c *Context) TErrors(errs check.Errors) check.TranslatedErrors
- func (c *Context) THTML(key string, a ...interface{}) template.HTML
- func (c *Context) Text(s string)
- func (c *Context) Textf(s string, a ...interface{})
- func (c *Context) Tn(key string, n int, args ...interface{}) string
- func (c *Context) TnHTML(key string, n int, args ...interface{}) template.HTML
- func (c *Context) View(name string, data ...ViewData)
- func (c *Context) Write(b []byte) (int, error)
- type Handler
- type Middleware
- type RouterGroup
- func (rg *RouterGroup) Delete(path string, handler Handler, middlewares ...Middleware)
- func (rg *RouterGroup) Get(path string, handler Handler, middlewares ...Middleware)
- func (rg *RouterGroup) Group(path string, middlewares ...Middleware) *RouterGroup
- func (rg *RouterGroup) Patch(path string, handler Handler, middlewares ...Middleware)
- func (rg *RouterGroup) Post(path string, handler Handler, middlewares ...Middleware)
- func (rg *RouterGroup) Put(path string, handler Handler, middlewares ...Middleware)
- func (rg *RouterGroup) Route(method, path string, handler Handler, middlewares ...Middleware)
- type ViewData
- type ViewFuncs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Address ¶
func Address() string
Address gives the address on which the app is running. It ensures that flags are parsed so don't use this function before setting your own flags with gowww/cli or they will be ignored.
func Delete ¶
func Delete(path string, handler Handler, middlewares ...Middleware)
Delete makes a route for DELETE method.
func EnvProduction ¶
func EnvProduction() bool
EnvProduction tells if the app is run with the production flag. It ensures that flags are parsed so don't use this function before setting your own flags with gowww/cli or they will be ignored.
func Error ¶
func Error(handler Handler)
Error registers the "internal error" handler.
Using Context.Error, you can retrieve the error value stored in request's context during recovering.
func Get ¶
func Get(path string, handler Handler, middlewares ...Middleware)
Get makes a route for GET method.
func GlobalViewData ¶
func GlobalViewData(data ViewData)
GlobalViewData adds global data for view templates.
func GlobalViewFuncs ¶
func GlobalViewFuncs(funcs ViewFuncs)
GlobalViewFuncs adds functions for view templates.
func Localize ¶
Localize sets app locales with fallback and client locale parsers. Order is mandatory and defaults are: ParseCookie, ParseFormValue, ParseAcceptLanguage.
func Patch ¶
func Patch(path string, handler Handler, middlewares ...Middleware)
Patch makes a route for PATCH method.
func Post ¶
func Post(path string, handler Handler, middlewares ...Middleware)
Post makes a route for POST method.
func Put ¶
func Put(path string, handler Handler, middlewares ...Middleware)
Put makes a route for PUT method.
func Route ¶
func Route(method, path string, handler Handler, middlewares ...Middleware)
Route makes a route for method and path.
func Run ¶
func Run(mm ...Middleware)
Run ensures that flags are parsed, sets the middlewares and starts the server.
Types ¶
type Context ¶
type Context struct { Res http.ResponseWriter Req *http.Request }
A Context contains the data for a handler.
func (*Context) BadRequest ¶
BadRequest uses a check.Checker to validate request form data, and a view name to execute on fail. If you don't provide a view name (empty string), the response will be a JSON errors map.
If the check fails, it sets the status to "400 Bad Request" and returns true, allowing you to exit from the handler.
Example ¶
package main import ( "net/http" "github.com/gowww/app" "github.com/gowww/check" ) func main() { userChecker := check.Checker{ "email": {check.Required, check.Email}, "phone": {check.Phone}, } app.Post("/users", func(c *app.Context) { if c.BadRequest(userChecker, "") { return } c.Status(http.StatusCreated) }) }
Output:
func (*Context) Check ¶
Check uses a check.Checker to validate request's data and always returns the non-nil errors map.
func (*Context) Cookie ¶
Cookie returns the value of the named cookie. If multiple cookies match the given name, only one cookie value will be returned. If the secret key is set for app, value will be decrypted before returning. If cookie is not found or the decryption fails, an empty string is returned.
func (*Context) DeleteCookie ¶
DeleteCookie removes a cookie from the client.
func (*Context) Error ¶
Error returns the error value stored in request's context after a recovering or a Context.Error call.
func (*Context) Get ¶
func (c *Context) Get(key interface{}) interface{}
Get returns a context value.
func (*Context) HasFormValue ¶
HasFormValue checks if the form value exists in the request.
func (*Context) JSON ¶
func (c *Context) JSON(v interface{})
JSON writes the response with a marshalled JSON. If v has a JSON() interface{} method, it will be used.
func (*Context) NotFound ¶
func (c *Context) NotFound()
NotFound responds with the "not found" handler.
func (*Context) Panic ¶
Panic logs error with stack trace and responds with the error handler if set.
func (*Context) Push ¶
func (c *Context) Push(target string, opts *http.PushOptions)
Push initiates an HTTP/2 server push if supported. See net/http.Pusher for documentation.
func (*Context) SetCookie ¶
SetCookie sets a cookie to the response. If the secret key is set for app, value will be encrypted. If the app is not in a production environment, the "secure" flag will be set to false.
func (*Context) TErrors ¶
func (c *Context) TErrors(errs check.Errors) check.TranslatedErrors
TErrors returns translated checking errors.
func (*Context) THTML ¶
THTML works like T but returns an HTML unescaped translation. An "nl2br" function is applied to the result.
func (*Context) Tn ¶
Tn returns the translation associated to key, for the client locale. If the translation defines plural forms (zero, one, other), it uses the most appropriate. All i18n.TnPlaceholder in the translation are replaced with number n. If translation is not found, an empty string is returned.
func (*Context) TnHTML ¶
TnHTML works like Tn but returns an HTML unescaped translation. An "nl2br" function is applied to the result.
type Middleware ¶
A Middleware is a handler that wraps another one.
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
RouterGroup contains the first path part for a routes group.
func Group ¶
func Group(path string, middlewares ...Middleware) *RouterGroup
Group initiates a routing group. All subroutes paths will be prefixed with the group path.
func (*RouterGroup) Delete ¶
func (rg *RouterGroup) Delete(path string, handler Handler, middlewares ...Middleware)
Delete makes a route for DELETE method.
func (*RouterGroup) Get ¶
func (rg *RouterGroup) Get(path string, handler Handler, middlewares ...Middleware)
Get makes a route for GET method.
func (*RouterGroup) Group ¶
func (rg *RouterGroup) Group(path string, middlewares ...Middleware) *RouterGroup
Group contains the first path part for a routes subgroup.
func (*RouterGroup) Patch ¶
func (rg *RouterGroup) Patch(path string, handler Handler, middlewares ...Middleware)
Patch makes a route for PATCH method.
func (*RouterGroup) Post ¶
func (rg *RouterGroup) Post(path string, handler Handler, middlewares ...Middleware)
Post makes a route for POST method.
func (*RouterGroup) Put ¶
func (rg *RouterGroup) Put(path string, handler Handler, middlewares ...Middleware)
Put makes a route for PUT method.
func (*RouterGroup) Route ¶
func (rg *RouterGroup) Route(method, path string, handler Handler, middlewares ...Middleware)
Route makes a route for method and path.