Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add StaticFileFS #2749

Merged
merged 3 commits into from Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -1241,7 +1241,8 @@ func main() {
router.Static("/assets", "./assets")
router.StaticFS("/more_static", http.Dir("my_file_system"))
router.StaticFile("/favicon.ico", "./resources/favicon.ico")

router.StaticFileFS("/more_favicon.ico", "more_favicon.ico", http.Dir("my_file_system"))
thinkgos marked this conversation as resolved.
Show resolved Hide resolved

// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}
Expand Down
16 changes: 16 additions & 0 deletions routergroup.go
Expand Up @@ -37,6 +37,7 @@ type IRoutes interface {
HEAD(string, ...HandlerFunc) IRoutes

StaticFile(string, string) IRoutes
StaticFileFS(string, string, http.FileSystem) IRoutes
Static(string, string) IRoutes
StaticFS(string, http.FileSystem) IRoutes
}
Expand Down Expand Up @@ -162,6 +163,21 @@ func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes {
return group.returnObj()
}

// StaticFileFS works just like `StaticFile` but a custom `http.FileSystem` can be used instead..
// router.StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})
// Gin by default user: gin.Dir()
func (group *RouterGroup) StaticFileFS(relativePath, filepath string, fs http.FileSystem) IRoutes {
thinkgos marked this conversation as resolved.
Show resolved Hide resolved
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static file")
}
handler := func(c *Context) {
c.FileFromFS(filepath, fs)
}
group.GET(relativePath, handler)
group.HEAD(relativePath, handler)
return group.returnObj()
}

// Static serves files from the given file system root.
// Internally a http.FileServer is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
Expand Down
12 changes: 12 additions & 0 deletions routergroup_test.go
Expand Up @@ -111,6 +111,17 @@ func TestRouterGroupInvalidStaticFile(t *testing.T) {
})
}

func TestRouterGroupInvalidStaticFileFS(t *testing.T) {
router := New()
assert.Panics(t, func() {
router.StaticFileFS("/path/:param", "favicon.ico", Dir(".", false))
})

assert.Panics(t, func() {
router.StaticFileFS("/path/*param", "favicon.ico", Dir(".", false))
})
}

func TestRouterGroupTooManyHandlers(t *testing.T) {
router := New()
handlers1 := make([]HandlerFunc, 40)
Expand Down Expand Up @@ -173,6 +184,7 @@ func testRoutesInterface(t *testing.T, r IRoutes) {
assert.Equal(t, r, r.HEAD("/", handler))

assert.Equal(t, r, r.StaticFile("/file", "."))
assert.Equal(t, r, r.StaticFileFS("/static2", ".", Dir(".", false)))
assert.Equal(t, r, r.Static("/static", "."))
assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))
}
34 changes: 34 additions & 0 deletions routes_test.go
Expand Up @@ -324,6 +324,40 @@ func TestRouteStaticFile(t *testing.T) {
assert.Equal(t, http.StatusOK, w3.Code)
}

// TestHandleStaticFile - ensure the static file handles properly
func TestRouteStaticFileFS(t *testing.T) {
// SETUP file
testRoot, _ := os.Getwd()
f, err := ioutil.TempFile(testRoot, "")
if err != nil {
t.Error(err)
}
defer os.Remove(f.Name())
_, err = f.WriteString("Gin Web Framework")
assert.NoError(t, err)
f.Close()

dir, filename := filepath.Split(f.Name())
// SETUP gin
router := New()
router.Static("/using_static", dir)
router.StaticFileFS("/result_fs", filename, Dir(dir, false))

w := performRequest(router, http.MethodGet, "/using_static/"+filename)
w2 := performRequest(router, http.MethodGet, "/result_fs")

assert.Equal(t, w, w2)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "Gin Web Framework", w.Body.String())
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))

w3 := performRequest(router, http.MethodHead, "/using_static/"+filename)
w4 := performRequest(router, http.MethodHead, "/result_fs")

assert.Equal(t, w3, w4)
assert.Equal(t, http.StatusOK, w3.Code)
}

// TestHandleStaticDir - ensure the root/sub dir handles properly
func TestRouteStaticListingDir(t *testing.T) {
router := New()
Expand Down