123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package controller
- import (
- "fmt"
- "os"
- "path"
- "strings"
- "git.aetherial.dev/aeth/keiji/pkg/helpers"
- "github.com/gin-gonic/gin"
- )
- // @Name ServeCss
- // @Summary serves css files from the web root directory
- // @Tags cdn
- // @Param file path string true "The CSS file to serve to the client"
- // @Router /api/v1/style/{file} [get]
- func (c *Controller) ServeCss(ctx *gin.Context) {
- f, exist := ctx.Params.Get("file")
- if !exist {
- ctx.JSON(404, map[string]string{
- "Error": "the requested file could not be found",
- })
- }
- css := fmt.Sprintf("%s/css/bootstrap-5.0.2/dist/css/%s", c.WebRoot, f)
- b, err := os.ReadFile(css)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Could not serve the requested file",
- "msg": err.Error(),
- })
- }
- ctx.Data(200, "text/css", b)
- }
- // @Name ServeJs
- // @Summary serves js files from the web root directory
- // @Tags cdn
- // @Param file path string true "The Javascript file to serve to the client"
- // @Router /api/v1/js/{file} [get]
- func (c *Controller) ServeJs(ctx *gin.Context) {
- f, exist := ctx.Params.Get("file")
- if !exist {
- ctx.JSON(404, map[string]string{
- "Error": "the requested file could not be found",
- })
- }
- css := fmt.Sprintf("%s/css/bootstrap-5.0.2/dist/js/%s", c.WebRoot, f)
- b, err := os.ReadFile(css)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Could not serve the requested file",
- "msg": err.Error(),
- })
- }
- ctx.Data(200, "text/javascript", b)
- }
- // @Name ServeMdbCss
- // @Summary serves some mdb assets
- // @Tags cdn
- // @Param file path string true "The CSS file to serve to the client"
- // @Router /api/v1/style/mdb/{file} [get]
- func (c *Controller) ServeMdbCss(ctx *gin.Context) {
- f, exist := ctx.Params.Get("file")
- if !exist {
- ctx.JSON(404, map[string]string{
- "Error": "the requested file could not be found",
- })
- }
- css := fmt.Sprintf("%s/css/MDB5-STANDARD-UI-KIT-Free-7.1.0/css/%s", c.WebRoot, f)
- b, err := os.ReadFile(css)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Could not serve the requested file",
- "msg": err.Error(),
- })
- }
- ctx.Data(200, "text/css", b)
- }
- // @Name ServeHtmx
- // @Summary serves some htmx assets
- // @Tags cdn
- // @Param file path string true "The JS file to serve to the client"
- // @Router /api/v1/htmx/{file} [get]
- func (c *Controller) ServeHtmx(ctx *gin.Context) {
- f, exist := ctx.Params.Get("file")
- if !exist {
- ctx.JSON(404, map[string]string{
- "Error": "the requested file could not be found",
- })
- }
- css := fmt.Sprintf("%s/htmx/%s", c.WebRoot, f)
- b, err := os.ReadFile(css)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Could not serve the requested file",
- "msg": err.Error(),
- })
- }
- ctx.Data(200, "text/javascript", b)
- }
- // @Name ServeAsset
- // @Summary serves assets to put in a webpage
- // @Tags cdn
- // @Router /assets/{file} [get]
- func (c *Controller) ServeAsset(ctx *gin.Context) {
- f, exist := ctx.Params.Get("file")
- if !exist {
- ctx.JSON(404, map[string]string{
- "Error": "the requested file could not be found",
- })
- }
- css := fmt.Sprintf("%s/assets/%s", c.WebRoot, f)
- b, err := os.ReadFile(css)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Could not serve the requested file",
- "msg": err.Error(),
- })
- }
- ctx.Data(200, "image/jpeg", b)
- }
- // @Name ServeImage
- // @Summary serves image from the image store
- // @Tags cdn
- // @Router /images/{file} [get]
- func (c *Controller) ServeImage(ctx *gin.Context) {
- f, exist := ctx.Params.Get("file")
- if !exist {
- ctx.JSON(404, map[string]string{
- "Error": "the requested file could not be found",
- })
- }
- css := fmt.Sprintf("%s/%s", helpers.GetImageStore(), f)
- b, err := os.ReadFile(css)
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Could not serve the requested file",
- "msg": err.Error(),
- })
- }
- ctx.Data(200, "image/jpeg", b)
- }
- // @Name ServeGeneric
- // @Summary serves file from the html file
- // @Tags cdn
- // @Router /api/v1/cdn/{file} [get]
- func (c *Controller) ServeGeneric(ctx *gin.Context) {
- f, exist := ctx.Params.Get("file")
- if !exist {
- ctx.JSON(404, map[string]string{
- "Error": "the requested file could not be found",
- })
- return
- }
- fext := strings.Split(f, ".")[len(strings.Split(f, "."))-1]
- var ctype string
- switch {
- case fext == "css":
- ctype = "text/css"
- case fext == "js":
- ctype = "text/javascript"
- case fext == "json":
- ctype = "application/json"
- default:
- ctype = "text"
- }
- b, err := os.ReadFile(path.Join(c.WebRoot, f))
- if err != nil {
- ctx.JSON(500, map[string]string{
- "Error": "Could not serve the requested file",
- "msg": err.Error(),
- })
- return
- }
- ctx.Data(200, ctype, b)
- }
|