mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-10-28 04:35:40 +00:00 
			
		
		
		
	 ad4fc7ef5f
			
		
	
	ad4fc7ef5f
	
	
	
		
			
			* refactor: don't export non-needed fields * feat: coderabbit suggestions * fix: avoid queries panic
		
			
				
	
	
		
			57 lines
		
	
	
		
			914 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			914 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package middleware
 | |
| 
 | |
| import (
 | |
| 	"io/fs"
 | |
| 	"net/http"
 | |
| 	"os"
 | |
| 	"strings"
 | |
| 	"tinyauth/internal/assets"
 | |
| 
 | |
| 	"github.com/gin-gonic/gin"
 | |
| )
 | |
| 
 | |
| type UIMiddleware struct {
 | |
| 	uiFs         fs.FS
 | |
| 	uiFileServer http.Handler
 | |
| }
 | |
| 
 | |
| func NewUIMiddleware() *UIMiddleware {
 | |
| 	return &UIMiddleware{}
 | |
| }
 | |
| 
 | |
| func (m *UIMiddleware) Init() error {
 | |
| 	ui, err := fs.Sub(assets.FrontendAssets, "dist")
 | |
| 
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	m.uiFs = ui
 | |
| 	m.uiFileServer = http.FileServer(http.FS(ui))
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (m *UIMiddleware) Middleware() gin.HandlerFunc {
 | |
| 	return func(c *gin.Context) {
 | |
| 		switch strings.Split(c.Request.URL.Path, "/")[1] {
 | |
| 		case "api":
 | |
| 			c.Next()
 | |
| 			return
 | |
| 		case "resources":
 | |
| 			c.Next()
 | |
| 			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
 | |
| 		}
 | |
| 	}
 | |
| }
 |