cdn_handlers.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package controller
  2. import (
  3. "fmt"
  4. "os"
  5. "git.aetherial.dev/aeth/keiji/pkg/helpers"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // @Name ServeCss
  9. // @Summary serves css files from the web root directory
  10. // @Tags cdn
  11. // @Param file path string true "The CSS file to serve to the client"
  12. // @Router /api/v1/style/{file} [get]
  13. func (c *Controller) ServeCss(ctx *gin.Context) {
  14. f, exist := ctx.Params.Get("file")
  15. if !exist {
  16. ctx.JSON(404, map[string]string{
  17. "Error": "the requested file could not be found",
  18. })
  19. }
  20. css := fmt.Sprintf("%s/css/bootstrap-5.0.2/dist/css/%s", c.WebRoot, f)
  21. b, err := os.ReadFile(css)
  22. if err != nil {
  23. ctx.JSON(500, map[string]string{
  24. "Error": "Could not serve the requested file",
  25. "msg": err.Error(),
  26. })
  27. }
  28. ctx.Data(200, "text/css", b)
  29. }
  30. // @Name ServeJs
  31. // @Summary serves js files from the web root directory
  32. // @Tags cdn
  33. // @Param file path string true "The Javascript file to serve to the client"
  34. // @Router /api/v1/js/{file} [get]
  35. func (c *Controller) ServeJs(ctx *gin.Context) {
  36. f, exist := ctx.Params.Get("file")
  37. if !exist {
  38. ctx.JSON(404, map[string]string{
  39. "Error": "the requested file could not be found",
  40. })
  41. }
  42. css := fmt.Sprintf("%s/css/bootstrap-5.0.2/dist/js/%s", c.WebRoot, f)
  43. b, err := os.ReadFile(css)
  44. if err != nil {
  45. ctx.JSON(500, map[string]string{
  46. "Error": "Could not serve the requested file",
  47. "msg": err.Error(),
  48. })
  49. }
  50. ctx.Data(200, "text/javascript", b)
  51. }
  52. // @Name ServeMdbCss
  53. // @Summary serves some mdb assets
  54. // @Tags cdn
  55. // @Param file path string true "The CSS file to serve to the client"
  56. // @Router /api/v1/style/mdb/{file} [get]
  57. func (c *Controller) ServeMdbCss(ctx *gin.Context) {
  58. f, exist := ctx.Params.Get("file")
  59. if !exist {
  60. ctx.JSON(404, map[string]string{
  61. "Error": "the requested file could not be found",
  62. })
  63. }
  64. css := fmt.Sprintf("%s/css/MDB5-STANDARD-UI-KIT-Free-7.1.0/css/%s", c.WebRoot, f)
  65. b, err := os.ReadFile(css)
  66. if err != nil {
  67. ctx.JSON(500, map[string]string{
  68. "Error": "Could not serve the requested file",
  69. "msg": err.Error(),
  70. })
  71. }
  72. ctx.Data(200, "text/css", b)
  73. }
  74. // @Name ServeHtmx
  75. // @Summary serves some htmx assets
  76. // @Tags cdn
  77. // @Param file path string true "The JS file to serve to the client"
  78. // @Router /api/v1/htmx/{file} [get]
  79. func (c *Controller) ServeHtmx(ctx *gin.Context) {
  80. f, exist := ctx.Params.Get("file")
  81. if !exist {
  82. ctx.JSON(404, map[string]string{
  83. "Error": "the requested file could not be found",
  84. })
  85. }
  86. css := fmt.Sprintf("%s/htmx/%s", c.WebRoot, f)
  87. b, err := os.ReadFile(css)
  88. if err != nil {
  89. ctx.JSON(500, map[string]string{
  90. "Error": "Could not serve the requested file",
  91. "msg": err.Error(),
  92. })
  93. }
  94. ctx.Data(200, "text/javascript", b)
  95. }
  96. // @Name ServeAsset
  97. // @Summary serves assets to put in a webpage
  98. // @Tags cdn
  99. // @Router /assets/{file} [get]
  100. func (c *Controller) ServeAsset(ctx *gin.Context) {
  101. f, exist := ctx.Params.Get("file")
  102. if !exist {
  103. ctx.JSON(404, map[string]string{
  104. "Error": "the requested file could not be found",
  105. })
  106. }
  107. css := fmt.Sprintf("%s/assets/%s", c.WebRoot, f)
  108. b, err := os.ReadFile(css)
  109. if err != nil {
  110. ctx.JSON(500, map[string]string{
  111. "Error": "Could not serve the requested file",
  112. "msg": err.Error(),
  113. })
  114. }
  115. ctx.Data(200, "image/jpeg", b)
  116. }
  117. // @Name ServeImage
  118. // @Summary serves image from the image store
  119. // @Tags cdn
  120. // @Router /images/{file} [get]
  121. func (c *Controller) ServeImage(ctx *gin.Context) {
  122. f, exist := ctx.Params.Get("file")
  123. if !exist {
  124. ctx.JSON(404, map[string]string{
  125. "Error": "the requested file could not be found",
  126. })
  127. }
  128. css := fmt.Sprintf("%s/%s", helpers.GetImageStore(), f)
  129. b, err := os.ReadFile(css)
  130. if err != nil {
  131. ctx.JSON(500, map[string]string{
  132. "Error": "Could not serve the requested file",
  133. "msg": err.Error(),
  134. })
  135. }
  136. ctx.Data(200, "image/jpeg", b)
  137. }