Add files via upload

pull/6/head
Fenny 2020-01-02 09:00:26 -05:00 committed by GitHub
parent 7b9191ded9
commit 30e74c7043
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 52 deletions

View File

@ -1,43 +1,12 @@
package fiber
import (
"sync"
"fmt"
"github.com/valyala/fasthttp"
)
// Context struct
type Context struct {
next bool
params *[]string
values []string
Fasthttp *fasthttp.RequestCtx
}
// Context pool
var ctxPool = sync.Pool{
New: func() interface{} {
return new(Context)
},
}
// Get new Context from pool
func acquireCtx(fctx *fasthttp.RequestCtx) *Context {
ctx := ctxPool.Get().(*Context)
ctx.Fasthttp = fctx
return ctx
}
// Return Context to pool
func releaseCtx(ctx *Context) {
ctx.next = false
ctx.params = nil
ctx.values = nil
ctx.Fasthttp = nil
ctxPool.Put(ctx)
}
// Next : Call the next middleware function in the stack.
// Next : Calls the next function that matches the route.
func (ctx *Context) Next() {
ctx.next = true
ctx.params = nil
@ -66,3 +35,65 @@ func (ctx *Context) Method() string {
func (ctx *Context) Path() string {
return b2s(ctx.Fasthttp.Path())
}
// Body :
func (ctx *Context) Body(args ...interface{}) string {
if len(args) == 0 {
return b2s(ctx.Fasthttp.Request.Body())
}
if len(args) == 1 {
switch arg := args[0].(type) {
case string:
return b2s(ctx.Fasthttp.Request.PostArgs().Peek(arg))
case func(string, string):
ctx.Fasthttp.Request.PostArgs().VisitAll(func(k []byte, v []byte) {
arg(b2s(k), b2s(v))
})
default:
return b2s(ctx.Fasthttp.Request.Body())
}
}
return ""
}
// Cookies :
func (ctx *Context) Cookies(args ...interface{}) string {
if len(args) == 1 {
switch arg := args[0].(type) {
case string:
return b2s(ctx.Fasthttp.Request.Header.Cookie(arg))
case func(string, string):
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
arg(b2s(k), b2s(v))
})
default:
panic("Invalid argument")
}
return ""
}
if len(args) > 1 {
key, keyOk := args[0].(string)
val, valOk := args[1].(string)
if !keyOk || !valOk {
panic("Invalid key or value string")
}
cook := &fasthttp.Cookie{}
cook.SetKey(key)
cook.SetValue(val)
if len(args) > 2 {
switch arg := args[2].(type) {
default:
fmt.Printf("%T\n", arg)
}
// fmt.Println(args[2])
// opt, optOk := args[2].(struct{})
// if !optOk {
// panic("Invalid cookie options")
// }
// fmt.Println(opt)
}
ctx.Fasthttp.Response.Header.SetCookie(cook)
}
return ""
}

BIN
logo.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 50 KiB

38
pool.go Normal file
View File

@ -0,0 +1,38 @@
package fiber
import (
"sync"
"github.com/valyala/fasthttp"
)
// Context struct
type Context struct {
next bool
params *[]string
values []string
Fasthttp *fasthttp.RequestCtx
}
// Context pool
var ctxPool = sync.Pool{
New: func() interface{} {
return new(Context)
},
}
// Get new Context from pool
func acquireCtx(fctx *fasthttp.RequestCtx) *Context {
ctx := ctxPool.Get().(*Context)
ctx.Fasthttp = fctx
return ctx
}
// Return Context to pool
func releaseCtx(ctx *Context) {
ctx.next = false
ctx.params = nil
ctx.values = nil
ctx.Fasthttp = nil
ctxPool.Put(ctx)
}

View File

@ -261,25 +261,25 @@ func (r *Fiber) Listen(port int) {
// Express custom handler
Handler: r.handler,
// Server settings
Name: r.Settings.Name,
Concurrency: r.Settings.Concurrency,
DisableKeepalive: r.Settings.DisableKeepAlive,
ReadBufferSize: r.Settings.ReadBufferSize,
WriteBufferSize: r.Settings.WriteBufferSize,
WriteTimeout: r.Settings.WriteTimeout,
IdleTimeout: r.Settings.IdleTimeout,
MaxConnsPerIP: r.Settings.MaxConnsPerIP,
MaxRequestsPerConn: r.Settings.MaxRequestsPerConn,
TCPKeepalive: r.Settings.TCPKeepalive,
TCPKeepalivePeriod: r.Settings.TCPKeepalivePeriod,
MaxRequestBodySize: r.Settings.MaxRequestBodySize,
ReduceMemoryUsage: r.Settings.ReduceMemoryUsage,
GetOnly: r.Settings.GetOnly,
DisableHeaderNamesNormalizing: r.Settings.DisableHeaderNamesNormalizing,
SleepWhenConcurrencyLimitsExceeded: r.Settings.SleepWhenConcurrencyLimitsExceeded,
NoDefaultServerHeader: r.Settings.NoDefaultServerHeader,
NoDefaultContentType: r.Settings.NoDefaultContentType,
KeepHijackedConns: r.Settings.KeepHijackedConns,
// Name: r.Settings.Name,
// Concurrency: r.Settings.Concurrency,
// DisableKeepalive: r.Settings.DisableKeepAlive,
// ReadBufferSize: r.Settings.ReadBufferSize,
// WriteBufferSize: r.Settings.WriteBufferSize,
// WriteTimeout: r.Settings.WriteTimeout,
// IdleTimeout: r.Settings.IdleTimeout,
// MaxConnsPerIP: r.Settings.MaxConnsPerIP,
// MaxRequestsPerConn: r.Settings.MaxRequestsPerConn,
// TCPKeepalive: r.Settings.TCPKeepalive,
// TCPKeepalivePeriod: r.Settings.TCPKeepalivePeriod,
// MaxRequestBodySize: r.Settings.MaxRequestBodySize,
// ReduceMemoryUsage: r.Settings.ReduceMemoryUsage,
// GetOnly: r.Settings.GetOnly,
// DisableHeaderNamesNormalizing: r.Settings.DisableHeaderNamesNormalizing,
// SleepWhenConcurrencyLimitsExceeded: r.Settings.SleepWhenConcurrencyLimitsExceeded,
// NoDefaultServerHeader: r.Settings.NoDefaultServerHeader,
// NoDefaultContentType: r.Settings.NoDefaultContentType,
// KeepHijackedConns: r.Settings.KeepHijackedConns,
}
if r.Settings.TLSEnable {
if err := server.ListenAndServeTLS(fmt.Sprintf(":%v", port), r.Settings.CertFile, r.Settings.CertKey); err != nil {