mirror of https://github.com/gofiber/fiber.git
Update typos
parent
dbf3375aee
commit
7041f051e1
|
@ -116,9 +116,10 @@ app.Post(...)
|
|||
app.Put(...)
|
||||
app.Trace(...)
|
||||
|
||||
// Matches all HTTP verbs, Use refers to All
|
||||
app.All(...)
|
||||
app.Use(...)
|
||||
// Both All & Use matches all kind of HTTP request
|
||||
// But there is a big difference
|
||||
app.All(...) // Will match complete path with :params support
|
||||
app.Use(...) // Will only see wheter url starts with specified path without :params support
|
||||
```
|
||||
|
||||
#### Static
|
||||
|
@ -171,7 +172,6 @@ app.Listen("127.0.0.1:8080")
|
|||
|
||||
// Enable TLS/HTTPS
|
||||
app.Listen(443, "server.crt", "server.key")
|
||||
app.Listen("127.0.0.1:443", "server.crt", "server.key")
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -77,8 +77,11 @@ Functions that are designed to make changes to the request or response are calle
|
|||
|
||||
Here is a simple example of a middleware function that sets some response headers when a request to the app passes through it.
|
||||
|
||||
If you are not sure when to use **All()** vs **Use()**, read about the [Methods API here](/application/#methods)
|
||||
|
||||
```go
|
||||
app := fiber.New()
|
||||
// Use method path is a "mount" or "prefix" path and limits the middleware to only apply to any paths requested that begin with it. This means you cannot use :params on the Use method
|
||||
app.Use(func(c *fiber.Ctx) {
|
||||
// Set some security headers
|
||||
c.Set("X-XSS-Protection", "1; mode=block")
|
||||
|
|
|
@ -61,11 +61,10 @@ func (r *Fiber) Get(args ...interface{}) {
|
|||
|
||||
// All matches any HTTP method
|
||||
func (r *Fiber) All(args ...interface{}) {
|
||||
r.register("*", args...)
|
||||
r.register("ALL", args...)
|
||||
}
|
||||
|
||||
// Use is another name for All()
|
||||
// People using Expressjs are used to this
|
||||
// Use only matches the starting path
|
||||
func (r *Fiber) Use(args ...interface{}) {
|
||||
r.All(args...)
|
||||
r.register("USE", args...)
|
||||
}
|
||||
|
|
47
router.go
47
router.go
|
@ -8,7 +8,9 @@
|
|||
package fiber
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
@ -18,6 +20,8 @@ type route struct {
|
|||
method string
|
||||
// Stores the orignal path
|
||||
path string
|
||||
// Bool that defines if the route is a Use()
|
||||
use bool
|
||||
// wildcard bool is for routes without a path, * and /*
|
||||
wildcard bool
|
||||
// Stores compiled regex special routes :params, *wildcards, optionals?
|
||||
|
@ -30,6 +34,12 @@ type route struct {
|
|||
|
||||
// Function to add a route correctly
|
||||
func (r *Fiber) register(method string, args ...interface{}) {
|
||||
// Set if method is Use()
|
||||
var use = method == "USE"
|
||||
// Match any method
|
||||
if method == "ALL" || method == "USE" {
|
||||
method = "*"
|
||||
}
|
||||
// Prepare possible variables
|
||||
var path string // We could have a path/prefix
|
||||
var handler func(*Ctx) // We could have a ctx handler
|
||||
|
@ -43,16 +53,23 @@ func (r *Fiber) register(method string, args ...interface{}) {
|
|||
panic("Invalid path, must begin with slash '/' or wildcard '*'")
|
||||
}
|
||||
}
|
||||
if use && strings.Contains(path, "/:") {
|
||||
panic("You cannot use :params in Use()")
|
||||
}
|
||||
// If Use() path == "/", match anything aka *
|
||||
if use && path == "/" {
|
||||
path = "*"
|
||||
}
|
||||
// If the route needs to match any path
|
||||
if path == "" || path == "*" || path == "/*" {
|
||||
r.routes = append(r.routes, &route{method, path, true, nil, nil, handler})
|
||||
r.routes = append(r.routes, &route{method, path, use, true, nil, nil, handler})
|
||||
return
|
||||
}
|
||||
// Get params from path
|
||||
params := getParams(path)
|
||||
// If path has no params (simple path), we dont need regex
|
||||
if len(params) == 0 {
|
||||
r.routes = append(r.routes, &route{method, path, false, nil, nil, handler})
|
||||
// If path has no params (simple path), we dont need regex (also for use())
|
||||
if use || len(params) == 0 {
|
||||
r.routes = append(r.routes, &route{method, path, use, false, nil, nil, handler})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -62,7 +79,7 @@ func (r *Fiber) register(method string, args ...interface{}) {
|
|||
panic("Invalid url pattern: " + path)
|
||||
}
|
||||
// Add regex + params to route
|
||||
r.routes = append(r.routes, &route{method, path, false, regex, params, handler})
|
||||
r.routes = append(r.routes, &route{method, path, use, false, regex, params, handler})
|
||||
}
|
||||
|
||||
// then try to match a route as efficient as possible.
|
||||
|
@ -79,8 +96,9 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||
if route.method != "*" && route.method != method {
|
||||
continue
|
||||
}
|
||||
// First check if we match a static path or wildcard
|
||||
if route.wildcard || (route.path == path && route.params == nil) {
|
||||
// First check if we match a wildcard or static path
|
||||
if route.wildcard || route.path == path {
|
||||
// if route.wildcard || (route.path == path && route.params == nil) {
|
||||
// If * always set the path to the wildcard parameter
|
||||
if route.wildcard {
|
||||
ctx.params = &[]string{"*"}
|
||||
|
@ -100,6 +118,19 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||
// continue to go to the next route
|
||||
continue
|
||||
}
|
||||
// If route is Use() and path starts with route.path
|
||||
// aka strings.HasPrefix(route.path, path)
|
||||
if route.use && strings.HasPrefix(path, route.path) {
|
||||
fmt.Println(ctx.params)
|
||||
found = true
|
||||
ctx.route = route
|
||||
route.handler(ctx)
|
||||
if !ctx.next {
|
||||
break
|
||||
}
|
||||
ctx.next = false
|
||||
continue
|
||||
}
|
||||
// Skip route if regex does not exist
|
||||
if route.regex == nil {
|
||||
continue
|
||||
|
@ -109,7 +140,7 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||
continue
|
||||
}
|
||||
// If we have parameters, lets find the matches
|
||||
if route.params != nil && len(route.params) > 0 {
|
||||
if len(route.params) > 0 {
|
||||
matches := route.regex.FindAllStringSubmatch(path, -1)
|
||||
// If we have matches, add params and values to context
|
||||
if len(matches) > 0 && len(matches[0]) > 1 {
|
||||
|
|
|
@ -51,12 +51,12 @@ func (r *Fiber) Static(args ...string) {
|
|||
filePath := file
|
||||
// If the file is an index.html, bind the prefix to index.html directly
|
||||
if filepath.Base(filePath) == "index.html" {
|
||||
r.routes = append(r.routes, &route{"GET", prefix, wildcard, nil, nil, func(c *Ctx) {
|
||||
r.routes = append(r.routes, &route{"GET", prefix, wildcard, false, nil, nil, func(c *Ctx) {
|
||||
c.SendFile(filePath, gzip)
|
||||
}})
|
||||
}
|
||||
// Add the route + SendFile(filepath) to routes
|
||||
r.routes = append(r.routes, &route{"GET", path, wildcard, nil, nil, func(c *Ctx) {
|
||||
r.routes = append(r.routes, &route{"GET", path, wildcard, false, nil, nil, func(c *Ctx) {
|
||||
c.SendFile(filePath, gzip)
|
||||
}})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue