mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-10-31 06:05:43 +00:00 
			
		
		
		
	refactor: move resource handling to a controller
This commit is contained in:
		| @@ -32,10 +32,6 @@ func (m *ContextMiddleware) Init() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *ContextMiddleware) Name() string { | ||||
| 	return "ContextMiddleware" | ||||
| } | ||||
|  | ||||
| func (m *ContextMiddleware) Middleware() gin.HandlerFunc { | ||||
| 	return func(c *gin.Context) { | ||||
| 		cookie, err := m.Auth.GetSessionCookie(c) | ||||
|   | ||||
| @@ -4,28 +4,19 @@ import ( | ||||
| 	"io/fs" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"strings" | ||||
| 	"tinyauth/internal/assets" | ||||
|  | ||||
| 	"github.com/gin-gonic/gin" | ||||
| ) | ||||
|  | ||||
| type UIMiddlewareConfig struct { | ||||
| 	ResourcesDir string | ||||
| } | ||||
|  | ||||
| type UIMiddleware struct { | ||||
| 	Config              UIMiddlewareConfig | ||||
| 	UIFS                fs.FS | ||||
| 	UIFileServer        http.Handler | ||||
| 	ResourcesFileServer http.Handler | ||||
| 	UIFS         fs.FS | ||||
| 	UIFileServer http.Handler | ||||
| } | ||||
|  | ||||
| func NewUIMiddleware(config UIMiddlewareConfig) *UIMiddleware { | ||||
| 	return &UIMiddleware{ | ||||
| 		Config: config, | ||||
| 	} | ||||
| func NewUIMiddleware() *UIMiddleware { | ||||
| 	return &UIMiddleware{} | ||||
| } | ||||
|  | ||||
| func (m *UIMiddleware) Init() error { | ||||
| @@ -37,15 +28,10 @@ func (m *UIMiddleware) Init() error { | ||||
|  | ||||
| 	m.UIFS = ui | ||||
| 	m.UIFileServer = http.FileServer(http.FS(ui)) | ||||
| 	m.ResourcesFileServer = http.FileServer(http.Dir(m.Config.ResourcesDir)) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *UIMiddleware) Name() string { | ||||
| 	return "UIMiddleware" | ||||
| } | ||||
|  | ||||
| func (m *UIMiddleware) Middleware() gin.HandlerFunc { | ||||
| 	return func(c *gin.Context) { | ||||
| 		switch strings.Split(c.Request.URL.Path, "/")[1] { | ||||
| @@ -53,24 +39,7 @@ func (m *UIMiddleware) Middleware() gin.HandlerFunc { | ||||
| 			c.Next() | ||||
| 			return | ||||
| 		case "resources": | ||||
| 			requestFilePath := m.Config.ResourcesDir + strings.TrimPrefix(c.Request.URL.Path, "/resources/") | ||||
|  | ||||
| 			if !filepath.IsLocal(requestFilePath) { | ||||
| 				c.Status(404) | ||||
| 				c.Abort() | ||||
| 				return | ||||
| 			} | ||||
|  | ||||
| 			_, err := os.Stat(requestFilePath) | ||||
|  | ||||
| 			if os.IsNotExist(err) { | ||||
| 				c.Status(404) | ||||
| 				c.Abort() | ||||
| 				return | ||||
| 			} | ||||
|  | ||||
| 			m.ResourcesFileServer.ServeHTTP(c.Writer, c.Request) | ||||
| 			c.Abort() | ||||
| 			c.Next() | ||||
| 			return | ||||
| 		default: | ||||
| 			_, err := fs.Stat(m.UIFS, strings.TrimPrefix(c.Request.URL.Path, "/")) | ||||
|   | ||||
| @@ -26,10 +26,6 @@ func (m *ZerologMiddleware) Init() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m *ZerologMiddleware) Name() string { | ||||
| 	return "ZerologMiddleware" | ||||
| } | ||||
|  | ||||
| func (m *ZerologMiddleware) logPath(path string) bool { | ||||
| 	for _, prefix := range loggerSkipPathsPrefix { | ||||
| 		if strings.HasPrefix(path, prefix) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Stavros
					Stavros