mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
Update typos
This commit is contained in:
parent
dbf3375aee
commit
7041f051e1
@ -116,9 +116,10 @@ app.Post(...)
|
|||||||
app.Put(...)
|
app.Put(...)
|
||||||
app.Trace(...)
|
app.Trace(...)
|
||||||
|
|
||||||
// Matches all HTTP verbs, Use refers to All
|
// Both All & Use matches all kind of HTTP request
|
||||||
app.All(...)
|
// But there is a big difference
|
||||||
app.Use(...)
|
app.All(...) // Will match complete path with :params support
|
||||||
|
app.Use(...) // Will only see wheter url starts with specified path without :params support
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Static
|
#### Static
|
||||||
@ -171,7 +172,6 @@ app.Listen("127.0.0.1:8080")
|
|||||||
|
|
||||||
// Enable TLS/HTTPS
|
// Enable TLS/HTTPS
|
||||||
app.Listen(443, "server.crt", "server.key")
|
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.
|
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
|
```go
|
||||||
app := fiber.New()
|
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) {
|
app.Use(func(c *fiber.Ctx) {
|
||||||
// Set some security headers
|
// Set some security headers
|
||||||
c.Set("X-XSS-Protection", "1; mode=block")
|
c.Set("X-XSS-Protection", "1; mode=block")
|
||||||
|
@ -61,11 +61,10 @@ func (r *Fiber) Get(args ...interface{}) {
|
|||||||
|
|
||||||
// All matches any HTTP method
|
// All matches any HTTP method
|
||||||
func (r *Fiber) All(args ...interface{}) {
|
func (r *Fiber) All(args ...interface{}) {
|
||||||
r.register("*", args...)
|
r.register("ALL", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use is another name for All()
|
// Use only matches the starting path
|
||||||
// People using Expressjs are used to this
|
|
||||||
func (r *Fiber) Use(args ...interface{}) {
|
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
|
package fiber
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
@ -18,6 +20,8 @@ type route struct {
|
|||||||
method string
|
method string
|
||||||
// Stores the orignal path
|
// Stores the orignal path
|
||||||
path string
|
path string
|
||||||
|
// Bool that defines if the route is a Use()
|
||||||
|
use bool
|
||||||
// wildcard bool is for routes without a path, * and /*
|
// wildcard bool is for routes without a path, * and /*
|
||||||
wildcard bool
|
wildcard bool
|
||||||
// Stores compiled regex special routes :params, *wildcards, optionals?
|
// Stores compiled regex special routes :params, *wildcards, optionals?
|
||||||
@ -30,6 +34,12 @@ type route struct {
|
|||||||
|
|
||||||
// Function to add a route correctly
|
// Function to add a route correctly
|
||||||
func (r *Fiber) register(method string, args ...interface{}) {
|
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
|
// Prepare possible variables
|
||||||
var path string // We could have a path/prefix
|
var path string // We could have a path/prefix
|
||||||
var handler func(*Ctx) // We could have a ctx handler
|
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 '*'")
|
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 the route needs to match any path
|
||||||
if path == "" || path == "*" || 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
|
return
|
||||||
}
|
}
|
||||||
// Get params from path
|
// Get params from path
|
||||||
params := getParams(path)
|
params := getParams(path)
|
||||||
// If path has no params (simple path), we dont need regex
|
// If path has no params (simple path), we dont need regex (also for use())
|
||||||
if len(params) == 0 {
|
if use || len(params) == 0 {
|
||||||
r.routes = append(r.routes, &route{method, path, false, nil, nil, handler})
|
r.routes = append(r.routes, &route{method, path, use, false, nil, nil, handler})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +79,7 @@ func (r *Fiber) register(method string, args ...interface{}) {
|
|||||||
panic("Invalid url pattern: " + path)
|
panic("Invalid url pattern: " + path)
|
||||||
}
|
}
|
||||||
// Add regex + params to route
|
// 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.
|
// 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 {
|
if route.method != "*" && route.method != method {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// First check if we match a static path or wildcard
|
// First check if we match a wildcard or static path
|
||||||
if route.wildcard || (route.path == path && route.params == nil) {
|
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 * always set the path to the wildcard parameter
|
||||||
if route.wildcard {
|
if route.wildcard {
|
||||||
ctx.params = &[]string{"*"}
|
ctx.params = &[]string{"*"}
|
||||||
@ -100,6 +118,19 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
// continue to go to the next route
|
// continue to go to the next route
|
||||||
continue
|
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
|
// Skip route if regex does not exist
|
||||||
if route.regex == nil {
|
if route.regex == nil {
|
||||||
continue
|
continue
|
||||||
@ -109,7 +140,7 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// If we have parameters, lets find the matches
|
// 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)
|
matches := route.regex.FindAllStringSubmatch(path, -1)
|
||||||
// If we have matches, add params and values to context
|
// If we have matches, add params and values to context
|
||||||
if len(matches) > 0 && len(matches[0]) > 1 {
|
if len(matches) > 0 && len(matches[0]) > 1 {
|
||||||
|
@ -51,12 +51,12 @@ func (r *Fiber) Static(args ...string) {
|
|||||||
filePath := file
|
filePath := file
|
||||||
// If the file is an index.html, bind the prefix to index.html directly
|
// If the file is an index.html, bind the prefix to index.html directly
|
||||||
if filepath.Base(filePath) == "index.html" {
|
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)
|
c.SendFile(filePath, gzip)
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
// Add the route + SendFile(filepath) to routes
|
// 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)
|
c.SendFile(filePath, gzip)
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user