context.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package snap
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "git.thirdmartini.com/pub/snap/auth"
  10. )
  11. type Context struct {
  12. srv *Server
  13. auth *auth.AuthData
  14. w http.ResponseWriter
  15. r *http.Request
  16. vars map[string]string
  17. }
  18. type SnapContent struct {
  19. User string
  20. UserProperties map[string]string
  21. Theme string
  22. Meta map[string]string
  23. Content interface{}
  24. }
  25. func (c *Context) GetRequest() *http.Request {
  26. return c.r
  27. }
  28. func (c *Context) Writer() http.ResponseWriter {
  29. return c.w
  30. }
  31. func (c *Context) GetUser() string {
  32. if c.auth == nil {
  33. return ""
  34. }
  35. return c.auth.User
  36. }
  37. func (c *Context) Error(code int, msg string) {
  38. c.srv.renderError(c.w, code, msg)
  39. }
  40. func (c *Context) ErrorObject(code int, obj interface{}) {
  41. msg, err := json.Marshal(obj)
  42. if err != nil {
  43. c.Error(http.StatusInternalServerError, "Internal Server Error")
  44. return
  45. }
  46. c.Error(code, string(msg))
  47. }
  48. func (c *Context) Reply(msg string) {
  49. c.srv.reply(c.w, msg)
  50. }
  51. func (c *Context) ReplyWithHeaders(msg string, kv map[string]string) {
  52. h := c.w.Header()
  53. for k, v := range kv {
  54. h.Add(k, v)
  55. }
  56. c.srv.reply(c.w, msg)
  57. }
  58. func (c *Context) ReplyObject(obj interface{}) {
  59. msg, err := json.Marshal(obj)
  60. if err != nil {
  61. c.Error(http.StatusInternalServerError, "Internal Server Error")
  62. return
  63. }
  64. c.Reply(string(msg))
  65. }
  66. func (c *Context) GetObject(obj interface{}) error {
  67. data, err := ioutil.ReadAll(c.r.Body)
  68. defer c.r.Body.Close()
  69. if err != nil {
  70. return err
  71. }
  72. return json.Unmarshal(data, obj)
  73. }
  74. func (c *Context) Render(tmpl string, content interface{}) {
  75. c.srv.render(c.w, tmpl, content)
  76. }
  77. func (c *Context) RenderEx(tmpl string, content interface{}) {
  78. cnt := SnapContent{
  79. User: c.GetUser(),
  80. Theme: c.srv.theme,
  81. Meta: c.srv.meta,
  82. Content: content,
  83. }
  84. if c.auth != nil {
  85. fmt.Printf("Properties: %+v\n", c.auth.Properties)
  86. cnt.UserProperties = c.auth.Properties
  87. }
  88. c.srv.render(c.w, tmpl, &cnt)
  89. }
  90. func (c *Context) RenderWithMeta(tmpl string, meta map[string]string, content interface{}) {
  91. // Merge metas
  92. if meta == nil {
  93. meta = make(map[string]string)
  94. }
  95. for k, v := range c.srv.meta {
  96. if _, ok := meta[k]; !ok {
  97. meta[k] = v
  98. }
  99. }
  100. cnt := SnapContent{
  101. User: c.GetUser(),
  102. Theme: c.srv.theme,
  103. Meta: meta,
  104. Content: content,
  105. }
  106. if c.auth != nil {
  107. fmt.Printf("Properties: %+v\n", c.auth.Properties)
  108. cnt.UserProperties = c.auth.Properties
  109. }
  110. c.srv.render(c.w, tmpl, &cnt)
  111. }
  112. func (c *Context) GetVar(k string) (string, bool) {
  113. v, ok := c.vars[k]
  114. return v, ok
  115. }
  116. func (c *Context) GetVarDefault(k string, def string) string {
  117. v, ok := c.vars[k]
  118. if !ok {
  119. return def
  120. }
  121. return v
  122. }
  123. func (c *Context) GetVarAsSlice(k string, delim string, def []string) []string {
  124. v, ok := c.vars[k]
  125. if !ok {
  126. return def
  127. }
  128. r := strings.Split(v, delim)
  129. if len(r) == 1 && r[0] == "" {
  130. return def
  131. }
  132. return r
  133. }
  134. func (c *Context) ParseForm() error {
  135. return c.r.ParseForm()
  136. }
  137. func (c *Context) FormValue(k string) string {
  138. return c.r.FormValue(k)
  139. }
  140. func (c *Context) FormValueUint64(k string) uint64 {
  141. str := c.r.FormValue(k)
  142. val, err := strconv.ParseUint(str, 10, 64)
  143. if err != nil {
  144. return 0
  145. }
  146. return val
  147. }
  148. func (c *Context) FormValueAsSlice(k string, delim string) []string {
  149. v := c.r.FormValue(k)
  150. r := strings.Split(v, delim)
  151. if len(r) == 1 && r[0] == "" {
  152. return []string{}
  153. }
  154. return r
  155. }
  156. func (c *Context) Redirect(url string) {
  157. http.Redirect(c.w, c.r, url, http.StatusSeeOther)
  158. }
  159. func (c *Context) QueryValue(key string) string {
  160. return c.r.URL.Query().Get(key)
  161. }
  162. func (c *Context) QueryHas(key string) bool {
  163. _, ok := c.r.URL.Query()[key]
  164. return ok
  165. }
  166. func (c *Context) QueryValueWithDefault(key string, def string) string {
  167. val := c.r.URL.Query().Get(key)
  168. if val == "" {
  169. return def
  170. }
  171. return val
  172. }