mirror of https://github.com/gofiber/fiber.git
Add files via upload
parent
7b9191ded9
commit
30e74c7043
97
context.go
97
context.go
|
@ -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
BIN
logo.jpg
Binary file not shown.
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 50 KiB |
|
@ -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)
|
||||
}
|
38
router.go
38
router.go
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue