mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
v1.1.0 🎉 100 Stars 🌟
This commit is contained in:
parent
839080a76d
commit
9ff149324e
@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// Version for debugging
|
// Version for debugging
|
||||||
Version = "1.0.2"
|
Version = "1.1.0"
|
||||||
// https://play.golang.org/p/r6GNeV1gbH
|
// https://play.golang.org/p/r6GNeV1gbH
|
||||||
banner = "" +
|
banner = "" +
|
||||||
" \x1b[1;32m _____ _ _\n" +
|
" \x1b[1;32m _____ _ _\n" +
|
||||||
|
@ -35,7 +35,7 @@ type Cookie struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ctx pool
|
// Ctx pool
|
||||||
var ctxPool = sync.Pool{
|
var poolCtx = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return new(Ctx)
|
return new(Ctx)
|
||||||
},
|
},
|
||||||
@ -43,7 +43,7 @@ var ctxPool = sync.Pool{
|
|||||||
|
|
||||||
// Get new Ctx from pool
|
// Get new Ctx from pool
|
||||||
func acquireCtx(fctx *fasthttp.RequestCtx) *Ctx {
|
func acquireCtx(fctx *fasthttp.RequestCtx) *Ctx {
|
||||||
ctx := ctxPool.Get().(*Ctx)
|
ctx := poolCtx.Get().(*Ctx)
|
||||||
ctx.Fasthttp = fctx
|
ctx.Fasthttp = fctx
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
@ -55,5 +55,5 @@ func releaseCtx(ctx *Ctx) {
|
|||||||
ctx.params = nil
|
ctx.params = nil
|
||||||
ctx.values = nil
|
ctx.values = nil
|
||||||
ctx.Fasthttp = nil
|
ctx.Fasthttp = nil
|
||||||
ctxPool.Put(ctx)
|
poolCtx.Put(ctx)
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,8 @@ app.Use(...) // Will only see wheter url starts with specified path without :par
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Static
|
#### Static
|
||||||
To serve static files such as images, CSS files, and JavaScript files, replace your function handler with a file or directory string.
|
To serve static files such as images, CSS files, and JavaScript files, replace your function handler with a file or directory string.
|
||||||
|
By default this method will send `index.html` files in response to a request on a directory.
|
||||||
```go
|
```go
|
||||||
// Function signature
|
// Function signature
|
||||||
app.Static(root string)
|
app.Static(root string)
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
window.$docsify = {
|
window.$docsify = {
|
||||||
name: 'Fiber v1.0.2',
|
name: 'Fiber v1.1.0',
|
||||||
repo: 'gofiber/fiber',
|
repo: 'gofiber/fiber',
|
||||||
loadSidebar: "sidebar.md",
|
loadSidebar: "sidebar.md",
|
||||||
homepage: 'getting_started.md',
|
homepage: 'getting_started.md',
|
||||||
|
@ -66,5 +66,5 @@ func (r *Fiber) All(args ...interface{}) {
|
|||||||
|
|
||||||
// Use only matches the starting path
|
// Use only matches the starting path
|
||||||
func (r *Fiber) Use(args ...interface{}) {
|
func (r *Fiber) Use(args ...interface{}) {
|
||||||
r.register("USE", args...)
|
r.register("MIDWARE", args...)
|
||||||
}
|
}
|
||||||
|
20
request.go
20
request.go
@ -11,7 +11,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"mime"
|
"mime"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
@ -316,23 +315,8 @@ func (ctx *Ctx) Range() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Route : https://gofiber.github.io/fiber/#/context?id=route
|
// Route : https://gofiber.github.io/fiber/#/context?id=route
|
||||||
func (ctx *Ctx) Route() (s struct {
|
func (ctx *Ctx) Route() *route {
|
||||||
Method string
|
return ctx.route
|
||||||
Path string
|
|
||||||
Wildcard bool
|
|
||||||
Regex *regexp.Regexp
|
|
||||||
Params []string
|
|
||||||
Values []string
|
|
||||||
Handler func(*Ctx)
|
|
||||||
}) {
|
|
||||||
s.Method = ctx.route.method
|
|
||||||
s.Path = ctx.route.path
|
|
||||||
s.Wildcard = ctx.route.wildcard
|
|
||||||
s.Regex = ctx.route.regex
|
|
||||||
s.Params = ctx.route.params
|
|
||||||
s.Values = ctx.values
|
|
||||||
s.Handler = ctx.route.handler
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveFile : https://gofiber.github.io/fiber/#/context?id=secure
|
// SaveFile : https://gofiber.github.io/fiber/#/context?id=secure
|
||||||
|
58
router.go
58
router.go
@ -17,27 +17,27 @@ import (
|
|||||||
|
|
||||||
type route struct {
|
type route struct {
|
||||||
// HTTP method in uppercase, can be a * for Use() & All()
|
// HTTP method in uppercase, can be a * for Use() & All()
|
||||||
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()
|
// Bool that defines if the route is a Use() middleware
|
||||||
use bool
|
Midware 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?
|
||||||
regex *regexp.Regexp
|
Regex *regexp.Regexp
|
||||||
// Store params if special routes :params, *wildcards, optionals?
|
// Store params if special routes :params, *wildcards, optionals?
|
||||||
params []string
|
Params []string
|
||||||
// Callback function for specific route
|
// Callback function for specific route
|
||||||
handler func(*Ctx)
|
Handler func(*Ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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()
|
// Set if method is Use() midware
|
||||||
var use = method == "USE"
|
var midware = method == "MIDWARE"
|
||||||
// Match any method
|
// Match any method
|
||||||
if method == "ALL" || method == "USE" {
|
if method == "ALL" || midware {
|
||||||
method = "*"
|
method = "*"
|
||||||
}
|
}
|
||||||
// Prepare possible variables
|
// Prepare possible variables
|
||||||
@ -53,23 +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, "/:") {
|
if midware && strings.Contains(path, "/:") {
|
||||||
panic("You cannot use :params in Use()")
|
panic("You cannot use :params in Use()")
|
||||||
}
|
}
|
||||||
// If Use() path == "/", match anything aka *
|
// If Use() path == "/", match anything aka *
|
||||||
if use && path == "/" {
|
if midware && path == "/" {
|
||||||
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, use, true, nil, nil, handler})
|
r.routes = append(r.routes, &route{method, path, midware, 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 (also for use())
|
// If path has no params (simple path), we dont need regex (also for use())
|
||||||
if use || len(params) == 0 {
|
if midware || len(params) == 0 {
|
||||||
r.routes = append(r.routes, &route{method, path, use, false, nil, nil, handler})
|
r.routes = append(r.routes, &route{method, path, midware, false, nil, nil, handler})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,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, use, false, regex, params, handler})
|
r.routes = append(r.routes, &route{method, path, midware, false, regex, params, handler})
|
||||||
}
|
}
|
||||||
|
|
||||||
// then try to match a route as efficient as possible.
|
// then try to match a route as efficient as possible.
|
||||||
@ -93,14 +93,14 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
// loop trough routes
|
// loop trough routes
|
||||||
for _, route := range r.routes {
|
for _, route := range r.routes {
|
||||||
// Skip route if method is not allowed
|
// Skip route if method is not allowed
|
||||||
if route.method != "*" && route.method != method {
|
if route.Method != "*" && route.Method != method {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// First check if we match a wildcard or static path
|
// First check if we match a wildcard or static path
|
||||||
if route.wildcard || route.path == path {
|
if route.Wildcard || route.Path == path {
|
||||||
// if route.wildcard || (route.path == path && route.params == nil) {
|
// 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{"*"}
|
||||||
ctx.values = []string{path}
|
ctx.values = []string{path}
|
||||||
}
|
}
|
||||||
@ -108,7 +108,7 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
// Set route pointer if user wants to call .Route()
|
// Set route pointer if user wants to call .Route()
|
||||||
ctx.route = route
|
ctx.route = route
|
||||||
// Execute handler with context
|
// Execute handler with context
|
||||||
route.handler(ctx)
|
route.Handler(ctx)
|
||||||
// if next is not set, leave loop and release ctx
|
// if next is not set, leave loop and release ctx
|
||||||
if !ctx.next {
|
if !ctx.next {
|
||||||
break
|
break
|
||||||
@ -120,11 +120,11 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
}
|
}
|
||||||
// If route is Use() and path starts with route.path
|
// If route is Use() and path starts with route.path
|
||||||
// aka strings.HasPrefix(route.path, path)
|
// aka strings.HasPrefix(route.path, path)
|
||||||
if route.use && strings.HasPrefix(path, route.path) {
|
if route.Midware && strings.HasPrefix(path, route.Path) {
|
||||||
fmt.Println(ctx.params)
|
fmt.Println(ctx.params)
|
||||||
found = true
|
found = true
|
||||||
ctx.route = route
|
ctx.route = route
|
||||||
route.handler(ctx)
|
route.Handler(ctx)
|
||||||
if !ctx.next {
|
if !ctx.next {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -132,19 +132,19 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
continue
|
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
|
||||||
}
|
}
|
||||||
// Skip route if regex does not match
|
// Skip route if regex does not match
|
||||||
if !route.regex.MatchString(path) {
|
if !route.Regex.MatchString(path) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// If we have parameters, lets find the matches
|
// If we have parameters, lets find the matches
|
||||||
if 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 {
|
||||||
ctx.params = &route.params
|
ctx.params = &route.Params
|
||||||
ctx.values = matches[0][1:len(matches[0])]
|
ctx.values = matches[0][1:len(matches[0])]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,7 +152,7 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
|
|||||||
// Set route pointer if user wants to call .Route()
|
// Set route pointer if user wants to call .Route()
|
||||||
ctx.route = route
|
ctx.route = route
|
||||||
// Execute handler with context
|
// Execute handler with context
|
||||||
route.handler(ctx)
|
route.Handler(ctx)
|
||||||
// if next is not set, leave loop and release ctx
|
// if next is not set, leave loop and release ctx
|
||||||
if !ctx.next {
|
if !ctx.next {
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user