page.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package web
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "mime/multipart"
  8. "net/http"
  9. "strings"
  10. h "github.com/theplant/htmlgo"
  11. )
  12. var Default = New()
  13. func Page(pf PageFunc, efs ...interface{}) (p *PageBuilder) {
  14. p = &PageBuilder{
  15. b: Default,
  16. }
  17. p.pageRenderFunc = pf
  18. p.RegisterEventFunc("__reload__", reload)
  19. p.EventFuncs(efs...)
  20. return
  21. }
  22. type PageBuilder struct {
  23. EventsHub
  24. b *Builder
  25. pageRenderFunc PageFunc
  26. maxFormSize int64
  27. }
  28. func (b *Builder) Page(pf PageFunc) (p *PageBuilder) {
  29. p = Page(pf).Builder(b)
  30. return
  31. }
  32. func (p *PageBuilder) Builder(v *Builder) (r *PageBuilder) {
  33. p.b = v
  34. r = p
  35. return
  36. }
  37. func (p *PageBuilder) Wrap(middlewares ...func(in PageFunc) PageFunc) (r *PageBuilder) {
  38. pf := p.pageRenderFunc
  39. for _, m := range middlewares {
  40. pf = m(pf)
  41. }
  42. p.pageRenderFunc = pf
  43. r = p
  44. return
  45. }
  46. func (p *PageBuilder) MaxFormSize(v int64) (r *PageBuilder) {
  47. p.maxFormSize = v
  48. r = p
  49. return
  50. }
  51. func (p *PageBuilder) EventFuncs(vs ...interface{}) (r *PageBuilder) {
  52. p.addMultipleEventFuncs(vs...)
  53. return p
  54. }
  55. func (p *PageBuilder) EventFunc(name string, ef EventFunc) (r *PageBuilder) {
  56. p.RegisterEventFunc(name, ef)
  57. return p
  58. }
  59. func (p *PageBuilder) MergeHub(hub *EventsHub) (r *PageBuilder) {
  60. p.EventsHub.eventFuncs = append(hub.eventFuncs, p.EventsHub.eventFuncs...)
  61. return p
  62. }
  63. func (p *PageBuilder) render(
  64. w http.ResponseWriter,
  65. r *http.Request,
  66. c context.Context,
  67. head *PageInjector,
  68. ) (pager *PageResponse, body string) {
  69. if p.pageRenderFunc == nil {
  70. return
  71. }
  72. ctx := MustGetEventContext(c)
  73. ctx.R = r
  74. ctx.W = w
  75. ctx.Injector = head
  76. pr, err := p.pageRenderFunc(ctx)
  77. if err != nil {
  78. panic(err)
  79. }
  80. pager = &pr
  81. if pager.Body == nil {
  82. panic("page's RenderFunc returns nil body, use pr.Body = root to set it")
  83. }
  84. // fmt.Println("eventFuncs count: ", len(p.eventFuncs))
  85. b, err := pager.Body.MarshalHTML(c)
  86. if err != nil {
  87. panic(err)
  88. }
  89. body = string(b)
  90. return
  91. }
  92. func (p *PageBuilder) index(w http.ResponseWriter, r *http.Request) {
  93. var err error
  94. var head = &PageInjector{}
  95. ctx := new(EventContext)
  96. c := WrapEventContext(r.Context(), ctx)
  97. pr, body := p.render(w, r, c, head)
  98. if len(pr.PageTitle) > 0 {
  99. head.Title(pr.PageTitle)
  100. }
  101. var resp string
  102. resp, err = p.b.layoutFunc(r, head, body)
  103. if err != nil {
  104. panic(err)
  105. }
  106. _, err = fmt.Fprintln(w, resp)
  107. if err != nil {
  108. panic(err)
  109. }
  110. }
  111. func (p *PageBuilder) parseForm(r *http.Request) *multipart.Form {
  112. maxSize := p.maxFormSize
  113. if maxSize == 0 {
  114. maxSize = 128 << 20 // 128MB
  115. }
  116. err := r.ParseMultipartForm(maxSize)
  117. if err != nil {
  118. panic(err)
  119. }
  120. return r.MultipartForm
  121. }
  122. const EventFuncIDName = "__execute_event__"
  123. func (p *PageBuilder) executeEvent(w http.ResponseWriter, r *http.Request) {
  124. ctx := new(EventContext)
  125. ctx.R = r
  126. ctx.W = w
  127. ctx.Injector = &PageInjector{}
  128. c := WrapEventContext(r.Context(), ctx)
  129. eventFuncID := r.FormValue(EventFuncIDName)
  130. // for server side restart and lost all the eventFuncs,
  131. // but user keep clicking page without refresh page to call p.render to fill up eventFuncs
  132. // because default added reload
  133. if len(p.eventFuncs) <= 1 &&
  134. p.eventFuncById(eventFuncID) == nil &&
  135. p.b.eventFuncById(eventFuncID) == nil {
  136. log.Println("Re-render because event funcs gone, might server restarted")
  137. head := &PageInjector{}
  138. p.render(w, r, c, head)
  139. }
  140. ef := p.eventFuncById(eventFuncID)
  141. if ef == nil {
  142. ef = p.b.eventFuncById(eventFuncID)
  143. }
  144. if ef == nil {
  145. log.Printf("event %s not found\n", eventFuncID)
  146. http.NotFound(w, r)
  147. return
  148. }
  149. er, err := ef(ctx)
  150. if err != nil {
  151. panic(err)
  152. }
  153. if er.Reload {
  154. head := &PageInjector{}
  155. pr, body := p.render(w, r, c, head)
  156. er.Body = h.RawHTML(body)
  157. if len(er.PageTitle) == 0 {
  158. er.PageTitle = pr.PageTitle
  159. }
  160. }
  161. er.Body = h.RawHTML(h.MustString(er.Body, c))
  162. for _, up := range er.UpdatePortals {
  163. up.Body = h.RawHTML(h.MustString(up.Body, c))
  164. }
  165. err = json.NewEncoder(w).Encode(er)
  166. if err != nil {
  167. panic(err)
  168. }
  169. }
  170. func reload(ctx *EventContext) (r EventResponse, err error) {
  171. r.Reload = true
  172. return
  173. }
  174. func (p *PageBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  175. if strings.Index(r.URL.String(), EventFuncIDName) >= 0 {
  176. p.executeEvent(w, r)
  177. return
  178. }
  179. p.index(w, r)
  180. }