Skip to content

Commit

Permalink
SPA example
Browse files Browse the repository at this point in the history
  • Loading branch information
aldas committed Jan 13, 2024
1 parent 4ffa066 commit d5d7e11
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion website/docs/middleware/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This serves static files from `static` directory and enables directory browsing.

Default behavior when using with non root URL paths is to append the URL path to the filesystem path.

#### Example
#### Example 1

```go
group := root.Group("somepath")
Expand All @@ -46,6 +46,33 @@ To turn off this behavior set the `IgnoreBase` config param to `true`.

:::


#### Example 2

Serve SPA assets from embbeded filesystem
```go
//go:embed web
var webAssets embed.FS

func main() {
e := echo.New()

e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
HTML5: true,
Root: "web", // because files are located in `web` directory in `webAssets` fs
Filesystem: http.FS(webAssets),
}))
api := e.Group("/api")
api.GET("/users", func(c echo.Context) error {
return c.String(http.StatusOK, "users")
})

if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}
```

## Configuration

```go
Expand Down Expand Up @@ -75,6 +102,10 @@ StaticConfig struct {
// the filesystem path is not doubled
// Optional. Default value false.
IgnoreBase bool `yaml:"ignoreBase"`

// Filesystem provides access to the static content.
// Optional. Defaults to http.Dir(config.Root)
Filesystem http.FileSystem `yaml:"-"`
}
```

Expand Down

0 comments on commit d5d7e11

Please sign in to comment.