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

embed enhancement #2885

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions README.md
Expand Up @@ -1248,6 +1248,32 @@ func main() {
}
```

### Serving static files from embed

```go
// static
// ├── css
// │ └── chunk.css
// ├── favicon.ico
// ├── index.html
//
//go:embed static
var static embed.FS

func main() {
router := gin.Default()
// /public/css/chunk.css
// /public/favicon.ico
// /public/index.html
router.StaticFSFromEmbed("/public/", "static/", static)
// /asset/chunk.css
router.StaticFSFromEmbed("/asset/", "static/css/", static)
// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}

```

### Serving data from file

```go
Expand Down
2 changes: 1 addition & 1 deletion go.mod
@@ -1,6 +1,6 @@
module github.com/gin-gonic/gin

go 1.13
go 1.16

require (
github.com/gin-contrib/sse v0.1.0
Expand Down
30 changes: 30 additions & 0 deletions routergroup.go
Expand Up @@ -5,6 +5,8 @@
package gin

import (
"embed"
"io/fs"
"net/http"
"path"
"regexp"
Expand Down Expand Up @@ -36,6 +38,7 @@ type IRoutes interface {
OPTIONS(string, ...HandlerFunc) IRoutes
HEAD(string, ...HandlerFunc) IRoutes

StaticFSFromEmbed(relativePath, root string, assets embed.FS) IRoutes
StaticFile(string, string) IRoutes
Static(string, string) IRoutes
StaticFS(string, http.FileSystem) IRoutes
Expand Down Expand Up @@ -172,6 +175,33 @@ func (group *RouterGroup) Static(relativePath, root string) IRoutes {
return group.StaticFS(relativePath, Dir(root, false))
}

// Define redirectable embed fs interface
type fsFunc func(name string) (fs.File, error)

func (f fsFunc) Open(name string) (fs.File, error) {
return f(name)
}

// Redirectable embed files
// use :
// //go:embed static
// var static embed.FS
// ......
// router.StaticFSFromEmbed("/", "static/", static)
func (group *RouterGroup) StaticFSFromEmbed(relativePath, root string, assets embed.FS) IRoutes {
fs := http.FS(fsFunc(func(name string) (fs.File, error) {
assetPath := path.Join(root, name)
// If we can't find the asset, fs can handle the error
file, err := assets.Open(assetPath)
if err != nil {
return nil, err
}
// Otherwise assume this is a legitimate request routed correctly
return file, err
}))
return group.StaticFS(relativePath, fs)
}

// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.
// Gin by default user: gin.Dir()
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes {
Expand Down
5 changes: 5 additions & 0 deletions routergroup_test.go
Expand Up @@ -5,6 +5,7 @@
package gin

import (
"embed"
"net/http"
"testing"

Expand Down Expand Up @@ -162,6 +163,9 @@ func TestRouterGroupPipeline(t *testing.T) {
testRoutesInterface(t, v1)
}

//go:embed testdata
var embed_static embed.FS

func testRoutesInterface(t *testing.T, r IRoutes) {
handler := func(c *Context) {}
assert.Equal(t, r, r.Use(handler))
Expand All @@ -179,4 +183,5 @@ func testRoutesInterface(t *testing.T, r IRoutes) {
assert.Equal(t, r, r.StaticFile("/file", "."))
assert.Equal(t, r, r.Static("/static", "."))
assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))
assert.Equal(t, r, r.StaticFSFromEmbed("/static3", "testdata", embed_static))
}