mirror of
https://github.com/gofiber/fiber.git
synced 2025-04-27 13:14:31 +00:00
v1.8.431
This commit is contained in:
parent
48dd9846c6
commit
79c378c384
@ -29,8 +29,8 @@ const Version = "1.8.43"
|
|||||||
// Map is a shortcut for map[string]interface{}
|
// Map is a shortcut for map[string]interface{}
|
||||||
type Map map[string]interface{}
|
type Map map[string]interface{}
|
||||||
|
|
||||||
// Fiber denotes the Fiber application.
|
// App denotes the Fiber application.
|
||||||
type Fiber struct {
|
type App struct {
|
||||||
server *fasthttp.Server // FastHTTP server
|
server *fasthttp.Server // FastHTTP server
|
||||||
routes []*Route // Route stack
|
routes []*Route // Route stack
|
||||||
Settings *Settings // Fiber settings
|
Settings *Settings // Fiber settings
|
||||||
@ -67,7 +67,7 @@ type Settings struct {
|
|||||||
// Group struct
|
// Group struct
|
||||||
type Group struct {
|
type Group struct {
|
||||||
prefix string
|
prefix string
|
||||||
app *Fiber
|
app *App
|
||||||
}
|
}
|
||||||
|
|
||||||
// Global variables
|
// Global variables
|
||||||
@ -75,7 +75,7 @@ var isPrefork, isChild bool
|
|||||||
|
|
||||||
// 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.
|
||||||
func New(settings ...*Settings) *Fiber {
|
func New(settings ...*Settings) *App {
|
||||||
// Parse arguments
|
// Parse arguments
|
||||||
for _, v := range os.Args[1:] {
|
for _, v := range os.Args[1:] {
|
||||||
if v == "-prefork" {
|
if v == "-prefork" {
|
||||||
@ -85,7 +85,7 @@ func New(settings ...*Settings) *Fiber {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Create app
|
// Create app
|
||||||
app := new(Fiber)
|
app := new(App)
|
||||||
// Create settings
|
// Create settings
|
||||||
app.Settings = new(Settings)
|
app.Settings = new(Settings)
|
||||||
// Set default settings
|
// Set default settings
|
||||||
@ -109,7 +109,7 @@ func New(settings ...*Settings) *Fiber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
|
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
|
||||||
func (app *Fiber) Group(prefix string, handlers ...func(*Ctx)) *Group {
|
func (app *App) Group(prefix string, handlers ...func(*Ctx)) *Group {
|
||||||
if len(handlers) > 0 {
|
if len(handlers) > 0 {
|
||||||
app.registerMethod("USE", prefix, handlers...)
|
app.registerMethod("USE", prefix, handlers...)
|
||||||
}
|
}
|
||||||
@ -139,7 +139,7 @@ type Static struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Static registers a new route with path prefix to serve static files from the provided root directory.
|
// Static registers a new route with path prefix to serve static files from the provided root directory.
|
||||||
func (app *Fiber) Static(prefix, root string, config ...Static) *Fiber {
|
func (app *App) Static(prefix, root string, config ...Static) *App {
|
||||||
app.registerStatic(prefix, root, config...)
|
app.registerStatic(prefix, root, config...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
@ -147,7 +147,7 @@ func (app *Fiber) Static(prefix, root string, config ...Static) *Fiber {
|
|||||||
// Use registers a middleware route.
|
// Use registers a middleware route.
|
||||||
// Middleware matches requests beginning with the provided prefix.
|
// Middleware matches requests beginning with the provided prefix.
|
||||||
// Providing a prefix is optional, it defaults to "/"
|
// Providing a prefix is optional, it defaults to "/"
|
||||||
func (app *Fiber) Use(args ...interface{}) *Fiber {
|
func (app *App) Use(args ...interface{}) *App {
|
||||||
var path = ""
|
var path = ""
|
||||||
var handlers []func(*Ctx)
|
var handlers []func(*Ctx)
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
@ -165,61 +165,61 @@ func (app *Fiber) Use(args ...interface{}) *Fiber {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect : https://fiber.wiki/application#http-methods
|
// Connect : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Connect(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Connect(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodConnect, path, handlers...)
|
app.registerMethod(MethodConnect, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put : https://fiber.wiki/application#http-methods
|
// Put : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Put(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Put(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodPut, path, handlers...)
|
app.registerMethod(MethodPut, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post : https://fiber.wiki/application#http-methods
|
// Post : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Post(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Post(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodPost, path, handlers...)
|
app.registerMethod(MethodPost, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete : https://fiber.wiki/application#http-methods
|
// Delete : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Delete(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Delete(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodDelete, path, handlers...)
|
app.registerMethod(MethodDelete, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head : https://fiber.wiki/application#http-methods
|
// Head : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Head(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Head(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodHead, path, handlers...)
|
app.registerMethod(MethodHead, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch : https://fiber.wiki/application#http-methods
|
// Patch : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Patch(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Patch(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodPatch, path, handlers...)
|
app.registerMethod(MethodPatch, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options : https://fiber.wiki/application#http-methods
|
// Options : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Options(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Options(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodOptions, path, handlers...)
|
app.registerMethod(MethodOptions, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trace : https://fiber.wiki/application#http-methods
|
// Trace : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Trace(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Trace(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodTrace, path, handlers...)
|
app.registerMethod(MethodTrace, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get : https://fiber.wiki/application#http-methods
|
// Get : https://fiber.wiki/application#http-methods
|
||||||
func (app *Fiber) Get(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) Get(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod(MethodGet, path, handlers...)
|
app.registerMethod(MethodGet, path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// All matches all HTTP methods and complete paths
|
// All matches all HTTP methods and complete paths
|
||||||
func (app *Fiber) All(path string, handlers ...func(*Ctx)) *Fiber {
|
func (app *App) All(path string, handlers ...func(*Ctx)) *App {
|
||||||
app.registerMethod("ALL", path, handlers...)
|
app.registerMethod("ALL", path, handlers...)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
@ -325,7 +325,7 @@ func (grp *Group) All(path string, handlers ...func(*Ctx)) *Group {
|
|||||||
|
|
||||||
// Listen serves HTTP requests from the given addr or port.
|
// Listen serves HTTP requests from the given addr or port.
|
||||||
// You can pass an optional *tls.Config to enable TLS.
|
// You can pass an optional *tls.Config to enable TLS.
|
||||||
func (app *Fiber) Listen(address interface{}, tlsconfig ...*tls.Config) error {
|
func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error {
|
||||||
addr, ok := address.(string)
|
addr, ok := address.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
port, ok := address.(int)
|
port, ok := address.(int)
|
||||||
@ -370,7 +370,7 @@ func (app *Fiber) Listen(address interface{}, tlsconfig ...*tls.Config) error {
|
|||||||
// Make sure the program doesn't exit and waits instead for Shutdown to return.
|
// Make sure the program doesn't exit and waits instead for Shutdown to return.
|
||||||
//
|
//
|
||||||
// Shutdown does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
|
// Shutdown does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
|
||||||
func (app *Fiber) Shutdown() error {
|
func (app *App) Shutdown() error {
|
||||||
if app.server == nil {
|
if app.server == nil {
|
||||||
return fmt.Errorf("Server is not running")
|
return fmt.Errorf("Server is not running")
|
||||||
}
|
}
|
||||||
@ -379,7 +379,7 @@ func (app *Fiber) Shutdown() error {
|
|||||||
|
|
||||||
// Test is used for internal debugging by passing a *http.Request.
|
// Test is used for internal debugging by passing a *http.Request.
|
||||||
// Timeout is optional and defaults to 200ms, -1 will disable it completely.
|
// Timeout is optional and defaults to 200ms, -1 will disable it completely.
|
||||||
func (app *Fiber) Test(request *http.Request, msTimeout ...int) (*http.Response, error) {
|
func (app *App) Test(request *http.Request, msTimeout ...int) (*http.Response, error) {
|
||||||
timeout := 200
|
timeout := 200
|
||||||
if len(msTimeout) > 0 {
|
if len(msTimeout) > 0 {
|
||||||
timeout = msTimeout[0]
|
timeout = msTimeout[0]
|
||||||
@ -426,7 +426,7 @@ func (app *Fiber) Test(request *http.Request, msTimeout ...int) (*http.Response,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sharding: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
|
// Sharding: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
|
||||||
func (app *Fiber) prefork(address string) (ln net.Listener, err error) {
|
func (app *App) prefork(address string) (ln net.Listener, err error) {
|
||||||
// Master proc
|
// Master proc
|
||||||
if !isChild {
|
if !isChild {
|
||||||
addr, err := net.ResolveTCPAddr("tcp", address)
|
addr, err := net.ResolveTCPAddr("tcp", address)
|
||||||
@ -474,7 +474,7 @@ func (dl *disableLogger) Printf(format string, args ...interface{}) {
|
|||||||
// fmt.Println(fmt.Sprintf(format, args...))
|
// fmt.Println(fmt.Sprintf(format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Fiber) newServer() *fasthttp.Server {
|
func (app *App) newServer() *fasthttp.Server {
|
||||||
return &fasthttp.Server{
|
return &fasthttp.Server{
|
||||||
Handler: app.handler,
|
Handler: app.handler,
|
||||||
Name: app.Settings.ServerHeader,
|
Name: app.Settings.ServerHeader,
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
var handler = func(c *Ctx) {}
|
var handler = func(c *Ctx) {}
|
||||||
|
|
||||||
func is200(t *testing.T, app *Fiber, url string, m ...string) {
|
func is200(t *testing.T, app *App, url string, m ...string) {
|
||||||
|
|
||||||
method := "GET"
|
method := "GET"
|
||||||
if len(m) > 0 {
|
if len(m) > 0 {
|
4
ctx.go
4
ctx.go
@ -28,7 +28,7 @@ import (
|
|||||||
// Ctx represents the Context which hold the HTTP request and response.
|
// Ctx represents the Context which hold the HTTP request and response.
|
||||||
// It has methods for the request query string, parameters, body, HTTP headers and so on.
|
// It has methods for the request query string, parameters, body, HTTP headers and so on.
|
||||||
type Ctx struct {
|
type Ctx struct {
|
||||||
app *Fiber // Reference to *Fiber
|
app *App // Reference to *App
|
||||||
route *Route // Reference to *Route
|
route *Route // Reference to *Route
|
||||||
index int // Index of the current stack
|
index int // Index of the current stack
|
||||||
method string // HTTP method
|
method string // HTTP method
|
||||||
@ -831,5 +831,5 @@ func (ctx *Ctx) Write(bodies ...interface{}) {
|
|||||||
// XHR returns a Boolean property, that is true, if the request’s X-Requested-With header field is XMLHttpRequest,
|
// XHR returns a Boolean property, that is true, if the request’s X-Requested-With header field is XMLHttpRequest,
|
||||||
// indicating that the request was issued by a client library (such as jQuery).
|
// indicating that the request was issued by a client library (such as jQuery).
|
||||||
func (ctx *Ctx) XHR() bool {
|
func (ctx *Ctx) XHR() bool {
|
||||||
return ctx.Get(HeaderXRequestedWith) == "XMLHttpRequest"
|
return strings.ToLower(ctx.Get(HeaderXRequestedWith)) == "xmlhttprequest"
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ type Route struct {
|
|||||||
Handler func(*Ctx) // ctx handler
|
Handler func(*Ctx) // ctx handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Fiber) nextRoute(ctx *Ctx) {
|
func (app *App) nextRoute(ctx *Ctx) {
|
||||||
// Keep track of head matches
|
// Keep track of head matches
|
||||||
lenr := len(app.routes) - 1
|
lenr := len(app.routes) - 1
|
||||||
for ctx.index < lenr {
|
for ctx.index < lenr {
|
||||||
@ -101,7 +101,7 @@ func (r *Route) matchRoute(method, path string) (match bool, values []string) {
|
|||||||
return false, values
|
return false, values
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
func (app *App) handler(fctx *fasthttp.RequestCtx) {
|
||||||
// get fiber context from sync pool
|
// get fiber context from sync pool
|
||||||
ctx := acquireCtx(fctx)
|
ctx := acquireCtx(fctx)
|
||||||
defer releaseCtx(ctx)
|
defer releaseCtx(ctx)
|
||||||
@ -120,7 +120,7 @@ func (app *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
app.nextRoute(ctx)
|
app.nextRoute(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Fiber) registerMethod(method, path string, handlers ...func(*Ctx)) {
|
func (app *App) registerMethod(method, path string, handlers ...func(*Ctx)) {
|
||||||
// Route requires atleast one handler
|
// Route requires atleast one handler
|
||||||
if len(handlers) == 0 {
|
if len(handlers) == 0 {
|
||||||
log.Fatalf("Missing handler in route")
|
log.Fatalf("Missing handler in route")
|
||||||
@ -185,7 +185,7 @@ func (app *Fiber) registerMethod(method, path string, handlers ...func(*Ctx)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Fiber) registerStatic(prefix, root string, config ...Static) {
|
func (app *App) registerStatic(prefix, root string, config ...Static) {
|
||||||
// Cannot have an empty prefix
|
// Cannot have an empty prefix
|
||||||
if prefix == "" {
|
if prefix == "" {
|
||||||
prefix = "/"
|
prefix = "/"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user