1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-12 02:41:05 +00:00
fiber/app.go

722 lines
22 KiB
Go
Raw Normal View History

// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
2020-02-21 18:07:43 +01:00
2020-07-06 17:12:35 +02:00
// Package fiber
// Fiber is an Express inspired web framework built on top of Fasthttp,
// the fastest HTTP engine for Go. Designed to ease things up for fast
// development with zero memory allocation and performance in mind.
2020-02-21 18:07:43 +01:00
package fiber
import (
"bufio"
"fmt"
"net"
"net/http"
"net/http/httputil"
2020-06-22 15:55:24 +02:00
"os"
2020-02-27 04:10:26 -05:00
"reflect"
2020-06-23 15:04:21 +02:00
"runtime"
2020-02-21 18:07:43 +01:00
"strconv"
"strings"
"sync"
2020-02-21 18:07:43 +01:00
"time"
2020-09-13 11:20:11 +02:00
"github.com/gofiber/fiber/v2/utils"
"github.com/gofiber/fiber/v2/utils/colorable"
"github.com/gofiber/fiber/v2/utils/isatty"
"github.com/valyala/fasthttp"
2020-02-21 18:07:43 +01:00
)
2020-03-24 05:31:51 +01:00
// Version of current package
2020-09-13 11:20:11 +02:00
const Version = "2.0.0"
2020-02-21 18:07:43 +01:00
2020-05-30 17:39:33 +02:00
// Map is a shortcut for map[string]interface{}, useful for JSON returns
2020-03-24 05:31:51 +01:00
type Map map[string]interface{}
2020-06-01 11:19:29 +02:00
// Handler defines a function to serve HTTP requests.
2020-09-13 11:20:11 +02:00
type Handler = func(*Ctx) error
// ErrorHandler defines a function that will process all errors
// returned from any handlers in the stack
type ErrorHandler = func(*Ctx, error) error
// Error represents an error that occurred while handling a request.
type Error struct {
2020-07-02 20:26:38 +02:00
Code int `json:"code"`
Message string `json:"message"`
}
2020-06-07 20:57:55 +02:00
2020-03-31 10:03:39 +02:00
// App denotes the Fiber application.
type App struct {
mutex sync.Mutex
2020-06-20 17:26:48 +02:00
// Route stack divided by HTTP methods
stack [][]*Route
// Route stack divided by HTTP methods and route prefixes
treeStack []map[string][]*Route
// Amount of registered routes
routesCount int
// Amount of registered handlers
handlerCount int
// Ctx pool
pool sync.Pool
// Fasthttp server
2020-05-16 05:11:25 +02:00
server *fasthttp.Server
2020-09-13 11:20:11 +02:00
// App config
config Config
}
// Config is a struct holding the server settings.
type Config struct {
// When set to true, this will spawn multiple Go processes listening on the same port.
// Default: false
Prefork bool `json:"prefork"`
2020-06-03 17:20:20 +02:00
2020-06-03 00:01:55 +02:00
// Enables the "Server: value" HTTP header.
// Default: ""
2020-07-02 20:26:38 +02:00
ServerHeader string `json:"server_header"`
// When set to true, the router treats "/foo" and "/foo/" as different.
// By default this is disabled and both "/foo" and "/foo/" will execute the same handler.
2020-07-02 20:26:38 +02:00
StrictRouting bool `json:"strict_routing"`
// When set to true, enables case sensitive routing.
// E.g. "/FoO" and "/foo" are treated as different routes.
// By default this is disabled and both "/FoO" and "/foo" will execute the same handler.
2020-07-02 20:26:38 +02:00
CaseSensitive bool `json:"case_sensitive"`
// When set to true, this relinquishes the 0-allocation promise in certain
2020-07-12 22:48:12 +09:00
// cases in order to access the handler values (e.g. request bodies) in an
// immutable fashion so that these values are available even if you return
// from handler.
2020-06-03 00:01:55 +02:00
// Default: false
2020-07-02 20:26:38 +02:00
Immutable bool `json:"immutable"`
// When set to true, converts all encoded characters in the route back
// before setting the path for the context, so that the routing can also
// work with urlencoded special characters.
// Default: false
2020-07-02 20:26:38 +02:00
UnescapePath bool `json:"unescape_path"`
// Enable or disable ETag header generation, since both weak and strong etags are generated
// using the same hashing method (CRC-32). Weak ETags are the default when enabled.
// Default: false
2020-07-02 20:26:38 +02:00
ETag bool `json:"etag"`
// Max body size that the server accepts.
2020-06-03 00:01:55 +02:00
// Default: 4 * 1024 * 1024
2020-07-02 20:26:38 +02:00
BodyLimit int `json:"body_limit"`
2020-04-19 16:10:19 +02:00
// Maximum number of concurrent connections.
2020-06-03 00:01:55 +02:00
// Default: 256 * 1024
2020-07-02 20:26:38 +02:00
Concurrency int `json:"concurrency"`
2020-06-12 12:29:57 +02:00
// Views is the interface that wraps the Render function.
// Default: nil
2020-07-02 20:26:38 +02:00
Views Views `json:"-"`
2020-06-12 12:29:57 +02:00
2020-03-24 05:31:51 +01:00
// The amount of time allowed to read the full request including body.
2020-06-21 18:00:57 +02:00
// It is reset after the request handler has returned.
// The connection's read deadline is reset when the connection opens.
2020-06-03 00:01:55 +02:00
// Default: unlimited
2020-07-02 20:26:38 +02:00
ReadTimeout time.Duration `json:"read_timeout"`
2020-03-24 05:31:51 +01:00
// The maximum duration before timing out writes of the response.
2020-06-21 18:00:57 +02:00
// It is reset after the request handler has returned.
2020-06-03 00:01:55 +02:00
// Default: unlimited
2020-07-02 20:26:38 +02:00
WriteTimeout time.Duration `json:"write_timeout"`
2020-03-24 05:31:51 +01:00
// The maximum amount of time to wait for the next request when keep-alive is enabled.
2020-06-21 18:00:57 +02:00
// If IdleTimeout is zero, the value of ReadTimeout is used.
2020-06-03 00:01:55 +02:00
// Default: unlimited
2020-07-02 20:26:38 +02:00
IdleTimeout time.Duration `json:"idle_timeout"`
2020-02-21 18:07:43 +01:00
2020-06-15 13:36:50 +02:00
// Per-connection buffer size for requests' reading.
// This also limits the maximum header size.
// Increase this buffer if your clients send multi-KB RequestURIs
// and/or multi-KB headers (for example, BIG cookies).
// Default: 4096
2020-07-02 20:26:38 +02:00
ReadBufferSize int `json:"read_buffer_size"`
2020-06-15 13:36:50 +02:00
// Per-connection buffer size for responses' writing.
// Default: 4096
2020-07-02 20:26:38 +02:00
WriteBufferSize int `json:"write_buffer_size"`
2020-06-15 13:36:50 +02:00
2020-06-07 20:35:41 +02:00
// CompressedFileSuffix adds suffix to the original file name and
// tries saving the resulting compressed file under the new file name.
// Default: ".fiber.gz"
2020-07-02 20:26:38 +02:00
CompressedFileSuffix string `json:"compressed_file_suffix"`
2020-06-07 20:35:41 +02:00
2020-09-13 11:20:11 +02:00
// ProxyHeader will enable c.IP() to return the value of the given header key
// By default c.IP() will return the Remote IP from the TCP connection
// This property can be useful if you are behind a load balancer: X-Forwarded-*
// NOTE: headers are easily spoofed and the detected IP addresses are unreliable.
// Default: ""
ProxyHeader string `json:"proxy_header"`
// GETOnly rejects all non-GET requests if set to true.
// This option is useful as anti-DoS protection for servers
// accepting only GET requests. The request size is limited
// by ReadBufferSize if GETOnly is set.
// Server accepts all the requests by default.
GETOnly bool `json:"get_only"`
// ErrorHandler is executed when an error is returned from fiber.Handler.
// cfg := fiber.Config{}
// cfg.ErrorHandler = func(c *Ctx, err error) error {
// code := StatusInternalServerError
// if e, ok := err.(*Error); ok {
// code = e.Code
// }
// c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
// return c.Status(code).SendString(err.Error())
// }
// app := fiber.New(cfg)
ErrorHandler ErrorHandler `json:"-"`
// When set to true, disables keep-alive connections.
// The server will close incoming connections after sending the first response to client.
// Default: false
2020-09-14 04:54:26 +02:00
DisableKeepalive bool `json:"disable_keepalive"`
2020-09-13 11:20:11 +02:00
// When set to true, causes the default date header to be excluded from the response.
// Default: false
DisableDefaultDate bool `json:"disable_default_date"`
// When set to true, causes the default Content-Type header to be excluded from the response.
// Default: false
DisableDefaultContentType bool `json:"disable_default_content_type"`
// When set to true, disables header normalization.
// By default all header names are normalized: conteNT-tYPE -> Content-Type.
// Default: false
DisableHeaderNormalizing bool `json:"disable_header_normalizing"`
// When set to true, it will not print out the «Fiber» ASCII art and listening address.
// Default: false
DisableStartupMessage bool `json:"disable_startup_message"`
2020-09-14 05:22:15 +02:00
// FEATURE: v2.2.x
// The router executes the same handler by default if StrictRouting or CaseSensitive is disabled.
// Enabling RedirectFixedPath will change this behaviour into a client redirect to the original route path.
// Using the status code 301 for GET requests and 308 for all other request methods.
// RedirectFixedPath bool
}
// Static defines configuration options when defining static assets.
type Static struct {
// When set to true, the server tries minimizing CPU usage by caching compressed files.
// This works differently than the github.com/gofiber/compression middleware.
// Optional. Default value false
2020-09-13 11:20:11 +02:00
Compress bool `json:"compress"`
// When set to true, enables byte range requests.
// Optional. Default value false
2020-09-13 11:20:11 +02:00
ByteRange bool `json:"byte_range"`
// When set to true, enables directory browsing.
// Optional. Default value false.
2020-09-13 11:20:11 +02:00
Browse bool `json:"browse"`
// The name of the index file for serving a directory.
// Optional. Default value "index.html".
2020-09-13 11:20:11 +02:00
Index string `json:"index"`
}
2020-09-13 11:39:55 +02:00
// Default Config values
const (
2020-09-13 11:20:11 +02:00
DefaultBodyLimit = 4 * 1024 * 1024
DefaultConcurrency = 256 * 1024
DefaultReadBufferSize = 4096
DefaultWriteBufferSize = 4096
DefaultCompressedFileSuffix = ".fiber.gz"
)
2020-09-13 12:04:14 +02:00
// Default ErrorHandler that process return errors from handlers
2020-09-13 11:20:11 +02:00
var DefaultErrorHandler = func(c *Ctx, err error) error {
code := StatusInternalServerError
if e, ok := err.(*Error); ok {
code = e.Code
}
2020-09-13 11:20:11 +02:00
c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
return c.Status(code).SendString(err.Error())
}
2020-03-24 05:31:51 +01:00
// New creates a new Fiber named instance.
2020-09-13 11:20:11 +02:00
// app := fiber.New()
2020-09-14 04:54:26 +02:00
// You can pass optional configuration options by passing a Config struct:
2020-09-13 11:20:11 +02:00
// app := fiber.New(fiber.Config{
// Prefork: true,
// ServerHeader: "Fiber",
// })
2020-09-13 11:20:11 +02:00
func New(config ...Config) *App {
// Create a new app
app := &App{
// Create router stack
stack: make([][]*Route, len(intMethod)),
treeStack: make([]map[string][]*Route, len(intMethod)),
// Create Ctx pool
pool: sync.Pool{
New: func() interface{} {
return new(Ctx)
},
},
2020-09-13 11:20:11 +02:00
// Create config
config: Config{},
}
2020-09-13 11:20:11 +02:00
// Override config if provided
if len(config) > 0 {
app.config = config[0]
2020-03-14 12:30:21 +01:00
}
2020-09-13 11:20:11 +02:00
// Override default values
if app.config.BodyLimit <= 0 {
app.config.BodyLimit = DefaultBodyLimit
2020-06-07 20:57:55 +02:00
}
2020-09-13 11:20:11 +02:00
if app.config.Concurrency <= 0 {
app.config.Concurrency = DefaultConcurrency
2020-06-15 13:36:50 +02:00
}
2020-09-13 11:20:11 +02:00
if app.config.ReadBufferSize <= 0 {
app.config.ReadBufferSize = DefaultReadBufferSize
2020-06-15 13:36:50 +02:00
}
2020-09-13 11:20:11 +02:00
if app.config.WriteBufferSize <= 0 {
app.config.WriteBufferSize = DefaultWriteBufferSize
2020-06-07 20:57:55 +02:00
}
2020-09-13 11:20:11 +02:00
if app.config.CompressedFileSuffix == "" {
app.config.CompressedFileSuffix = DefaultCompressedFileSuffix
2020-06-07 20:57:55 +02:00
}
2020-09-13 11:20:11 +02:00
if app.config.Immutable {
2020-06-07 21:19:58 +02:00
getBytes, getString = getBytesImmutable, getStringImmutable
2020-06-07 20:57:55 +02:00
}
2020-09-13 11:20:11 +02:00
if app.config.ErrorHandler == nil {
app.config.ErrorHandler = DefaultErrorHandler
}
// Init app
app.init()
2020-06-12 12:29:57 +02:00
// Return app
return app
2020-02-21 18:07:43 +01:00
}
2020-09-14 04:54:26 +02:00
// Use registers a middleware route that will match requests
// with the provided prefix (which is optional and defaults to "/").
2020-09-13 11:20:11 +02:00
//
// app.Use(func(c *fiber.Ctx) error {
// return c.Next()
// })
// app.Use("/api", func(c *fiber.Ctx) error {
// return c.Next()
// })
2020-09-14 04:54:26 +02:00
// app.Use("/api", handler, func(c *fiber.Ctx) error {
2020-09-13 11:20:11 +02:00
// return c.Next()
// })
2020-05-16 05:11:25 +02:00
//
2020-09-13 11:20:11 +02:00
// This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...
func (app *App) Use(args ...interface{}) Router {
2020-05-16 05:11:25 +02:00
var prefix string
var handlers []Handler
2020-05-16 05:11:25 +02:00
2020-02-27 04:10:26 -05:00
for i := 0; i < len(args); i++ {
switch arg := args[i].(type) {
case string:
2020-05-16 05:11:25 +02:00
prefix = arg
case Handler:
2020-02-27 04:10:26 -05:00
handlers = append(handlers, arg)
2020-09-13 11:20:11 +02:00
case *App:
stack := arg.Stack()
for m := range stack {
for r := range stack[m] {
route := app.copyRoute(stack[m][r])
app.addRoute(route.Method, app.addPrefixToRoute(prefix, route))
}
}
return app
2020-02-27 04:10:26 -05:00
default:
2020-07-15 17:43:30 +02:00
panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
2020-02-27 04:10:26 -05:00
}
}
2020-07-19 13:36:26 +02:00
app.register(methodUse, prefix, handlers...)
return app
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Get registers a route for GET methods that requests a representation
// of the specified resource. Requests using GET should only retrieve data.
func (app *App) Get(path string, handlers ...Handler) Router {
2020-09-13 11:20:11 +02:00
return app.Add(MethodHead, path, handlers...).Add(MethodGet, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Head registers a route for HEAD methods that asks for a response identical
// to that of a GET request, but without the response body.
func (app *App) Head(path string, handlers ...Handler) Router {
return app.Add(MethodHead, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Post registers a route for POST methods that is used to submit an entity to the
// specified resource, often causing a change in state or side effects on the server.
func (app *App) Post(path string, handlers ...Handler) Router {
return app.Add(MethodPost, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Put registers a route for PUT methods that replaces all current representations
// of the target resource with the request payload.
func (app *App) Put(path string, handlers ...Handler) Router {
return app.Add(MethodPut, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Delete registers a route for DELETE methods that deletes the specified resource.
func (app *App) Delete(path string, handlers ...Handler) Router {
return app.Add(MethodDelete, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Connect registers a route for CONNECT methods that establishes a tunnel to the
// server identified by the target resource.
func (app *App) Connect(path string, handlers ...Handler) Router {
return app.Add(MethodConnect, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Options registers a route for OPTIONS methods that is used to describe the
// communication options for the target resource.
func (app *App) Options(path string, handlers ...Handler) Router {
return app.Add(MethodOptions, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Trace registers a route for TRACE methods that performs a message loop-back
// test along the path to the target resource.
func (app *App) Trace(path string, handlers ...Handler) Router {
return app.Add(MethodTrace, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-07-06 17:57:00 +02:00
// Patch registers a route for PATCH methods that is used to apply partial
// modifications to a resource.
func (app *App) Patch(path string, handlers ...Handler) Router {
return app.Add(MethodPatch, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-09-13 11:20:11 +02:00
// Add allows you to specify a HTTP method to register a route
func (app *App) Add(method, path string, handlers ...Handler) Router {
2020-09-13 11:20:11 +02:00
return app.register(method, path, handlers...)
2020-05-16 05:11:25 +02:00
}
2020-09-13 11:20:11 +02:00
// Static will create a file server serving static files
func (app *App) Static(prefix, root string, config ...Static) Router {
2020-09-13 11:20:11 +02:00
return app.registerStatic(prefix, root, config...)
2020-05-16 05:11:25 +02:00
}
2020-09-13 11:20:11 +02:00
// All will register the handler on all HTTP methods
func (app *App) All(path string, handlers ...Handler) Router {
for _, method := range intMethod {
2020-09-13 11:20:11 +02:00
_ = app.Add(method, path, handlers...)
}
return app
2020-02-21 18:07:43 +01:00
}
2020-03-24 05:31:51 +01:00
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
2020-09-13 11:20:11 +02:00
// api := app.Group("/api")
2020-09-14 04:54:26 +02:00
// api.Get("/users", handler)
func (app *App) Group(prefix string, handlers ...Handler) Router {
if len(handlers) > 0 {
app.register(methodUse, prefix, handlers...)
2020-05-16 05:11:25 +02:00
}
return &Group{prefix: prefix, app: app}
}
2020-09-13 11:20:11 +02:00
// Error makes it compatible with the `error` interface.
2020-06-22 15:12:50 +02:00
func (e *Error) Error() string {
return e.Message
}
2020-09-14 04:54:26 +02:00
// NewError creates a new Error instance with an optional message
2020-06-22 15:12:50 +02:00
func NewError(code int, message ...string) *Error {
2020-09-13 11:20:11 +02:00
e := &Error{
Code: code,
}
2020-06-22 15:12:50 +02:00
if len(message) > 0 {
e.Message = message[0]
2020-09-13 11:20:11 +02:00
} else {
e.Message = utils.StatusMessage(code)
2020-06-22 15:12:50 +02:00
}
return e
}
// Listener can be used to pass a custom listener.
2020-09-13 11:20:11 +02:00
func (app *App) Listener(ln net.Listener) error {
// Prefork is supported for custom listeners
if app.config.Prefork {
addr, tls := lnMetadata(ln)
return app.prefork(addr, tls)
2020-04-12 14:58:05 +02:00
}
2020-09-13 11:20:11 +02:00
// Print startup message
2020-09-13 11:20:11 +02:00
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), false, "")
}
2020-09-13 11:20:11 +02:00
// TODO: Detect TLS
2020-04-12 14:58:05 +02:00
return app.server.Serve(ln)
}
2020-09-13 11:20:11 +02:00
// Listen serves HTTP requests from the given addr.
2020-07-07 12:07:06 +02:00
//
// app.Listen(":8080")
// app.Listen("127.0.0.1:8080")
2020-09-13 11:20:11 +02:00
func (app *App) Listen(addr string) error {
2020-07-18 01:19:45 +02:00
// Start prefork
2020-09-13 11:20:11 +02:00
if app.config.Prefork {
return app.prefork(addr, nil)
2020-07-18 01:17:52 +02:00
}
2020-06-22 15:12:50 +02:00
// Setup listener
2020-09-13 11:20:11 +02:00
ln, err := net.Listen("tcp4", addr)
2020-06-22 15:12:50 +02:00
if err != nil {
return err
}
2020-06-25 22:48:49 +02:00
// Print startup message
2020-09-13 11:20:11 +02:00
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), false, "")
2020-06-25 22:48:49 +02:00
}
2020-06-22 15:12:50 +02:00
// Start listening
2020-02-21 18:07:43 +01:00
return app.server.Serve(ln)
}
2020-09-13 11:20:11 +02:00
// Config returns the app config as value ( read-only ).
func (app *App) Config() Config {
return app.config
}
// Handler returns the server handler.
func (app *App) Handler() fasthttp.RequestHandler {
return app.handler
}
2020-09-13 11:20:11 +02:00
// Stack returns the raw router stack.
func (app *App) Stack() [][]*Route {
return app.stack
}
2020-09-13 11:20:11 +02:00
// Shutdown gracefully shuts down the server without interrupting any active connections.
2020-03-24 05:31:51 +01:00
// Shutdown works by first closing all open listeners and then waiting indefinitely for all connections to return to idle and then shut down.
//
// Make sure the program doesn't exit and waits instead for Shutdown to return.
//
// Shutdown does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
2020-03-31 10:03:39 +02:00
func (app *App) Shutdown() error {
app.mutex.Lock()
defer app.mutex.Unlock()
2020-02-21 18:07:43 +01:00
if app.server == nil {
2020-06-08 02:55:19 +02:00
return fmt.Errorf("shutdown: server is not running")
2020-02-21 18:07:43 +01:00
}
return app.server.Shutdown()
}
// Test is used for internal debugging by passing a *http.Request.
// Timeout is optional and defaults to 1s, -1 will disable it completely.
2020-09-13 11:20:11 +02:00
func (app *App) Test(req *http.Request, msTimeout ...int) (resp *http.Response, err error) {
// Set timeout
timeout := 1000
2020-03-22 17:35:12 +01:00
if len(msTimeout) > 0 {
timeout = msTimeout[0]
}
2020-09-13 11:20:11 +02:00
// Add Content-Length if not provided with body
2020-09-13 11:20:11 +02:00
if req.Body != http.NoBody && req.Header.Get(HeaderContentLength) == "" {
req.Header.Add(HeaderContentLength, strconv.FormatInt(req.ContentLength, 10))
}
2020-09-13 11:20:11 +02:00
2020-03-20 16:43:28 +01:00
// Dump raw http request
2020-09-13 11:20:11 +02:00
dump, err := httputil.DumpRequest(req, true)
2020-02-21 18:07:43 +01:00
if err != nil {
return nil, err
}
2020-09-13 11:20:11 +02:00
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
// Create test connection
conn := new(testConn)
2020-09-13 11:20:11 +02:00
2020-03-20 16:43:28 +01:00
// Write raw http request
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
if _, err = conn.r.Write(dump); err != nil {
2020-02-21 18:07:43 +01:00
return nil, err
}
2020-09-13 11:20:11 +02:00
2020-02-21 18:07:43 +01:00
// Serve conn to server
channel := make(chan error)
go func() {
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
channel <- app.server.ServeConn(conn)
2020-02-21 18:07:43 +01:00
}()
2020-09-13 11:20:11 +02:00
2020-03-23 21:52:37 +01:00
// Wait for callback
if timeout >= 0 {
// With timeout
select {
case err = <-channel:
case <-time.After(time.Duration(timeout) * time.Millisecond):
2020-06-08 02:55:19 +02:00
return nil, fmt.Errorf("test: timeout error %vms", timeout)
2020-02-21 18:07:43 +01:00
}
} else {
// Without timeout
2020-05-12 23:24:04 +02:00
err = <-channel
}
2020-09-13 11:20:11 +02:00
// Check for errors
2020-09-13 11:20:11 +02:00
if err != nil && err != fasthttp.ErrGetOnly {
return nil, err
2020-03-24 03:43:13 +01:00
}
2020-09-13 11:20:11 +02:00
2020-03-20 16:43:28 +01:00
// Read response
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
buffer := bufio.NewReader(&conn.w)
2020-09-13 11:20:11 +02:00
2020-03-20 16:43:28 +01:00
// Convert raw http response to *http.Response
2020-09-13 11:20:11 +02:00
return http.ReadResponse(buffer, req)
2020-02-21 18:07:43 +01:00
}
type disableLogger struct{}
func (dl *disableLogger) Printf(format string, args ...interface{}) {
// fmt.Println(fmt.Sprintf(format, args...))
}
func (app *App) init() *App {
2020-09-13 11:20:11 +02:00
// lock application
Use param support + optimizations (#361) * Benchmark workflow * Update router.go * Clean root * Add mutex * Benchmark workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Benchmark Workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Update security workflow * Benchmark workflow * Add mutex * Enable benchmark tests * Enable race testing Co-Authored-By: ReneWerner87 <renewerner87@users.noreply.github.com> * Make Ctx pool accessible - Add ctx benchmarks * v1.9.6 * v1.9.6 Co-Authored-By: ReneWerner87 <renewerner87@googlemail.com> * Improve context functions * Add utils benchmarks * Update benchmarks & tests * Add utils tests * New tests * update test * Move fastpath tests * offer negotiation * Cleanup * Update Vary Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize Append Co-Authored-By: RW <renewerner87@googlemail.com> * Optimize more methods Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add param support to Use Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Add use_params tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * Tests Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> * v1.9.7 Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Update app_test.go Co-Authored-By: RW <renewerner87@googlemail.com> Co-Authored-By: Vic Shóstak <vikkyshostak@gmail.com> Co-Authored-By: József Sallai <jozsef@sallai.me> Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-Authored-By: Nifty255 <nifty255@users.noreply.github.com> * Rename argument Co-Authored-By: RW <renewerner87@googlemail.com> * Add nosec for WriteByte Co-Authored-By: RW <renewerner87@googlemail.com> * Add media article * Update media articles * Fix typo Co-Authored-By: Thomas van Vugt <thomasvvugt@users.noreply.github.com> * Fix typo Co-authored-by: ReneWerner87 <renewerner87@users.noreply.github.com> Co-authored-by: ReneWerner87 <renewerner87@googlemail.com> Co-authored-by: Vic Shóstak <vikkyshostak@gmail.com> Co-authored-by: József Sallai <jozsef@sallai.me> Co-authored-by: Thomas van Vugt <thomasvvugt@users.noreply.github.com> Co-authored-by: Nifty255 <nifty255@users.noreply.github.com>
2020-05-12 19:24:04 +02:00
app.mutex.Lock()
2020-08-11 00:08:04 +02:00
2020-09-13 11:20:11 +02:00
// Only load templates if an view engine is specified
if app.config.Views != nil {
if err := app.config.Views.Load(); err != nil {
fmt.Printf("views: %v\n", err)
2020-06-12 12:29:57 +02:00
}
}
2020-09-13 11:20:11 +02:00
// create fasthttp server
app.server = &fasthttp.Server{
Logger: &disableLogger{},
LogAllErrors: false,
ErrorHandler: func(fctx *fasthttp.RequestCtx, err error) {
c := app.AcquireCtx(fctx)
if _, ok := err.(*fasthttp.ErrSmallBuffer); ok {
err = ErrRequestHeaderFieldsTooLarge
} else if netErr, ok := err.(*net.OpError); ok && netErr.Timeout() {
err = ErrRequestTimeout
} else if err == fasthttp.ErrBodyTooLarge {
err = ErrRequestEntityTooLarge
} else if err == fasthttp.ErrGetOnly {
err = ErrMethodNotAllowed
} else if strings.Contains(err.Error(), "timeout") {
err = ErrRequestTimeout
} else {
err = ErrBadRequest
}
2020-09-14 04:17:36 +02:00
if catch := app.config.ErrorHandler(c, err); catch != nil {
_ = c.SendStatus(StatusInternalServerError)
}
2020-09-13 11:20:11 +02:00
app.ReleaseCtx(c)
},
2020-05-16 05:11:25 +02:00
}
2020-09-13 11:20:11 +02:00
// fasthttp server settings
app.server.Handler = app.handler
app.server.Name = app.config.ServerHeader
app.server.Concurrency = app.config.Concurrency
app.server.NoDefaultDate = app.config.DisableDefaultDate
app.server.NoDefaultContentType = app.config.DisableDefaultContentType
app.server.DisableHeaderNamesNormalizing = app.config.DisableHeaderNormalizing
app.server.DisableKeepalive = app.config.DisableKeepalive
app.server.MaxRequestBodySize = app.config.BodyLimit
app.server.NoDefaultServerHeader = app.config.ServerHeader == ""
app.server.ReadTimeout = app.config.ReadTimeout
app.server.WriteTimeout = app.config.WriteTimeout
app.server.IdleTimeout = app.config.IdleTimeout
app.server.ReadBufferSize = app.config.ReadBufferSize
app.server.WriteBufferSize = app.config.WriteBufferSize
app.server.GetOnly = app.config.GETOnly
// unlock application
app.mutex.Unlock()
return app
2020-02-21 18:07:43 +01:00
}
2020-06-07 21:10:38 +02:00
2020-06-27 04:22:22 +02:00
func (app *App) startupMessage(addr string, tls bool, pids string) {
// ignore child processes
2020-09-13 11:20:11 +02:00
if IsChild() {
2020-06-27 04:22:22 +02:00
return
}
2020-09-13 11:20:11 +02:00
2020-07-13 15:40:24 +02:00
// ascii logo
2020-06-27 04:22:22 +02:00
var logo string
2020-09-13 11:20:11 +02:00
// logo += `%s _______ __ %s` + "\n"
// logo += `%s ____%s / ____(_) /_ ___ _____ %s` + "\n"
// logo += `%s_____%s / /_ / / __ \/ _ \/ ___/ %s` + "\n"
// logo += `%s __%s / __/ / / /_/ / __/ / %s` + "\n"
// logo += `%s /_/ /_/_.___/\___/_/%s %s` + "\n"
logo += "\n%s"
logo += " ┌───────────────────────────────────────────────────────┐\n"
logo += " │ %sFiber v%s%s │\n"
logo += " │ Express inspired web framework │\n"
logo += " │ │\n"
logo += " │ Host : %s %s : OS │\n"
logo += " │ Port : %s %s : Threads │\n"
logo += " │ TLS : %s %s : Prefork │\n"
logo += " │ Handlers : %s %s : PID │\n"
logo += " └───────────────────────────────────────────────────────┘"
logo += "%s\n"
const (
cBlack = "\u001b[90m"
cRed = "\u001b[91m"
cCyan = "\u001b[96m"
cGreen = "\u001b[92m"
// cYellow = "\u001b[93m"
// cBlue = "\u001b[94m"
// cMagenta = "\u001b[95m"
// cWhite = "\u001b[97m"
cReset = "\u001b[0m"
)
clrL := func(v interface{}) string {
if v == "disabled" {
return fmt.Sprintf("%s%15v%s", cRed, v, cBlack)
}
if v == "enabled" {
return fmt.Sprintf("%s%15v%s", cGreen, v, cBlack)
}
return fmt.Sprintf("%s%15v%s", cCyan, v, cBlack)
2020-09-01 14:31:43 +02:00
}
2020-09-13 11:20:11 +02:00
clR := func(v interface{}) string {
if v == "disabled" {
return fmt.Sprintf("%s%-15v%s", cRed, v, cBlack)
}
if v == "enabled" {
return fmt.Sprintf("%s%-15v%s", cGreen, v, cBlack)
}
return fmt.Sprintf("%s%-15v%s", cCyan, v, cBlack)
}
host, port := parseAddr(addr)
2020-06-27 04:22:22 +02:00
var (
2020-09-13 11:20:11 +02:00
isTLS = "disabled"
isPrefork = "disabled"
2020-06-27 04:22:22 +02:00
)
2020-09-13 11:20:11 +02:00
2020-06-25 22:48:49 +02:00
if host == "" {
2020-07-04 08:47:50 +02:00
host = "0.0.0.0"
2020-06-25 22:48:49 +02:00
}
2020-06-27 04:22:22 +02:00
if tls {
2020-09-13 11:20:11 +02:00
isTLS = "enabled"
}
if app.config.Prefork {
isPrefork = "enabled"
}
out := colorable.NewColorableStdout()
if os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) {
out = colorable.NewNonColorable(os.Stdout)
}
fmt.Fprintf(out, logo,
cBlack,
cCyan, Version, cBlack,
clR(host), clrL(utils.ToUpper(runtime.GOOS)),
clR(port), clrL(runtime.NumCPU()),
clR(isTLS), clrL(isPrefork),
clR(app.handlerCount), clrL(os.Getpid()),
cReset,
2020-06-27 04:22:22 +02:00
)
2020-09-13 11:20:11 +02:00
2020-06-07 21:10:38 +02:00
}