page.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. head.setDefault(pr.PageTitle)
  99. var resp string
  100. resp, err = p.b.layoutFunc(r, head, body)
  101. if err != nil {
  102. panic(err)
  103. }
  104. _, err = fmt.Fprintln(w, resp)
  105. if err != nil {
  106. panic(err)
  107. }
  108. }
  109. func (p *PageBuilder) parseForm(r *http.Request) *multipart.Form {
  110. maxSize := p.maxFormSize
  111. if maxSize == 0 {
  112. maxSize = 128 << 20 // 128MB
  113. }
  114. err := r.ParseMultipartForm(maxSize)
  115. if err != nil {
  116. panic(err)
  117. }
  118. return r.MultipartForm
  119. }
  120. const EventFuncIDName = "__execute_event__"
  121. func (p *PageBuilder) executeEvent(w http.ResponseWriter, r *http.Request) {
  122. ctx := new(EventContext)
  123. ctx.R = r
  124. ctx.W = w
  125. ctx.Injector = &PageInjector{}
  126. c := WrapEventContext(r.Context(), ctx)
  127. eventFuncID := r.FormValue(EventFuncIDName)
  128. // for server side restart and lost all the eventFuncs,
  129. // but user keep clicking page without refresh page to call p.render to fill up eventFuncs
  130. // because default added reload
  131. if len(p.eventFuncs) <= 1 &&
  132. p.eventFuncById(eventFuncID) == nil &&
  133. p.b.eventFuncById(eventFuncID) == nil {
  134. log.Println("Re-render because event funcs gone, might server restarted")
  135. head := &PageInjector{}
  136. p.render(w, r, c, head)
  137. }
  138. ef := p.eventFuncById(eventFuncID)
  139. if ef == nil {
  140. ef = p.b.eventFuncById(eventFuncID)
  141. }
  142. if ef == nil {
  143. log.Printf("event %s not found\n", eventFuncID)
  144. http.NotFound(w, r)
  145. return
  146. }
  147. er, err := ef(ctx)
  148. if err != nil {
  149. panic(err)
  150. }
  151. if er.Reload {
  152. head := &PageInjector{}
  153. pr, body := p.render(w, r, c, head)
  154. er.Body = h.RawHTML(body)
  155. if len(er.PageTitle) == 0 {
  156. er.PageTitle = pr.PageTitle
  157. }
  158. }
  159. er.Body = h.RawHTML(h.MustString(er.Body, c))
  160. for _, up := range er.UpdatePortals {
  161. up.Body = h.RawHTML(h.MustString(up.Body, c))
  162. }
  163. err = json.NewEncoder(w).Encode(er)
  164. if err != nil {
  165. panic(err)
  166. }
  167. }
  168. func reload(ctx *EventContext) (r EventResponse, err error) {
  169. r.Reload = true
  170. return
  171. }
  172. func (p *PageBuilder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  173. if strings.Index(r.URL.String(), EventFuncIDName) >= 0 {
  174. p.executeEvent(w, r)
  175. return
  176. }
  177. p.index(w, r)
  178. }