70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package static
|
|
|
|
import (
|
|
"github.com/Superdanda/hade/framework/gin"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
const INDEX = "index.html"
|
|
|
|
type ServeFileSystem interface {
|
|
http.FileSystem
|
|
Exists(prefix string, path string) bool
|
|
}
|
|
|
|
type localFileSystem struct {
|
|
http.FileSystem
|
|
root string
|
|
indexes bool
|
|
}
|
|
|
|
func LocalFile(root string, indexes bool) *localFileSystem {
|
|
return &localFileSystem{
|
|
FileSystem: gin.Dir(root, indexes),
|
|
root: root,
|
|
indexes: indexes,
|
|
}
|
|
}
|
|
|
|
func (l *localFileSystem) Exists(prefix string, filepath string) bool {
|
|
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
|
|
name := path.Join(l.root, p)
|
|
stats, err := os.Stat(name)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if stats.IsDir() {
|
|
if !l.indexes {
|
|
index := path.Join(name, INDEX)
|
|
_, err := os.Stat(index)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func ServeRoot(urlPrefix, root string) gin.HandlerFunc {
|
|
return Serve(urlPrefix, LocalFile(root, false))
|
|
}
|
|
|
|
// Serve Static returns a middleware handler that serves static files in the given directory.
|
|
func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc {
|
|
fileServer := http.FileServer(fs)
|
|
if urlPrefix != "" {
|
|
fileServer = http.StripPrefix(urlPrefix, fileServer)
|
|
}
|
|
return func(c *gin.Context) {
|
|
if fs.Exists(urlPrefix, c.Request.URL.Path) {
|
|
fileServer.ServeHTTP(c.Writer, c.Request)
|
|
c.Abort()
|
|
}
|
|
}
|
|
}
|