2018-02-07 22:25:00 +00:00
|
|
|
package snap
|
|
|
|
|
2018-03-24 16:12:02 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-01-04 00:38:02 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2018-03-24 16:12:02 +00:00
|
|
|
"git.thirdmartini.com/pub/snap/auth"
|
|
|
|
)
|
2018-02-07 22:25:00 +00:00
|
|
|
|
|
|
|
type Context struct {
|
2019-01-04 00:38:02 +00:00
|
|
|
srv *Server
|
2018-03-24 16:12:02 +00:00
|
|
|
auth *auth.AuthData
|
|
|
|
|
2019-01-04 00:38:02 +00:00
|
|
|
w http.ResponseWriter
|
|
|
|
r *http.Request
|
2018-02-11 00:17:14 +00:00
|
|
|
vars map[string]string
|
2018-02-07 22:25:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 00:17:14 +00:00
|
|
|
type SnapContent struct {
|
2019-01-04 00:38:02 +00:00
|
|
|
User string
|
|
|
|
Theme string
|
2018-03-24 16:12:02 +00:00
|
|
|
Content interface{}
|
2018-02-11 00:17:14 +00:00
|
|
|
}
|
2018-02-07 22:25:00 +00:00
|
|
|
|
|
|
|
func (c *Context) GetRequest() *http.Request {
|
|
|
|
return c.r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Writer() http.ResponseWriter {
|
|
|
|
return c.w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) GetUser() string {
|
2018-03-24 16:12:02 +00:00
|
|
|
if c.auth == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.auth.User
|
2018-02-07 22:25:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 00:17:14 +00:00
|
|
|
func (c *Context) Error(code int, msg string) {
|
|
|
|
c.srv.renderError(c.w, code, msg)
|
|
|
|
}
|
|
|
|
|
2018-03-24 16:12:02 +00:00
|
|
|
func (c *Context) Reply(msg string) {
|
|
|
|
c.srv.reply(c.w, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) ReplyObject(obj interface{}) {
|
|
|
|
msg, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
c.Error(400, "Internal Server Error")
|
|
|
|
}
|
|
|
|
c.Reply(string(msg))
|
|
|
|
}
|
|
|
|
|
2019-01-04 00:38:02 +00:00
|
|
|
func (c *Context) GetObject(obj interface{}) error {
|
|
|
|
data, err := ioutil.ReadAll(c.r.Body)
|
|
|
|
defer c.r.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(data, obj)
|
|
|
|
}
|
2018-02-07 22:25:00 +00:00
|
|
|
|
|
|
|
func (c *Context) Render(tmpl string, content interface{}) {
|
|
|
|
c.srv.render(c.w, tmpl, content)
|
|
|
|
}
|
|
|
|
|
2018-02-11 00:17:14 +00:00
|
|
|
func (c *Context) RenderEx(tmpl string, content interface{}) {
|
2019-01-04 00:38:02 +00:00
|
|
|
cnt := SnapContent{
|
|
|
|
User: c.GetUser(),
|
|
|
|
Theme: c.srv.theme,
|
|
|
|
Content: content,
|
2018-02-11 00:17:14 +00:00
|
|
|
}
|
|
|
|
c.srv.render(c.w, tmpl, &cnt)
|
|
|
|
}
|
|
|
|
|
2019-01-04 00:38:02 +00:00
|
|
|
func (c *Context) GetVar(k string) (string, bool) {
|
|
|
|
v, ok := c.vars[k]
|
2018-02-11 00:17:14 +00:00
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2019-01-04 00:38:02 +00:00
|
|
|
func (c *Context) GetVarDefault(k string, def string) string {
|
|
|
|
v, ok := c.vars[k]
|
2018-02-11 00:17:14 +00:00
|
|
|
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)
|
2019-01-04 00:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) FormValueUint64(k string) uint64 {
|
|
|
|
str := c.r.FormValue(k)
|
|
|
|
|
|
|
|
val, err := strconv.ParseUint(str, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Redirect(url string) {
|
|
|
|
http.Redirect(c.w, c.r, url, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) QueryValue(key string) string {
|
|
|
|
return c.r.URL.Query().Get(key)
|
|
|
|
}
|
2019-10-20 14:01:21 +00:00
|
|
|
|
|
|
|
func (c *Context) QueryValueWithDefault(key string, def string) string {
|
|
|
|
val := c.r.URL.Query().Get(key)
|
|
|
|
if val == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|