Skip to content

Commit

Permalink
support multi indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed May 19, 2024
1 parent 545a5eb commit 75c4067
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
6 changes: 3 additions & 3 deletions docs/middleware/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ curl http://localhost:3000/static/john/doee # will show hello.html
</details>

:::caution
If you want to define static routes using `Get`, you need to use the wildcard (`*`) operator at the end of the route.
If you want to define static routes using `Get`, you need to put the wildcard (`*`) operator at the end of the route.
:::

## Config
Expand All @@ -113,7 +113,7 @@ If you want to define static routes using `Get`, you need to use the wildcard (`
| ByteRange | `bool` | When set to true, enables byte range requests. | `false` |
| Browse | `bool` | When set to true, enables directory browsing. | `false` |
| Download | `bool` | When set to true, enables direct download. | `false` |
| Index | `string` | The name of the index file for serving a directory. | `index.html` |
| IndexNames | `[]string` | The names of the index files for serving a directory. | `[]string{"index.html"}` |
| CacheDuration | `string` | Expiration duration for inactive file handlers.<br /><br />Use a negative time.Duration to disable it. | `10 * time.Second` |
| MaxAge | `int` | The value for the Cache-Control HTTP-header that is set on the file response. MaxAge is defined in seconds. | `0` |
| ModifyResponse | `fiber.Handler` | ModifyResponse defines a function that allows you to alter the response. | `nil` |
Expand All @@ -123,7 +123,7 @@ If you want to define static routes using `Get`, you need to use the wildcard (`

```go
var ConfigDefault = Config{
Index: "index.html",
Index: []string{"index.html"},
CacheDuration: 10 * time.Second,
}
```
12 changes: 6 additions & 6 deletions middleware/static/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ type Config struct {
// Optional. Default: false.
Download bool `json:"download"`

// The name of the index file for serving a directory.
// The names of the index files for serving a directory.
//
// Optional. Default: "index.html".
Index string `json:"index"`
// Optional. Default: []string{"index.html"}.
IndexNames []string `json:"index"`

// Expiration duration for inactive file handlers.
// Use a negative time.Duration to disable it.
Expand All @@ -66,7 +66,7 @@ type Config struct {

// ConfigDefault is the default config
var ConfigDefault = Config{
Index: "index.html",
IndexNames: []string{"index.html"},
CacheDuration: 10 * time.Second,
}

Expand All @@ -81,8 +81,8 @@ func configDefault(config ...Config) Config {
cfg := config[0]

// Set default values
if cfg.Index == "" {
cfg.Index = ConfigDefault.Index
if cfg.IndexNames == nil {
cfg.IndexNames = ConfigDefault.IndexNames
}

if cfg.CacheDuration == 0 {
Expand Down
6 changes: 1 addition & 5 deletions middleware/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func New(root string, cfg ...Config) fiber.Handler {
CompressedFileSuffix: c.App().Config().CompressedFileSuffix,
CacheDuration: config.CacheDuration,
SkipCache: config.CacheDuration < 0,
IndexNames: []string{"index.html"},
IndexNames: config.IndexNames,
PathNotFound: func(fctx *fasthttp.RequestCtx) {
fctx.Response.SetStatusCode(fiber.StatusNotFound)
},
Expand Down Expand Up @@ -109,10 +109,6 @@ func New(root string, cfg ...Config) fiber.Handler {
cacheControlValue = "public, max-age=" + strconv.Itoa(maxAge)
}

if config.Index != "" {
fs.IndexNames = []string{config.Index}
}

fileHandler = fs.NewRequestHandler()
})

Expand Down
6 changes: 3 additions & 3 deletions middleware/static/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_Static_Index_Default(t *testing.T) {
app.Get("", New("../../.github/"))

app.Get("test", New("", Config{
Index: "index.html",
IndexNames: []string{"index.html"},
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
Expand Down Expand Up @@ -564,8 +564,8 @@ func Test_Static_FS_Prefix_Wildcard(t *testing.T) {
app := fiber.New()

app.Get("/test*", New("index.html", Config{
FS: os.DirFS("../../.github"),
Index: "not_index.html",
FS: os.DirFS("../../.github"),
IndexNames: []string{"not_index.html"},
}))

req := httptest.NewRequest(fiber.MethodGet, "/test/john/doe", nil)
Expand Down

0 comments on commit 75c4067

Please sign in to comment.