Skip to content

Commit 38fb806

Browse files
efectnReneWerner87
andauthored
middleware: add static middleware (#3006)
* middleware: add static middleware * uncomment broken tests * introduce isfile config property to fix file issues * test * add io/fs support to static mw * add io/fs support to static mw * remove filesystem and app.Static * fix linter * apply review * support disablecache * support multi indexes * add an example for io/fs * update whats new & apply reviews * update * use fasthttp from master * Update .github/README.md Co-authored-by: RW <[email protected]> * update1 * apply reviews * update * update * update examples * add more examples --------- Co-authored-by: RW <[email protected]>
1 parent fca62c1 commit 38fb806

File tree

20 files changed

+1259
-1705
lines changed

20 files changed

+1259
-1705
lines changed

.github/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ func main() {
203203
func main() {
204204
app := fiber.New()
205205

206-
app.Static("/", "./public")
206+
app.Get("/*", static.New("./public"))
207207
// => http://localhost:3000/js/script.js
208208
// => http://localhost:3000/css/style.css
209209

210-
app.Static("/prefix", "./public")
210+
app.Get("/prefix*", static.New("./public"))
211211
// => http://localhost:3000/prefix/js/script.js
212212
// => http://localhost:3000/prefix/css/style.css
213213

214-
app.Static("*", "./public/index.html")
214+
app.Get("*", static.New("./public/index.html"))
215215
// => http://localhost:3000/any/path/shows/index/html
216216

217217
log.Fatal(app.Listen(":3000"))
@@ -388,7 +388,7 @@ curl -H "Origin: http://example.com" --verbose http://localhost:3000
388388
func main() {
389389
app := fiber.New()
390390

391-
app.Static("/", "./public")
391+
app.Get("/", static.New("./public"))
392392

393393
app.Get("/demo", func(c fiber.Ctx) error {
394394
return c.SendString("This is a demo!")
@@ -586,7 +586,6 @@ Here is a list of middleware that are included within the Fiber framework.
586586
| [etag](https://github.com/gofiber/fiber/tree/main/middleware/etag) | Allows for caches to be more efficient and save bandwidth, as a web server does not need to resend a full response if the content has not changed. |
587587
| [expvar](https://github.com/gofiber/fiber/tree/main/middleware/expvar) | Serves via its HTTP server runtime exposed variants in the JSON format. |
588588
| [favicon](https://github.com/gofiber/fiber/tree/main/middleware/favicon) | Ignore favicon from logs or serve from memory if a file path is provided. |
589-
| [filesystem](https://github.com/gofiber/fiber/tree/main/middleware/filesystem) | FileSystem middleware for Fiber. |
590589
| [healthcheck](https://github.com/gofiber/fiber/tree/main/middleware/healthcheck) | Liveness and Readiness probes for Fiber. |
591590
| [helmet](https://github.com/gofiber/fiber/tree/main/middleware/helmet) | Helps secure your apps by setting various HTTP headers. |
592591
| [idempotency](https://github.com/gofiber/fiber/tree/main/middleware/idempotency) | Allows for fault-tolerant APIs where duplicate requests do not erroneously cause the same action performed multiple times on the server-side. |
@@ -601,6 +600,7 @@ Here is a list of middleware that are included within the Fiber framework.
601600
| [rewrite](https://github.com/gofiber/fiber/tree/main/middleware/rewrite) | Rewrites the URL path based on provided rules. It can be helpful for backward compatibility or just creating cleaner and more descriptive links. |
602601
| [session](https://github.com/gofiber/fiber/tree/main/middleware/session) | Session middleware. NOTE: This middleware uses our Storage package. |
603602
| [skip](https://github.com/gofiber/fiber/tree/main/middleware/skip) | Skip middleware that skips a wrapped handler if a predicate is true. |
603+
| [static](https://github.com/gofiber/fiber/tree/main/middleware/static) | Static middleware for Fiber that serves static files such as **images**, **CSS,** and **JavaScript**. |
604604
| [timeout](https://github.com/gofiber/fiber/tree/main/middleware/timeout) | Adds a max time for a request and forwards to ErrorHandler if it is exceeded. |
605605

606606
## 🧬 External Middleware

app.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -381,52 +381,6 @@ type Config struct {
381381
EnableSplittingOnParsers bool `json:"enable_splitting_on_parsers"`
382382
}
383383

384-
// Static defines configuration options when defining static assets.
385-
type Static struct {
386-
// When set to true, the server tries minimizing CPU usage by caching compressed files.
387-
// This works differently than the github.com/gofiber/compression middleware.
388-
// Optional. Default value false
389-
Compress bool `json:"compress"`
390-
391-
// When set to true, enables byte range requests.
392-
// Optional. Default value false
393-
ByteRange bool `json:"byte_range"`
394-
395-
// When set to true, enables directory browsing.
396-
// Optional. Default value false.
397-
Browse bool `json:"browse"`
398-
399-
// When set to true, enables direct download.
400-
// Optional. Default value false.
401-
Download bool `json:"download"`
402-
403-
// The name of the index file for serving a directory.
404-
// Optional. Default value "index.html".
405-
Index string `json:"index"`
406-
407-
// Expiration duration for inactive file handlers.
408-
// Use a negative time.Duration to disable it.
409-
//
410-
// Optional. Default value 10 * time.Second.
411-
CacheDuration time.Duration `json:"cache_duration"`
412-
413-
// The value for the Cache-Control HTTP-header
414-
// that is set on the file response. MaxAge is defined in seconds.
415-
//
416-
// Optional. Default value 0.
417-
MaxAge int `json:"max_age"`
418-
419-
// ModifyResponse defines a function that allows you to alter the response.
420-
//
421-
// Optional. Default: nil
422-
ModifyResponse Handler
423-
424-
// Next defines a function to skip this middleware when returned true.
425-
//
426-
// Optional. Default: nil
427-
Next func(c Ctx) bool
428-
}
429-
430384
// RouteMessage is some message need to be print when server starts
431385
type RouteMessage struct {
432386
name string
@@ -780,13 +734,6 @@ func (app *App) Add(methods []string, path string, handler Handler, middleware .
780734
return app
781735
}
782736

783-
// Static will create a file server serving static files
784-
func (app *App) Static(prefix, root string, config ...Static) Router {
785-
app.registerStatic(prefix, root, config...)
786-
787-
return app
788-
}
789-
790737
// All will register the handler on all HTTP methods
791738
func (app *App) All(path string, handler Handler, middleware ...Handler) Router {
792739
return app.Add(app.config.RequestMethods, path, handler, middleware...)

0 commit comments

Comments
 (0)