90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
// Package sushi provides a complete FastHTTP-based web framework
|
|
// with routing, sessions, authentication, CSRF protection, and utilities.
|
|
package sushi
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
func (h Handler) Serve(ctx Ctx) {
|
|
h(ctx)
|
|
}
|
|
|
|
func IsHTTPS(ctx Ctx) bool {
|
|
return ctx.IsTLS() ||
|
|
string(ctx.Request.Header.Peek("X-Forwarded-Proto")) == "https" ||
|
|
string(ctx.Request.Header.Peek("X-Forwarded-Scheme")) == "https"
|
|
}
|
|
|
|
// StandardHandler adapts a standard fasthttp.RequestHandler to the router's Handler
|
|
func StandardHandler(handler fasthttp.RequestHandler) Handler {
|
|
return func(ctx Ctx) {
|
|
handler(ctx.RequestCtx)
|
|
}
|
|
}
|
|
|
|
// ServerOptions contains configuration for the FastHTTP server
|
|
type ServerOptions struct {
|
|
Addr string
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
IdleTimeout time.Duration
|
|
MaxConnsPerIP int
|
|
MaxRequestsPerConn int
|
|
MaxRequestBodySize int
|
|
ReduceMemoryUsage bool
|
|
DisableKeepalive bool
|
|
TCPKeepalive bool
|
|
TCPKeepalivePeriod time.Duration
|
|
}
|
|
|
|
type App struct {
|
|
*fasthttp.Server
|
|
Router *Router
|
|
}
|
|
|
|
// New creates a new App instance with FastHTTP server and router
|
|
func New(opts ...ServerOptions) *App {
|
|
var options ServerOptions
|
|
if len(opts) > 0 {
|
|
options = opts[0]
|
|
}
|
|
|
|
// Set defaults
|
|
if options.ReadTimeout == 0 {
|
|
options.ReadTimeout = 10 * time.Second
|
|
}
|
|
if options.WriteTimeout == 0 {
|
|
options.WriteTimeout = 10 * time.Second
|
|
}
|
|
if options.IdleTimeout == 0 {
|
|
options.IdleTimeout = 60 * time.Second
|
|
}
|
|
if options.MaxRequestBodySize == 0 {
|
|
options.MaxRequestBodySize = 4 * 1024 * 1024 // 4MB
|
|
}
|
|
|
|
router := NewRouter()
|
|
|
|
app := &App{
|
|
Server: &fasthttp.Server{
|
|
Handler: router.Handler(),
|
|
ReadTimeout: options.ReadTimeout,
|
|
WriteTimeout: options.WriteTimeout,
|
|
IdleTimeout: options.IdleTimeout,
|
|
MaxConnsPerIP: options.MaxConnsPerIP,
|
|
MaxRequestsPerConn: options.MaxRequestsPerConn,
|
|
MaxRequestBodySize: options.MaxRequestBodySize,
|
|
ReduceMemoryUsage: options.ReduceMemoryUsage,
|
|
DisableKeepalive: options.DisableKeepalive,
|
|
TCPKeepalive: options.TCPKeepalive,
|
|
TCPKeepalivePeriod: options.TCPKeepalivePeriod,
|
|
},
|
|
Router: router,
|
|
}
|
|
|
|
return app
|
|
}
|