mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
Add IRouter interface, test and documentation
This commit is contained in:
parent
77e922b97c
commit
c32167d65f
26
README.md
26
README.md
@ -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
2
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
|
||||
|
@ -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
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user