routes.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package web
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "html/template"
  6. "net/http"
  7. "path/filepath"
  8. "time"
  9. "github.com/Masterminds/sprig/v3"
  10. "github.com/davecgh/go-spew/spew"
  11. "github.com/fgm/izidic"
  12. "github.com/gin-contrib/sessions"
  13. "github.com/gin-contrib/sessions/cookie"
  14. "github.com/gin-gonic/gin"
  15. "github.com/utrack/gin-csrf"
  16. "code.osinet.fr/fgm/sqs_demo/back/services"
  17. "code.osinet.fr/fgm/sqs_demo/back/services/redriver"
  18. "code.osinet.fr/fgm/sqs_demo/front"
  19. )
  20. func SetupRoutes(rd redriver.Redriver, ms redriver.MessageStore, renderer *template.Template, storeSecret, csrfSecret []byte) *gin.Engine {
  21. const assetsPrefix = "/assets/"
  22. r := gin.Default()
  23. r.SetHTMLTemplate(renderer)
  24. _ = r.SetTrustedProxies(nil)
  25. store := cookie.NewStore(storeSecret)
  26. r.Use(sessions.Sessions("defaultsession", store))
  27. mw := csrf.Middleware(csrf.Options{
  28. Secret: string(csrfSecret),
  29. ErrorFunc: func(c *gin.Context) {
  30. c.String(http.StatusBadRequest, "CSRF token does not match")
  31. c.Abort()
  32. },
  33. TokenGetter: nil,
  34. })
  35. // Done
  36. r.StaticFS(assetsPrefix, PrefixFileSystem(assetsPrefix, http.FS(front.Assets)))
  37. r.GET("/queue", gin.WrapH(http.RedirectHandler("/", http.StatusMovedPermanently)))
  38. r.GET("/queue/:name/confirm", mw, makeConfirmHandler()) // Needs mw to set token.
  39. // Back done, front WIP
  40. r.GET("/queue/:name", makeQueueHandler(rd, ms))
  41. // JSON done
  42. r.GET("/", makeHomeHandler(rd))
  43. // TODO
  44. r.POST("/queue/:name/delete", mw, makeDeleteHandler(rd)) // Needs mw to check token.
  45. r.POST("/queue/:name/purge", mw, makePurgeHandler(rd)) // Needs mw to check token.
  46. r.POST("/queue/:name/redrive", mw, makeRedriveHandler(rd)) // Needs mw to check token.
  47. return r
  48. }
  49. func HttpService(dic *izidic.Container) (any, error) {
  50. csrfSecret := dic.MustParam(services.PCSRFSecret).([]byte)
  51. storeSecret := dic.MustParam(services.PStoreSecret).([]byte)
  52. rd := dic.MustService(services.SvcRedriver).(redriver.Redriver)
  53. re := dic.MustService(services.SvcRenderer).(*template.Template)
  54. ms := dic.MustService(services.SvcMessageStore).(redriver.MessageStore)
  55. return SetupRoutes(rd, ms, re, storeSecret, csrfSecret), nil
  56. }
  57. func RendererService(_ *izidic.Container) (any, error) {
  58. var err error
  59. renderer := template.New("redriver").Funcs(template.FuncMap{
  60. "dump": func(args ...any) template.HTML {
  61. return "<pre>" + template.HTML(spew.Sdump(args...)) + "</pre>\n"
  62. },
  63. "timestamp": func(ts int64) time.Time {
  64. return time.Unix(ts, 0)
  65. },
  66. "nameFromARN": redriver.NameFromARNString,
  67. }).Funcs(sprig.FuncMap())
  68. for _, tpl := range []struct {
  69. name string
  70. value string
  71. }{
  72. {"confirm", front.Confirm},
  73. {"flashes", front.Flashes},
  74. {"home", front.Home},
  75. {"queue-get", front.QueueGet}, // Includes queue-item.
  76. {"500", front.Err500},
  77. } {
  78. renderer, err = renderer.New(tpl.name).Parse(tpl.value)
  79. if err != nil {
  80. return nil, fmt.Errorf("failed parsing %q template: %w", tpl.name, err)
  81. }
  82. }
  83. return renderer, nil
  84. }
  85. // PrefixFileSystem converts a http.FileSystem by serving it requests prefixed
  86. // by the passed prefix, allowing rooted static directories matching the name
  87. // of an embed.FS.
  88. func PrefixFileSystem(prefix string, ifs http.FileSystem) http.FileSystem {
  89. return &prefixedFS{
  90. prefix: prefix,
  91. ifs: ifs,
  92. }
  93. }
  94. type prefixedFS struct {
  95. prefix string
  96. ifs http.FileSystem
  97. }
  98. // Open implements http.FileSystem
  99. func (pfs *prefixedFS) Open(name string) (http.File, error) {
  100. return pfs.ifs.Open(filepath.Join(pfs.prefix, name))
  101. }