github.com/zooyer/miskit@v1.0.71/utils/antd/antd.go (about)

     1  package antd
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gin-gonic/gin"
     7  )
     8  
     9  type antFS struct {
    10  	http.FileSystem
    11  	route map[string]bool
    12  }
    13  
    14  func AntFS(fs http.FileSystem, route ...string) *antFS {
    15  	var routes = make(map[string]bool)
    16  	for _, r := range route {
    17  		routes[r] = true
    18  	}
    19  	return &antFS{
    20  		FileSystem: fs,
    21  		route:      routes,
    22  	}
    23  }
    24  
    25  func (a *antFS) Open(filename string) (http.File, error) {
    26  	if a.route[filename] {
    27  		return a.FileSystem.Open("index.html")
    28  	}
    29  	return a.FileSystem.Open(filename)
    30  }
    31  
    32  func (a *antFS) Router(route string) gin.HandlerFunc {
    33  	return func(ctx *gin.Context) {
    34  		ctx.FileFromFS(route, a)
    35  	}
    36  }
    37  
    38  func (a *antFS) Mount(relativePath string, route gin.IRouter) {
    39  	for r := range a.route {
    40  		route.HEAD(r, a.Router(r))
    41  		route.GET(r, a.Router(r))
    42  	}
    43  	route.StaticFS(relativePath, a)
    44  }
    45  
    46  func (a *antFS) Redirect(src, dest string, router gin.IRouter) {
    47  	router.GET(src, func(ctx *gin.Context) {
    48  		ctx.Redirect(http.StatusFound, dest)
    49  	})
    50  }