mirror of https://github.com/gofiber/fiber.git
🐛 ErrorHandler & ctx.Routes()
parent
f7b63d4c9b
commit
1acc65bd36
70
app.go
70
app.go
|
@ -16,6 +16,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -26,7 +27,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version of current package
|
// Version of current package
|
||||||
const Version = "1.10.5"
|
const Version = "1.11.0"
|
||||||
|
|
||||||
// Map is a shortcut for map[string]interface{}, useful for JSON returns
|
// Map is a shortcut for map[string]interface{}, useful for JSON returns
|
||||||
type Map map[string]interface{}
|
type Map map[string]interface{}
|
||||||
|
@ -39,6 +40,8 @@ type App struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
// Route stack
|
// Route stack
|
||||||
stack [][]*Route
|
stack [][]*Route
|
||||||
|
// Amount of registered routes
|
||||||
|
routes int
|
||||||
// Ctx pool
|
// Ctx pool
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
// Fasthttp server
|
// Fasthttp server
|
||||||
|
@ -50,12 +53,12 @@ type App struct {
|
||||||
// Settings holds is a struct holding the server settings
|
// Settings holds is a struct holding the server settings
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
// Possible feature for v1.11.x
|
// Possible feature for v1.11.x
|
||||||
// // ErrorHandler is executed when you pass an error in the Next(err) method
|
// ErrorHandler is executed when you pass an error in the Next(err) method
|
||||||
// // This function is also executed when a panic occurs somewhere in the stack
|
// This function is also executed when middleware.Recover() catches a panic
|
||||||
// // Default: func(err error, ctx *fiber.Ctx) {
|
// Default: func(ctx *fiber.Ctx, err error) {
|
||||||
// // ctx.Status(500).Send(err.Error())
|
// ctx.Status(fiber.StatusBadRequest).SendString(err.Error())
|
||||||
// // }
|
// }
|
||||||
// ErrorHandler func(*Ctx, error)
|
ErrorHandler func(*Ctx, error)
|
||||||
|
|
||||||
// Enables the "Server: value" HTTP header.
|
// Enables the "Server: value" HTTP header.
|
||||||
// Default: ""
|
// Default: ""
|
||||||
|
@ -137,7 +140,6 @@ type Settings struct {
|
||||||
type Static struct {
|
type Static struct {
|
||||||
// This works differently than the github.com/gofiber/compression middleware
|
// This works differently than the github.com/gofiber/compression middleware
|
||||||
// The server tries minimizing CPU usage by caching compressed files.
|
// The server tries minimizing CPU usage by caching compressed files.
|
||||||
// It adds ".fiber.gz" suffix to the original file name.
|
|
||||||
// Optional. Default value false
|
// Optional. Default value false
|
||||||
Compress bool
|
Compress bool
|
||||||
|
|
||||||
|
@ -154,22 +156,28 @@ type Static struct {
|
||||||
Index string
|
Index string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: v1.11 Potential feature to get all registered routes
|
// Routes returns all registered routes
|
||||||
// func (app *App) Routes(print ...bool) map[string][]string {
|
//
|
||||||
// routes := make(map[string][]string)
|
// for _, r := range app.Routes() {
|
||||||
// for i := range app.stack {
|
// fmt.Printf("%s\t%s\n", r.Method, r.Path)
|
||||||
// method := intMethod[i]
|
|
||||||
// routes[method] = []string{}
|
|
||||||
// for k := range app.stack[i] {
|
|
||||||
// routes[method] = append(routes[method], app.stack[i][k].Path)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if len(print) > 0 && print[0] {
|
|
||||||
// b, _ := json.MarshalIndent(routes, "", " ")
|
|
||||||
// fmt.Print(string(b))
|
|
||||||
// }
|
|
||||||
// return routes
|
|
||||||
// }
|
// }
|
||||||
|
func (app *App) Routes() []*Route {
|
||||||
|
routes := make([]*Route, 0)
|
||||||
|
for m := range app.stack {
|
||||||
|
for r := range app.stack[m] {
|
||||||
|
// Ignore HEAD routes handling GET routes
|
||||||
|
if m == 1 && app.stack[m][r].Method == MethodGet {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
routes = append(routes, app.stack[m][r])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Sort routes by stack position
|
||||||
|
sort.Slice(routes, func(i, k int) bool {
|
||||||
|
return routes[i].pos < routes[k].pos
|
||||||
|
})
|
||||||
|
return routes
|
||||||
|
}
|
||||||
|
|
||||||
// New creates a new Fiber named instance.
|
// New creates a new Fiber named instance.
|
||||||
// You can pass optional settings when creating a new instance.
|
// You can pass optional settings when creating a new instance.
|
||||||
|
@ -190,9 +198,9 @@ func New(settings ...*Settings) *App {
|
||||||
BodyLimit: 4 * 1024 * 1024,
|
BodyLimit: 4 * 1024 * 1024,
|
||||||
Concurrency: 256 * 1024,
|
Concurrency: 256 * 1024,
|
||||||
// Possible feature for v1.11.x
|
// Possible feature for v1.11.x
|
||||||
// ErrorHandler: func(ctx *Ctx, err error) {
|
ErrorHandler: func(ctx *Ctx, err error) {
|
||||||
// ctx.Status(500).SendString(err.Error())
|
ctx.Status(StatusInternalServerError).SendString(err.Error())
|
||||||
// },
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// Overwrite settings if provided
|
// Overwrite settings if provided
|
||||||
|
@ -213,11 +221,11 @@ func New(settings ...*Settings) *App {
|
||||||
getString = getStringImmutable
|
getString = getStringImmutable
|
||||||
}
|
}
|
||||||
// Possible feature for v1.11.x
|
// Possible feature for v1.11.x
|
||||||
// if app.Settings.ErrorHandler == nil {
|
if app.Settings.ErrorHandler == nil {
|
||||||
// app.Settings.ErrorHandler = func(ctx *Ctx, err error) {
|
app.Settings.ErrorHandler = func(ctx *Ctx, err error) {
|
||||||
// ctx.Status(500).SendString(err.Error())
|
ctx.Status(StatusInternalServerError).SendString(err.Error())
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
// Initialize app
|
// Initialize app
|
||||||
return app.init()
|
return app.init()
|
||||||
|
|
Loading…
Reference in New Issue