pull/6/head
Fenny 2020-01-15 03:07:49 +01:00
parent 1b12f9f232
commit 8d790b5cf0
9 changed files with 98 additions and 73 deletions

View File

@ -1,3 +1,10 @@
// 🚀 Fiber, Express on Steriods
// 📌 Don't use in production until version 1.0.0
// 🖥 https://github.com/fenny/fiber
// 🦸 Not all heroes wear capes, thank you +1000
// 💖 @valyala, @dgrr, @erikdubbelboer, @savsgio, @julienschmidt
package fiber
import (
@ -18,6 +25,7 @@ import (
// Ctx struct
type Ctx struct {
noCopy noCopy
route *route
next bool
params *[]string
values []string
@ -53,6 +61,7 @@ func acquireCtx(fctx *fasthttp.RequestCtx) *Ctx {
// Return Context to pool
func releaseCtx(ctx *Ctx) {
ctx.route = nil
ctx.next = false
ctx.params = nil
ctx.values = nil
@ -192,9 +201,9 @@ func (ctx *Ctx) Cookie(key, value string, options ...interface{}) {
if opt.Expire > 0 {
cook.SetExpire(time.Unix(int64(opt.Expire), 0))
}
// if opt.MaxAge > 0 {
// cook.SetMaxAge(opt.MaxAge)
// }
if opt.MaxAge > 0 {
cook.SetMaxAge(opt.MaxAge)
}
if opt.Domain != "" {
cook.SetDomain(opt.Domain)
}
@ -481,7 +490,7 @@ func (ctx *Ctx) Render() {
}
// Route : Only use in debugging
func (ctx *Ctx) Route(r *Fiber) (s struct {
func (ctx *Ctx) Route() (s struct {
Method string
Path string
Wildcard bool
@ -490,36 +499,13 @@ func (ctx *Ctx) Route(r *Fiber) (s struct {
Values []string
Handler func(*Ctx)
}) {
path := ctx.Path()
method := ctx.Method()
for _, route := range r.routes {
if route.method != "*" && route.method != method {
continue
}
if route.any || (route.path == path && route.params == nil) {
s.Method = method
s.Path = path
s.Wildcard = route.any
s.Regex = route.regex
s.Params = route.params
s.Values = ctx.values
s.Handler = route.handler
return
}
if route.regex == nil {
continue
}
if !route.regex.MatchString(path) {
continue
}
s.Method = method
s.Path = path
s.Wildcard = route.any
s.Regex = route.regex
s.Params = route.params
s.Handler = route.handler
return
}
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
}

View File

@ -7,6 +7,7 @@ Checks if the specified content types are acceptable, based on the requests A
// Function signature
c.Accepts(ext string) bool
// Example
app.Get("/", func(c *fiber.Ctx) {
// Accept: text/html
@ -20,8 +21,8 @@ app.Get("/", func(c *fiber.Ctx) {
c.Accepts("json")
// => // => true
c.Accepts("application/json")
// => // => true
c.Accepts("")
// => // => false
})
```
@ -725,31 +726,28 @@ app.Get("/", func(c *fiber.Ctx) {
#### Route
Contains the currently-matched route struct, **only use this for debugging**.
It returns an anonymous struct containing the following values:
```go
// Route struct
struct {
Method string
Path string
Wildcard bool
Regex *regexp.Regexp
Params []string
Values []string
Handler func(*Ctx)
}
```
It returns an anonymous struct as shown below.
```go
// Function signature
c.Route(app *Fiber) struct
c.Route() struct {
Method string
Path string
Wildcard bool
Regex *regexp.Regexp
Params []string
Values []string
Handler func(*Ctx)
}
// Example
app.Get("/hello", func(c *fiber.Ctx) {
c.Route(app)
// {GET /hello false <nil> [] [] 0x7b4ab0}
c.Route()
// => {GET /hello false <nil> [] [] 0x7b4ab0}
})
// http://localhost:8080/hello
app.Post("/:api?", func(c *fiber.Ctx) {
c.Route(app)
// {POST / false ^(?:/([^/]+?))?/?$ [lol] [] 0x7b49e0}
c.Route()
// => {POST / false ^(?:/([^/]+?))?/?$ [api] [hello] 0x7b49e0}
})
```

View File

@ -18,7 +18,7 @@
<script>
window.$docsify = {
name: 'Fiber v0.6.1',
name: 'Fiber v0.6.2',
repo: 'fenny/fiber',
loadSidebar: "sidebar.md",
homepage: 'getting_started.md',

6
go.mod
View File

@ -1,8 +1,8 @@
module github.com/fenny/fiber
go 1.11
require (
github.com/json-iterator/go v1.1.9
github.com/valyala/fasthttp v1.7.1
github.com/valyala/fasthttp v1.8.0
)
go 1.13

4
go.sum
View File

@ -16,8 +16,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.7.1 h1:UHtt5/7O70RSUZTR/hSu0PNWMAfWx5AtsPp9Jk+g17M=
github.com/valyala/fasthttp v1.7.1/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
github.com/valyala/fasthttp v1.8.0 h1:actnGGBYtGQmxVaZxyZpp57Vcc2NhcO7mMN0IMwCC0w=
github.com/valyala/fasthttp v1.8.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

13
middleware/cors.go Normal file
View File

@ -0,0 +1,13 @@
package middleware
import (
"fmt"
"github.com/fenny/fiber"
)
// Cors :
func Cors(c *fiber.Ctx) {
fmt.Println("LoL")
c.Next()
}

View File

@ -1,3 +1,10 @@
// 🚀 Fiber, Express on Steriods
// 📌 Don't use in production until version 1.0.0
// 🖥 https://github.com/fenny/fiber
// 🦸 Not all heroes wear capes, thank you +1000
// 💖 @valyala, @dgrr, @erikdubbelboer, @savsgio, @julienschmidt
package fiber
import (
@ -5,20 +12,21 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
// This json parsing lib is awesome *.*
// This json parsing lib is awesome
// "github.com/tidwall/gjson"
"github.com/valyala/fasthttp"
)
const (
// Version for debugging
Version = `0.6.1`
Version = `0.6.2`
// Port and Version are printed with the banner
banner = `%s _____ _ _
%s| __|_| |_ ___ ___
@ -59,10 +67,10 @@ type Fiber struct {
type route struct {
// HTTP method in uppercase, can be a * for Use() & All()
method string
// Any bool is for routes without a path or * and /*
any bool
// Stores the orignal path
path string
// wildcard bool is for routes without a path, * and /*
wildcard bool
// Stores compiled regex special routes :params, *wildcards, optionals?
regex *regexp.Regexp
// Store params if special routes :params, *wildcards, optionals?
@ -230,13 +238,14 @@ func (r *Fiber) register(method string, args ...interface{}) {
// function route!!
r.registerHandler(method, path, handler)
} else {
fmt.Println(reflect.TypeOf(handler))
panic("Every route needs to contain either a dir/file path or callback function")
}
}
func (r *Fiber) registerStatic(method, prefix, root string) {
var any bool
var wildcard bool
if prefix == "*" || prefix == "/*" {
any = true
wildcard = true
}
if prefix == "" {
prefix = "/"
@ -253,25 +262,25 @@ func (r *Fiber) registerStatic(method, prefix, root string) {
path := filepath.Join(prefix, strings.Replace(file, mount, "", 1))
filePath := file
if filepath.Base(filePath) == "index.html" {
r.routes = append(r.routes, &route{method, any, prefix, nil, nil, func(c *Ctx) {
r.routes = append(r.routes, &route{method, prefix, wildcard, nil, nil, func(c *Ctx) {
c.SendFile(filePath)
}})
}
r.routes = append(r.routes, &route{method, any, path, nil, nil, func(c *Ctx) {
r.routes = append(r.routes, &route{method, path, wildcard, nil, nil, func(c *Ctx) {
c.SendFile(filePath)
}})
}
}
func (r *Fiber) registerHandler(method, path string, handler func(*Ctx)) {
if path == "" || path == "*" || path == "/*" {
r.routes = append(r.routes, &route{method, true, path, nil, nil, handler})
r.routes = append(r.routes, &route{method, path, true, nil, nil, handler})
return
}
// Get params from path
params := getParams(path)
// If path has no params, we dont need regex
if len(params) == 0 {
r.routes = append(r.routes, &route{method, false, path, nil, nil, handler})
r.routes = append(r.routes, &route{method, path, false, nil, nil, handler})
return
}
@ -280,7 +289,7 @@ func (r *Fiber) registerHandler(method, path string, handler func(*Ctx)) {
if err != nil {
panic("Invalid url pattern: " + path)
}
r.routes = append(r.routes, &route{method, false, path, regex, params, handler})
r.routes = append(r.routes, &route{method, path, false, regex, params, handler})
}
// handler create a new context struct from the pool
@ -305,13 +314,15 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
continue
}
// First check if we match a static path or wildcard
if route.any || (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 route.any {
if route.wildcard {
ctx.params = &[]string{"*"}
ctx.values = []string{path}
}
found = true
// Set route pointer if user wants to call .Route()
ctx.route = route
// Execute handler with context
route.handler(ctx)
// if next is not set, leave loop and release ctx
@ -341,6 +352,8 @@ func (r *Fiber) handler(fctx *fasthttp.RequestCtx) {
}
}
found = true
// Set route pointer if user wants to call .Route()
ctx.route = route
// Execute handler with context
route.handler(ctx)
// if next is not set, leave loop and release ctx

View File

@ -1,5 +1,13 @@
// 🚀 Fiber, Express on Steriods
// 📌 Don't use in production until version 1.0.0
// 🖥 https://github.com/fenny/fiber
// 🦸 Not all heroes wear capes, thank you +1000
// 💖 @valyala, @dgrr, @erikdubbelboer, @savsgio, @julienschmidt
package fiber
// Credits @valyala
// https://github.com/valyala/fasthttp/blob/master/status.go
const (
StatusContinue = 100 // RFC 7231, 6.2.1

View File

@ -1,3 +1,10 @@
// 🚀 Fiber, Express on Steriods
// 📌 Don't use in production until version 1.0.0
// 🖥 https://github.com/fenny/fiber
// 🦸 Not all heroes wear capes, thank you +1000
// 💖 @valyala, @dgrr, @erikdubbelboer, @savsgio, @julienschmidt
package fiber
import (