static/serve.go

31 lines
687 B
Go
Raw Normal View History

package static
import (
"net/http"
2018-08-27 05:51:13 +03:00
"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()
}
}
}