mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +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
|
### Serving data from file
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module github.com/gin-gonic/gin
|
module github.com/gin-gonic/gin
|
||||||
|
|
||||||
go 1.13
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-contrib/sse v0.1.0
|
github.com/gin-contrib/sse v0.1.0
|
||||||
|
@ -38,6 +38,7 @@ type IRoutes interface {
|
|||||||
OPTIONS(string, ...HandlerFunc) IRoutes
|
OPTIONS(string, ...HandlerFunc) IRoutes
|
||||||
HEAD(string, ...HandlerFunc) IRoutes
|
HEAD(string, ...HandlerFunc) IRoutes
|
||||||
|
|
||||||
|
StaticFSFromEmbed(relativePath, root string, assets embed.FS) IRoutes
|
||||||
StaticFile(string, string) IRoutes
|
StaticFile(string, string) IRoutes
|
||||||
Static(string, string) IRoutes
|
Static(string, string) IRoutes
|
||||||
StaticFS(string, http.FileSystem) IRoutes
|
StaticFS(string, http.FileSystem) IRoutes
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -162,6 +163,9 @@ func TestRouterGroupPipeline(t *testing.T) {
|
|||||||
testRoutesInterface(t, v1)
|
testRoutesInterface(t, v1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed testdata
|
||||||
|
var embed_static embed.FS
|
||||||
|
|
||||||
func testRoutesInterface(t *testing.T, r IRoutes) {
|
func testRoutesInterface(t *testing.T, r IRoutes) {
|
||||||
handler := func(c *Context) {}
|
handler := func(c *Context) {}
|
||||||
assert.Equal(t, r, r.Use(handler))
|
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.StaticFile("/file", "."))
|
||||||
assert.Equal(t, r, r.Static("/static", "."))
|
assert.Equal(t, r, r.Static("/static", "."))
|
||||||
assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))
|
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