mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
Add files via upload
This commit is contained in:
parent
7b9191ded9
commit
30e74c7043
97
context.go
97
context.go
@ -1,43 +1,12 @@
|
|||||||
package fiber
|
package fiber
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"fmt"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context struct
|
// Next : Calls the next function that matches the route.
|
||||||
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.
|
|
||||||
func (ctx *Context) Next() {
|
func (ctx *Context) Next() {
|
||||||
ctx.next = true
|
ctx.next = true
|
||||||
ctx.params = nil
|
ctx.params = nil
|
||||||
@ -66,3 +35,65 @@ func (ctx *Context) Method() string {
|
|||||||
func (ctx *Context) Path() string {
|
func (ctx *Context) Path() string {
|
||||||
return b2s(ctx.Fasthttp.Path())
|
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 |
38
pool.go
Normal file
38
pool.go
Normal 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)
|
||||||
|
}
|
38
router.go
38
router.go
@ -261,25 +261,25 @@ func (r *Fiber) Listen(port int) {
|
|||||||
// Express custom handler
|
// Express custom handler
|
||||||
Handler: r.handler,
|
Handler: r.handler,
|
||||||
// Server settings
|
// Server settings
|
||||||
Name: r.Settings.Name,
|
// Name: r.Settings.Name,
|
||||||
Concurrency: r.Settings.Concurrency,
|
// Concurrency: r.Settings.Concurrency,
|
||||||
DisableKeepalive: r.Settings.DisableKeepAlive,
|
// DisableKeepalive: r.Settings.DisableKeepAlive,
|
||||||
ReadBufferSize: r.Settings.ReadBufferSize,
|
// ReadBufferSize: r.Settings.ReadBufferSize,
|
||||||
WriteBufferSize: r.Settings.WriteBufferSize,
|
// WriteBufferSize: r.Settings.WriteBufferSize,
|
||||||
WriteTimeout: r.Settings.WriteTimeout,
|
// WriteTimeout: r.Settings.WriteTimeout,
|
||||||
IdleTimeout: r.Settings.IdleTimeout,
|
// IdleTimeout: r.Settings.IdleTimeout,
|
||||||
MaxConnsPerIP: r.Settings.MaxConnsPerIP,
|
// MaxConnsPerIP: r.Settings.MaxConnsPerIP,
|
||||||
MaxRequestsPerConn: r.Settings.MaxRequestsPerConn,
|
// MaxRequestsPerConn: r.Settings.MaxRequestsPerConn,
|
||||||
TCPKeepalive: r.Settings.TCPKeepalive,
|
// TCPKeepalive: r.Settings.TCPKeepalive,
|
||||||
TCPKeepalivePeriod: r.Settings.TCPKeepalivePeriod,
|
// TCPKeepalivePeriod: r.Settings.TCPKeepalivePeriod,
|
||||||
MaxRequestBodySize: r.Settings.MaxRequestBodySize,
|
// MaxRequestBodySize: r.Settings.MaxRequestBodySize,
|
||||||
ReduceMemoryUsage: r.Settings.ReduceMemoryUsage,
|
// ReduceMemoryUsage: r.Settings.ReduceMemoryUsage,
|
||||||
GetOnly: r.Settings.GetOnly,
|
// GetOnly: r.Settings.GetOnly,
|
||||||
DisableHeaderNamesNormalizing: r.Settings.DisableHeaderNamesNormalizing,
|
// DisableHeaderNamesNormalizing: r.Settings.DisableHeaderNamesNormalizing,
|
||||||
SleepWhenConcurrencyLimitsExceeded: r.Settings.SleepWhenConcurrencyLimitsExceeded,
|
// SleepWhenConcurrencyLimitsExceeded: r.Settings.SleepWhenConcurrencyLimitsExceeded,
|
||||||
NoDefaultServerHeader: r.Settings.NoDefaultServerHeader,
|
// NoDefaultServerHeader: r.Settings.NoDefaultServerHeader,
|
||||||
NoDefaultContentType: r.Settings.NoDefaultContentType,
|
// NoDefaultContentType: r.Settings.NoDefaultContentType,
|
||||||
KeepHijackedConns: r.Settings.KeepHijackedConns,
|
// KeepHijackedConns: r.Settings.KeepHijackedConns,
|
||||||
}
|
}
|
||||||
if r.Settings.TLSEnable {
|
if r.Settings.TLSEnable {
|
||||||
if err := server.ListenAndServeTLS(fmt.Sprintf(":%v", port), r.Settings.CertFile, r.Settings.CertKey); err != nil {
|
if err := server.ListenAndServeTLS(fmt.Sprintf(":%v", port), r.Settings.CertFile, r.Settings.CertKey); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user