cdn_handlers.go 4.5 KB

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