wip: add middlewares

This commit is contained in:
Stavros
2025-08-25 13:28:30 +03:00
parent 4979121395
commit ace22acdb2
6 changed files with 276 additions and 225 deletions

View File

@@ -0,0 +1,66 @@
package middlewares
import (
"io/fs"
"net/http"
"os"
"strings"
"tinyauth/internal/assets"
"github.com/gin-gonic/gin"
)
type UIMiddleware struct {
UIFS fs.FS
UIFileServer http.Handler
ResourcesFileServer http.Handler
}
func NewUIMiddleware() (*UIMiddleware, error) {
ui, err := fs.Sub(assets.Assets, "dist")
if err != nil {
return nil, err
}
uiFileServer := http.FileServer(http.FS(ui))
resourcesFileServer := http.FileServer(http.Dir("/data/resources"))
return &UIMiddleware{
UIFS: ui,
UIFileServer: uiFileServer,
ResourcesFileServer: resourcesFileServer,
}, nil
}
func (m UIMiddleware) Middlware() gin.HandlerFunc {
return func(c *gin.Context) {
switch strings.Split(c.Request.URL.Path, "/")[1] {
case "api":
c.Next()
return
case "resources":
_, err := os.Stat("/data/resources/" + strings.TrimPrefix(c.Request.URL.Path, "/resources/"))
if os.IsNotExist(err) {
c.Status(404)
c.Abort()
return
}
m.ResourcesFileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
return
default:
_, err := fs.Stat(m.UIFS, strings.TrimPrefix(c.Request.URL.Path, "/"))
if os.IsNotExist(err) {
c.Request.URL.Path = "/"
}
m.UIFileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
return
}
}
}