Skip to content

Commit

Permalink
Add IRouter interface, test and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
souloss committed Sep 28, 2021
1 parent 77e922b commit c32167d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions routergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,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
5 changes: 5 additions & 0 deletions routergroup_test.go
Original file line number Diff line number Diff line change
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))
}

0 comments on commit c32167d

Please sign in to comment.