Skip to content

Commit

Permalink
middleware: add static middleware (#3006)
Browse files Browse the repository at this point in the history
* 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 <rene@gofiber.io>

* update1

* apply reviews

* update

* update

* update examples

* add more examples

---------

Co-authored-by: RW <rene@gofiber.io>
  • Loading branch information
efectn and ReneWerner87 authored May 28, 2024
1 parent fca62c1 commit 38fb806
Show file tree
Hide file tree
Showing 20 changed files with 1,259 additions and 1,705 deletions.
10 changes: 5 additions & 5 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ func main() {
func main() {
app := fiber.New()

app.Static("/", "./public")
app.Get("/*", static.New("./public"))
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css

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

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

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

app.Static("/", "./public")
app.Get("/", static.New("./public"))

app.Get("/demo", func(c fiber.Ctx) error {
return c.SendString("This is a demo!")
Expand Down Expand Up @@ -586,7 +586,6 @@ Here is a list of middleware that are included within the Fiber framework.
| [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. |
| [expvar](https://github.com/gofiber/fiber/tree/main/middleware/expvar) | Serves via its HTTP server runtime exposed variants in the JSON format. |
| [favicon](https://github.com/gofiber/fiber/tree/main/middleware/favicon) | Ignore favicon from logs or serve from memory if a file path is provided. |
| [filesystem](https://github.com/gofiber/fiber/tree/main/middleware/filesystem) | FileSystem middleware for Fiber. |
| [healthcheck](https://github.com/gofiber/fiber/tree/main/middleware/healthcheck) | Liveness and Readiness probes for Fiber. |
| [helmet](https://github.com/gofiber/fiber/tree/main/middleware/helmet) | Helps secure your apps by setting various HTTP headers. |
| [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. |
Expand All @@ -601,6 +600,7 @@ Here is a list of middleware that are included within the Fiber framework.
| [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. |
| [session](https://github.com/gofiber/fiber/tree/main/middleware/session) | Session middleware. NOTE: This middleware uses our Storage package. |
| [skip](https://github.com/gofiber/fiber/tree/main/middleware/skip) | Skip middleware that skips a wrapped handler if a predicate is true. |
| [static](https://github.com/gofiber/fiber/tree/main/middleware/static) | Static middleware for Fiber that serves static files such as **images**, **CSS,** and **JavaScript**. |
| [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. |

## 🧬 External Middleware
Expand Down
53 changes: 0 additions & 53 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,52 +381,6 @@ type Config struct {
EnableSplittingOnParsers bool `json:"enable_splitting_on_parsers"`
}

// Static defines configuration options when defining static assets.
type Static struct {
// When set to true, the server tries minimizing CPU usage by caching compressed files.
// This works differently than the github.com/gofiber/compression middleware.
// Optional. Default value false
Compress bool `json:"compress"`

// When set to true, enables byte range requests.
// Optional. Default value false
ByteRange bool `json:"byte_range"`

// When set to true, enables directory browsing.
// Optional. Default value false.
Browse bool `json:"browse"`

// When set to true, enables direct download.
// Optional. Default value false.
Download bool `json:"download"`

// The name of the index file for serving a directory.
// Optional. Default value "index.html".
Index string `json:"index"`

// Expiration duration for inactive file handlers.
// Use a negative time.Duration to disable it.
//
// Optional. Default value 10 * time.Second.
CacheDuration time.Duration `json:"cache_duration"`

// The value for the Cache-Control HTTP-header
// that is set on the file response. MaxAge is defined in seconds.
//
// Optional. Default value 0.
MaxAge int `json:"max_age"`

// ModifyResponse defines a function that allows you to alter the response.
//
// Optional. Default: nil
ModifyResponse Handler

// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c Ctx) bool
}

// RouteMessage is some message need to be print when server starts
type RouteMessage struct {
name string
Expand Down Expand Up @@ -780,13 +734,6 @@ func (app *App) Add(methods []string, path string, handler Handler, middleware .
return app
}

// Static will create a file server serving static files
func (app *App) Static(prefix, root string, config ...Static) Router {
app.registerStatic(prefix, root, config...)

return app
}

// All will register the handler on all HTTP methods
func (app *App) All(path string, handler Handler, middleware ...Handler) Router {
return app.Add(app.config.RequestMethods, path, handler, middleware...)
Expand Down
Loading

0 comments on commit 38fb806

Please sign in to comment.