routes.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, prefix string) *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, prefix))
  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, ms)) // 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. prefix := dic.MustParam(services.PPrefix).(string)
  52. storeSecret := dic.MustParam(services.PStoreSecret).([]byte)
  53. rd := dic.MustService(services.SvcRedriver).(redriver.Redriver)
  54. re := dic.MustService(services.SvcRenderer).(*template.Template)
  55. ms := dic.MustService(services.SvcMessageStore).(redriver.MessageStore)
  56. return SetupRoutes(rd, ms, re, storeSecret, csrfSecret, prefix), nil
  57. }
  58. func RendererService(_ *izidic.Container) (any, error) {
  59. var err error
  60. renderer := template.New("redriver").Funcs(template.FuncMap{
  61. "dump": func(args ...any) template.HTML {
  62. return "<pre>" + template.HTML(spew.Sdump(args...)) + "</pre>\n"
  63. },
  64. "timestamp": func(ts int64) time.Time {
  65. return time.Unix(ts, 0)
  66. },
  67. "nameFromARN": redriver.NameFromARNString,
  68. }).Funcs(sprig.FuncMap())
  69. for _, tpl := range []struct {
  70. name string
  71. value string
  72. }{
  73. {"confirm", front.Confirm},
  74. {"flashes", front.Flashes},
  75. {"home", front.Home},
  76. {"queue-get", front.QueueGet}, // Includes queue-item.
  77. {"500", front.Err500},
  78. } {
  79. renderer, err = renderer.New(tpl.name).Parse(tpl.value)
  80. if err != nil {
  81. return nil, fmt.Errorf("failed parsing %q template: %w", tpl.name, err)
  82. }
  83. }
  84. return renderer, nil
  85. }
  86. // PrefixFileSystem converts a http.FileSystem by serving it requests prefixed
  87. // by the passed prefix, allowing rooted static directories matching the name
  88. // of an embed.FS.
  89. func PrefixFileSystem(prefix string, ifs http.FileSystem) http.FileSystem {
  90. return &prefixedFS{
  91. prefix: prefix,
  92. ifs: ifs,
  93. }
  94. }
  95. type prefixedFS struct {
  96. prefix string
  97. ifs http.FileSystem
  98. }
  99. // Open implements http.FileSystem
  100. func (pfs *prefixedFS) Open(name string) (http.File, error) {
  101. return pfs.ifs.Open(filepath.Join(pfs.prefix, name))
  102. }