31 lines
687 B
Go
31 lines
687 B
Go
package static
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ServeFileSystem interface {
|
|
http.FileSystem
|
|
Exists(prefix string, path string) bool
|
|
}
|
|
|
|
func ServeRoot(urlPrefix, root string) gin.HandlerFunc {
|
|
return Serve(urlPrefix, LocalFile(root, false))
|
|
}
|
|
|
|
// Serve 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()
|
|
}
|
|
}
|
|
}
|