Merge pull request #667 from Fenny/master

🧬 Update filesystem readme
This commit is contained in:
fenny 2020-07-23 06:21:16 +02:00 committed by GitHub
commit f852f08939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 218 additions and 24 deletions

View File

@ -1,8 +1,6 @@
package middleware
import (
"fmt"
fiber "github.com/gofiber/fiber"
fasthttp "github.com/valyala/fasthttp"
)
@ -58,12 +56,6 @@ func Compress(options ...interface{}) fiber.Handler {
return compress(config)
}
// CompressWithConfig is deprecated, please use Compress instead
func CompressWithConfig(config CompressConfig) fiber.Handler {
fmt.Println("compress: `CompressWithConfig()` is deprecated since v1.12.4, please use `Compress()`")
return compress(config)
}
func compress(config CompressConfig) fiber.Handler {
// Init middleware settings
var compressHandler fasthttp.RequestHandler

View File

@ -0,0 +1,150 @@
# FileSystem
FileSystem middleware for Fiber
### Example
The middleware packages comes with the official Fiber framework.
```go
import (
"github.com/gofiber/fiber"
"github.com/gofiber/fiber/middleware"
)
```
### Signature
```go
embed.New(config ...embed.Config) func(c *fiber.Ctx)
```
### Config
| Property | Type | Description | Default |
| :--- | :--- | :--- | :--- |
| Index | `string` | Index file name | `index.html` |
| Browse | `bool` | Enable directory browsing | `false` |
| Root | `http.FileSystem` | http.FileSystem to use | `nil` |
| ErrorHandler | `func(*fiber.Ctx, error)` | Error handler | `InternalServerError` |
### pkger
```go
package main
import (
"net/http"
"github.com/gofiber/fiber"
"github.com/gofiber/middleware/filesystem"
)
func main() {
app := fiber.New()
// Pass a FileSystem
app.Use("/assets", middleware.FileSystem(http.Dir("./assets")))
// Define the index file for serving a directory
app.Use("/assets", middleware.FileSystem(http.Dir("./assets"), "index.html"))
// Enable directory browsing
app.Use("/assets", middleware.FileSystem(http.Dir("./assets"), true))
// Pass a config
app.Use("/assets", middleware.FileSystem(middleware.FileSystemConfig{
Root: http.Dir("./assets"),
Index: "index.html",
Browse: true,
}))
app.Listen(8080)
}
```
### packr
```go
package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/middleware/filesystem"
"github.com/gobuffalo/packr/v2"
)
func main() {
app := fiber.New()
app.Use("/assets", middleware.FileSystem(packr.New("Assets Box", "/assets")))
app.Listen(8080)
}
```
### go.rice
```go
package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/middleware/filesystem"
"github.com/GeertJohan/go.rice"
)
func main() {
app := fiber.New()
app.Use("/assets", middleware.FileSystem(rice.MustFindBox("assets").HTTPBox()))
app.Listen(8080)
}
```
### fileb0x
```go
package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/middleware/filesystem"
"<Your go module>/myEmbeddedFiles"
)
func main() {
app := fiber.New()
app.Use("/assets", middleware.FileSystem(myEmbeddedFiles.HTTP))
app.Listen(8080)
}
```
### statik
```go
package main
import (
"log"
"github.com/gofiber/fiber"
"github.com/gofiber/middleware/filesystem"
"<Your go module>/statik"
fs "github.com/rakyll/statik/fs"
)
func main() {
statik, err := fs.New()
if err != nil {
log.Fatal(err)
}
app := fiber.New()
app.Use("/", middleware.FileSystem.New(statikFS))
app.Listen(8080)
}
```

View File

@ -185,12 +185,6 @@ func Logger(options ...interface{}) fiber.Handler {
return logger(config)
}
// LoggerWithConfig is deprecated, please use Logger instead
func LoggerWithConfig(config LoggerConfig) fiber.Handler {
fmt.Println("logger: `LoggerWithConfig()` is deprecated since v1.12.4, please use `Logger()`")
return logger(config)
}
func logger(config LoggerConfig) fiber.Handler {
// Set config default values
if config.Format == "" {

66
middleware/pprof.go Normal file
View File

@ -0,0 +1,66 @@
package middleware
import (
"net/http/pprof"
"strings"
fiber "github.com/gofiber/fiber"
fasthttpadaptor "github.com/valyala/fasthttp/fasthttpadaptor"
)
// Set pprof adaptors
var (
pprofIndex = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Index)
pprofCmdline = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Cmdline)
pprofProfile = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Profile)
pprofSymbol = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol)
pprofTrace = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace)
pprofAllocs = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("allocs").ServeHTTP)
pprofBlock = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("block").ServeHTTP)
pprofGoroutine = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("goroutine").ServeHTTP)
pprofHeap = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("heap").ServeHTTP)
pprofMutex = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("mutex").ServeHTTP)
pprofThreadcreate = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("threadcreate").ServeHTTP)
)
// Pprof will enabling profiling
func Pprof() fiber.Handler {
// Return handler
return func(c *fiber.Ctx) {
path := c.Path()
// We are only interested in /debug/pprof routes
if len(path) < 12 || !strings.HasPrefix(path, "/debug/pprof") {
c.Next()
return
}
// Switch to original path without stripped slashes
switch path {
case "/debug/pprof/":
c.Fasthttp.SetContentType(fiber.MIMETextHTML)
pprofIndex(c.Fasthttp)
case "/debug/pprof/cmdline":
pprofCmdline(c.Fasthttp)
case "/debug/pprof/profile":
pprofProfile(c.Fasthttp)
case "/debug/pprof/symbol":
pprofSymbol(c.Fasthttp)
case "/debug/pprof/trace":
pprofTrace(c.Fasthttp)
case "/debug/pprof/allocs":
pprofAllocs(c.Fasthttp)
case "/debug/pprof/block":
pprofBlock(c.Fasthttp)
case "/debug/pprof/goroutine":
pprofGoroutine(c.Fasthttp)
case "/debug/pprof/heap":
pprofHeap(c.Fasthttp)
case "/debug/pprof/mutex":
pprofMutex(c.Fasthttp)
case "/debug/pprof/threadcreate":
pprofThreadcreate(c.Fasthttp)
default:
// pprof index only works with trailing slash
c.Redirect("/debug/pprof/", 302)
}
}
}

View File

@ -1,8 +1,6 @@
package middleware
import (
"fmt"
fiber "github.com/gofiber/fiber"
utils "github.com/gofiber/utils"
)
@ -60,7 +58,7 @@ func RequestID(options ...interface{}) fiber.Handler {
case RequestIDConfig:
config = opt
default:
panic("RequestID: the following option types are allowed: string, func() string, func(*fiber.Ctx) bool, RequestIDConfig")
panic("RequestID: the following option types are allowed: `string`, `func() string`, `func(*fiber.Ctx) bool`, `RequestIDConfig`")
}
}
}
@ -68,12 +66,6 @@ func RequestID(options ...interface{}) fiber.Handler {
return requestID(config)
}
// RequestIDWithConfig is deprecated, please use RequestID instead
func RequestIDWithConfig(config RequestIDConfig) fiber.Handler {
fmt.Println("compress: `RequestIDWithConfig()` is deprecated since v1.12.4, please use `RequestID()`")
return requestID(config)
}
func requestID(config RequestIDConfig) fiber.Handler {
// Set default values
if config.Header == "" {

View File

@ -157,7 +157,7 @@ func Test_Middleware_RequestID_Skip(t *testing.T) {
func Test_Middleware_RequestID_Panic(t *testing.T) {
defer func() {
utils.AssertEqual(t,
"RequestID: the following option types are allowed: string, func() string, func(*fiber.Ctx) bool, RequestIDConfig",
"RequestID: the following option types are allowed: `string`, `func() string`, `func(*fiber.Ctx) bool`, `RequestIDConfig`",
fmt.Sprintf("%s", recover()))
}()