Add IRouter interface, test and documentation

This commit is contained in:
witchc 2021-09-29 01:38:52 +08:00
parent 77e922b97c
commit c32167d65f
4 changed files with 33 additions and 1 deletions

View File

@ -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

2
go.mod
View File

@ -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

View File

@ -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

View File

@ -5,6 +5,7 @@
package gin
import (
"embed"
"net/http"
"testing"
@ -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))
@ -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))
}