Skip to content

Commit

Permalink
Merge pull request #252 from Fenny/master
Browse files Browse the repository at this point in the history
v1.8.431
  • Loading branch information
Fenny authored Mar 31, 2020
2 parents f60c61c + 79c378c commit feb7c59
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
46 changes: 23 additions & 23 deletions fiber.go → app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const Version = "1.8.43"
// Map is a shortcut for map[string]interface{}
type Map map[string]interface{}

// Fiber denotes the Fiber application.
type Fiber struct {
// App denotes the Fiber application.
type App struct {
server *fasthttp.Server // FastHTTP server
routes []*Route // Route stack
Settings *Settings // Fiber settings
Expand Down Expand Up @@ -67,15 +67,15 @@ type Settings struct {
// Group struct
type Group struct {
prefix string
app *Fiber
app *App
}

// Global variables
var isPrefork, isChild bool

// New creates a new Fiber named instance.
// You can pass optional settings when creating a new instance.
func New(settings ...*Settings) *Fiber {
func New(settings ...*Settings) *App {
// Parse arguments
for _, v := range os.Args[1:] {
if v == "-prefork" {
Expand All @@ -85,7 +85,7 @@ func New(settings ...*Settings) *Fiber {
}
}
// Create app
app := new(Fiber)
app := new(App)
// Create settings
app.Settings = new(Settings)
// Set default settings
Expand All @@ -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.
func (app *Fiber) Group(prefix string, handlers ...func(*Ctx)) *Group {
func (app *App) Group(prefix string, handlers ...func(*Ctx)) *Group {
if len(handlers) > 0 {
app.registerMethod("USE", prefix, handlers...)
}
Expand Down Expand Up @@ -139,15 +139,15 @@ type Static struct {
}

// 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...)
return app
}

// Use registers a middleware route.
// Middleware matches requests beginning with the provided prefix.
// 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 handlers []func(*Ctx)
for i := 0; i < len(args); i++ {
Expand All @@ -165,61 +165,61 @@ func (app *Fiber) Use(args ...interface{}) *Fiber {
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}

// 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...)
return app
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func (grp *Group) All(path string, handlers ...func(*Ctx)) *Group {

// Listen serves HTTP requests from the given addr or port.
// 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)
if !ok {
port, ok := address.(int)
Expand Down Expand Up @@ -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.
//
// 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 {
return fmt.Errorf("Server is not running")
}
Expand All @@ -379,7 +379,7 @@ func (app *Fiber) Shutdown() error {

// Test is used for internal debugging by passing a *http.Request.
// 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
if len(msTimeout) > 0 {
timeout = msTimeout[0]
Expand Down Expand Up @@ -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/
func (app *Fiber) prefork(address string) (ln net.Listener, err error) {
func (app *App) prefork(address string) (ln net.Listener, err error) {
// Master proc
if !isChild {
addr, err := net.ResolveTCPAddr("tcp", address)
Expand Down Expand Up @@ -474,7 +474,7 @@ func (dl *disableLogger) Printf(format string, args ...interface{}) {
// fmt.Println(fmt.Sprintf(format, args...))
}

func (app *Fiber) newServer() *fasthttp.Server {
func (app *App) newServer() *fasthttp.Server {
return &fasthttp.Server{
Handler: app.handler,
Name: app.Settings.ServerHeader,
Expand Down
2 changes: 1 addition & 1 deletion fiber_test.go → app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

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"
if len(m) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// 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.
type Ctx struct {
app *Fiber // Reference to *Fiber
app *App // Reference to *App
route *Route // Reference to *Route
index int // Index of the current stack
method string // HTTP method
Expand Down Expand Up @@ -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,
// indicating that the request was issued by a client library (such as jQuery).
func (ctx *Ctx) XHR() bool {
return ctx.Get(HeaderXRequestedWith) == "XMLHttpRequest"
return strings.ToLower(ctx.Get(HeaderXRequestedWith)) == "xmlhttprequest"
}
8 changes: 4 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Route struct {
Handler func(*Ctx) // ctx handler
}

func (app *Fiber) nextRoute(ctx *Ctx) {
func (app *App) nextRoute(ctx *Ctx) {
// Keep track of head matches
lenr := len(app.routes) - 1
for ctx.index < lenr {
Expand Down Expand Up @@ -101,7 +101,7 @@ func (r *Route) matchRoute(method, path string) (match bool, values []string) {
return false, values
}

func (app *Fiber) handler(fctx *fasthttp.RequestCtx) {
func (app *App) handler(fctx *fasthttp.RequestCtx) {
// get fiber context from sync pool
ctx := acquireCtx(fctx)
defer releaseCtx(ctx)
Expand All @@ -120,7 +120,7 @@ func (app *Fiber) handler(fctx *fasthttp.RequestCtx) {
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
if len(handlers) == 0 {
log.Fatalf("Missing handler in route")
Expand Down Expand Up @@ -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
if prefix == "" {
prefix = "/"
Expand Down

0 comments on commit feb7c59

Please sign in to comment.