cdn_handlers.go 4.6 KB

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