snap/context.go

70 lines
1.1 KiB
Go

package snap
import "net/http"
type Context struct {
Username string
srv *server
w http.ResponseWriter
r *http.Request
vars map[string]string
}
type SnapContent struct {
Username string
AppData interface{}
}
func (c *Context) GetRequest() *http.Request {
return c.r
}
func (c *Context) Writer() http.ResponseWriter {
return c.w
}
func (c *Context) GetUser() string {
return c.Username
}
func (c *Context) Error(code int, msg string) {
c.srv.renderError(c.w, code, msg)
}
func (c *Context) Render(tmpl string, content interface{}) {
c.srv.render(c.w, tmpl, content)
}
func (c *Context) RenderEx(tmpl string, content interface{}) {
cnt := SnapContent {
Username:c.Username,
AppData:content,
}
c.srv.render(c.w, tmpl, &cnt)
}
func (c *Context) GetVar(k string) ( string, bool) {
v,ok := c.vars[k]
return v, ok
}
func (c *Context) GetVarDefault(k string,def string) (string) {
v,ok := c.vars[k]
if !ok {
return def
}
return v
}
func (c *Context) ParseForm() error {
return c.r.ParseForm()
}
func (c *Context) FormValue(k string) string {
return c.r.FormValue(k)
}