1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-16 02:52:45 +00:00
fiber/app.go

678 lines
20 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
package fiber
import (
"bufio"
2020-03-04 12:30:29 +01:00
"crypto/tls"
2020-02-21 18:07:43 +01:00
"fmt"
2020-02-27 04:10:26 -05:00
"log"
2020-02-21 18:07:43 +01:00
"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-06-06 07:30:22 +02:00
"sort"
2020-02-21 18:07:43 +01:00
"strconv"
"strings"
"sync"
2020-06-23 15:04:21 +02:00
"text/tabwriter"
2020-02-21 18:07:43 +01:00
"time"
utils "github.com/gofiber/utils"
2020-06-27 04:22:22 +02:00
colorable "github.com/mattn/go-colorable"
2020-07-02 13:51:10 +02:00
isatty "github.com/mattn/go-isatty"
2020-02-21 18:07:43 +01:00
fasthttp "github.com/valyala/fasthttp"
)
2020-03-24 05:31:51 +01:00
// Version of current package
2020-07-04 08:39:45 +02:00
const Version = "1.12.4"
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.
type Handler = func(*Ctx)
// 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
2020-06-06 07:30:22 +02:00
// Amount of registered routes
routes int
// Ctx pool
pool sync.Pool
// Fasthttp server
2020-05-16 05:11:25 +02:00
server *fasthttp.Server
// App settings
2020-07-02 20:26:38 +02:00
Settings *Settings `json:"settings"`
2020-03-24 05:31:51 +01:00
}
// Settings holds is a struct holding the server settings
type Settings struct {
2020-06-06 07:30:22 +02:00
// ErrorHandler is executed when you pass an error in the Next(err) method
// This function is also executed when middleware.Recover() catches a panic
2020-06-06 20:49:02 +02:00
// Default: func(ctx *Ctx, err error) {
2020-06-06 20:42:08 +02:00
// code := StatusInternalServerError
2020-06-06 20:49:02 +02:00
// if e, ok := err.(*Error); ok {
2020-06-06 20:42:08 +02:00
// code = e.Code
// }
2020-06-12 12:29:57 +02:00
// ctx.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
2020-06-06 20:49:02 +02:00
// ctx.Status(code).SendString(err.Error())
2020-06-06 07:30:22 +02:00
// }
2020-07-02 20:26:38 +02:00
ErrorHandler func(*Ctx, error) `json:"-"`
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"`
2020-03-24 05:31:51 +01:00
// Enable strict routing. When enabled, 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"`
// Enable case sensitive routing. When enabled, "/FoO" and "/foo" are 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"`
2020-03-24 05:31:51 +01:00
// Enables handler values to be immutable 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"`
// 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.
2020-06-03 00:01:55 +02:00
// Default value false
2020-07-02 20:26:38 +02:00
ETag bool `json:"etag"`
2020-06-03 00:01:55 +02:00
// This will spawn multiple Go processes listening on the same port
// Default: false
2020-07-02 20:26:38 +02:00
Prefork bool `json:"prefork"`
2020-06-03 00:01:55 +02:00
2020-03-24 05:31:51 +01:00
// 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-04-19 16:10:19 +02:00
// Disable keep-alive connections, the server will close incoming connections after sending the first response to client
2020-06-03 00:01:55 +02:00
// Default: false
2020-07-02 20:26:38 +02:00
DisableKeepalive bool `json:"disable_keep_alive"`
2020-04-19 16:10:19 +02:00
// When set to true causes the default date header to be excluded from the response.
2020-06-03 00:01:55 +02:00
// Default: false
2020-07-02 20:26:38 +02:00
DisableDefaultDate bool `json:"disable_default_date"`
2020-04-19 16:10:19 +02:00
// When set to true, causes the default Content-Type header to be excluded from the Response.
2020-06-03 00:01:55 +02:00
// Default: false
2020-07-02 20:26:38 +02:00
DisableDefaultContentType bool `json:"disable_default_content_type"`
// By default all header names are normalized: conteNT-tYPE -> Content-Type
2020-06-03 00:01:55 +02:00
// Default: false
2020-07-02 20:26:38 +02:00
DisableHeaderNormalizing bool `json:"disable_header_normalizing"`
// When set to true, it will not print out the «Fiber» ASCII art and listening address
2020-06-03 00:01:55 +02:00
// Default: false
2020-07-02 20:26:38 +02:00
DisableStartupMessage bool `json:"disable_startup_message"`
2020-06-12 12:29:57 +02:00
// Templates is deprecated please use Views
2020-06-03 00:01:55 +02:00
// Default: nil
2020-07-02 20:26:38 +02:00
Templates Templates `json:"-"`
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-06-17 13:37:50 +02:00
// FEATURE: v1.13
// 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 struct
type Static struct {
// This works differently than the github.com/gofiber/compression middleware
// The server tries minimizing CPU usage by caching compressed files.
// Optional. Default value false
Compress bool
// Enables byte range requests if set to true.
// Optional. Default value false
ByteRange bool
// Enable directory browsing.
// Optional. Default value false.
Browse bool
// Index file for serving a directory.
// Optional. Default value "index.html".
Index string
}
// default settings
var (
2020-06-15 13:36:50 +02:00
defaultBodyLimit = 4 * 1024 * 1024
defaultConcurrency = 256 * 1024
defaultReadBufferSize = 4096
defaultWriteBufferSize = 4096
defaultErrorHandler = func(ctx *Ctx, err error) {
code := StatusInternalServerError
if e, ok := err.(*Error); ok {
code = e.Code
2020-06-06 07:30:22 +02:00
}
ctx.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
ctx.Status(code).SendString(err.Error())
2020-06-06 07:30:22 +02:00
}
defaultCompressedFileSuffix = ".fiber.gz"
)
2020-03-24 05:31:51 +01:00
// New creates a new Fiber named instance.
2020-03-16 17:18:25 +01:00
// You can pass optional settings when creating a new instance.
2020-03-31 10:03:39 +02:00
func New(settings ...*Settings) *App {
// Create a new app
app := &App{
// Create router stack
2020-06-29 22:51:41 +02:00
stack: make([][]*Route, len(intMethod)),
// Create Ctx pool
pool: sync.Pool{
New: func() interface{} {
return new(Ctx)
},
},
2020-06-07 20:57:55 +02:00
// Set settings
Settings: &Settings{},
}
2020-06-06 20:42:08 +02:00
// Overwrite settings if provided
2020-02-21 18:07:43 +01:00
if len(settings) > 0 {
app.Settings = settings[0]
2020-03-14 12:30:21 +01:00
}
2020-06-07 20:57:55 +02:00
if app.Settings.BodyLimit <= 0 {
app.Settings.BodyLimit = defaultBodyLimit
}
if app.Settings.Concurrency <= 0 {
app.Settings.Concurrency = defaultConcurrency
}
2020-06-15 13:36:50 +02:00
if app.Settings.ReadBufferSize <= 0 {
app.Settings.ReadBufferSize = defaultReadBufferSize
}
if app.Settings.WriteBufferSize <= 0 {
app.Settings.WriteBufferSize = defaultWriteBufferSize
}
2020-06-07 20:57:55 +02:00
// Set default compressed file suffix
if app.Settings.CompressedFileSuffix == "" {
app.Settings.CompressedFileSuffix = defaultCompressedFileSuffix
}
// Set default error
if app.Settings.ErrorHandler == nil {
app.Settings.ErrorHandler = defaultErrorHandler
}
if !app.Settings.Prefork { // Default to -prefork flag if false
2020-06-23 16:56:48 +02:00
app.Settings.Prefork = utils.GetArgument(flagPrefork)
2020-06-07 20:57:55 +02:00
}
// Replace unsafe conversion functions
if app.Settings.Immutable {
2020-06-07 21:19:58 +02:00
getBytes, getString = getBytesImmutable, getStringImmutable
2020-06-07 20:57:55 +02:00
}
2020-06-12 12:29:57 +02:00
// Return app
return app
2020-02-21 18:07:43 +01:00
}
2020-03-24 05:31:51 +01:00
// Use registers a middleware route.
// Middleware matches requests beginning with the provided prefix.
2020-05-16 05:11:25 +02:00
// Providing a prefix is optional, it defaults to "/".
//
// - app.Use(handler)
// - app.Use("/api", handler)
// - app.Use("/api", handler, handler)
func (app *App) Use(args ...interface{}) *Route {
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)
default:
log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg))
2020-02-27 04:10:26 -05:00
}
}
2020-05-16 05:11:25 +02:00
return app.register("USE", prefix, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Get ...
func (app *App) Get(path string, handlers ...Handler) *Route {
return app.Add(MethodGet, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Head ...
func (app *App) Head(path string, handlers ...Handler) *Route {
return app.Add(MethodHead, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Post ...
func (app *App) Post(path string, handlers ...Handler) *Route {
return app.Add(MethodPost, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Put ...
func (app *App) Put(path string, handlers ...Handler) *Route {
return app.Add(MethodPut, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Delete ...
func (app *App) Delete(path string, handlers ...Handler) *Route {
return app.Add(MethodDelete, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Connect ...
func (app *App) Connect(path string, handlers ...Handler) *Route {
return app.Add(MethodConnect, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Options ...
func (app *App) Options(path string, handlers ...Handler) *Route {
return app.Add(MethodOptions, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Trace ...
func (app *App) Trace(path string, handlers ...Handler) *Route {
return app.Add(MethodTrace, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Patch ...
func (app *App) Patch(path string, handlers ...Handler) *Route {
return app.Add(MethodPatch, path, handlers...)
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
// Add ...
func (app *App) Add(method, path string, handlers ...Handler) *Route {
2020-05-16 05:11:25 +02:00
return app.register(method, path, handlers...)
}
// Static ...
func (app *App) Static(prefix, root string, config ...Static) *Route {
return app.registerStatic(prefix, root, config...)
2020-05-16 05:11:25 +02:00
}
// All ...
func (app *App) All(path string, handlers ...Handler) []*Route {
2020-06-29 22:51:41 +02:00
routes := make([]*Route, len(intMethod))
for i, method := range intMethod {
routes[i] = app.Add(method, path, handlers...)
}
return routes
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.
func (app *App) Group(prefix string, handlers ...Handler) *Group {
if len(handlers) > 0 {
app.register("USE", prefix, handlers...)
2020-05-16 05:11:25 +02:00
}
return &Group{prefix: prefix, app: app}
}
2020-06-22 15:12:50 +02:00
// Error makes it compatible with `error` interface.
func (e *Error) Error() string {
return e.Message
}
// NewError creates a new HTTPError instance.
func NewError(code int, message ...string) *Error {
e := &Error{code, utils.StatusMessage(code)}
if len(message) > 0 {
e.Message = message[0]
}
return e
}
// Routes returns all registered routes
//
// for _, r := range app.Routes() {
// fmt.Printf("%s\t%s\n", r.Method, r.Path)
// }
func (app *App) Routes() []*Route {
routes := make([]*Route, 0)
for m := range app.stack {
for r := range app.stack[m] {
// Ignore HEAD routes handling GET routes
if m == 1 && app.stack[m][r].Method == MethodGet {
continue
}
2020-06-25 16:24:21 +02:00
// Don't duplicate USE routes
if app.stack[m][r].Method == "USE" {
duplicate := false
for i := range routes {
if routes[i].Method == "USE" && routes[i].Name == app.stack[m][r].Name {
duplicate = true
}
}
if !duplicate {
routes = append(routes, app.stack[m][r])
}
} else {
routes = append(routes, app.stack[m][r])
}
2020-06-22 15:12:50 +02:00
}
}
// Sort routes by stack position
sort.Slice(routes, func(i, k int) bool {
return routes[i].pos < routes[k].pos
})
return routes
}
2020-04-12 14:58:05 +02:00
// Serve can be used to pass a custom listener
// This method does not support the Prefork feature
2020-06-22 15:12:50 +02:00
// Prefork is not supported using app.Serve(ln net.Listener)
2020-04-12 14:58:05 +02:00
// You can pass an optional *tls.Config to enable TLS.
func (app *App) Serve(ln net.Listener, tlsconfig ...*tls.Config) error {
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
// Update fiber server settings
2020-05-16 05:11:25 +02:00
app.init()
2020-04-12 14:58:05 +02:00
// TLS config
if len(tlsconfig) > 0 {
ln = tls.NewListener(ln, tlsconfig[0])
}
// Print startup message
if !app.Settings.DisableStartupMessage {
2020-06-27 04:22:22 +02:00
app.startupMessage(ln.Addr().String(), len(tlsconfig) > 0, "")
}
2020-04-12 14:58:05 +02:00
return app.server.Serve(ln)
}
2020-03-24 05:31:51 +01:00
// Listen serves HTTP requests from the given addr or port.
// You can pass an optional *tls.Config to enable TLS.
2020-03-31 10:03:39 +02:00
func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error {
2020-06-22 15:12:50 +02:00
// Convert address to string
2020-02-21 18:07:43 +01:00
addr, ok := address.(string)
if !ok {
port, ok := address.(int)
if !ok {
2020-06-08 02:55:19 +02:00
return fmt.Errorf("listen: host must be an `int` port or `string` address")
2020-02-21 18:07:43 +01:00
}
addr = strconv.Itoa(port)
}
if !strings.Contains(addr, ":") {
2020-07-04 08:47:50 +02:00
addr = ":" + addr
2020-02-21 18:07:43 +01: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
// Update fiber server settings
2020-05-16 05:11:25 +02:00
app.init()
2020-06-22 15:12:50 +02:00
// Start prefork
2020-06-22 15:55:24 +02:00
if app.Settings.Prefork {
2020-06-23 16:56:48 +02:00
return app.prefork(addr, tlsconfig...)
2020-02-21 18:07:43 +01:00
}
2020-06-22 15:12:50 +02:00
// Setup listener
ln, err := net.Listen("tcp4", addr)
if err != nil {
return err
}
// Add TLS config if provided
2020-03-04 12:30:29 +01:00
if len(tlsconfig) > 0 {
ln = tls.NewListener(ln, tlsconfig[0])
2020-02-21 18:07:43 +01:00
}
2020-06-25 22:48:49 +02:00
// Print startup message
if !app.Settings.DisableStartupMessage {
2020-06-27 04:22:22 +02:00
app.startupMessage(ln.Addr().String(), len(tlsconfig) > 0, "")
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)
}
// Handler returns the server handler
func (app *App) Handler() fasthttp.RequestHandler {
return app.handler
}
2020-03-24 05:31:51 +01:00
// Shutdown gracefully shuts down the server without interrupting any active connections.
// Shutdown works by first closing all open listeners and then waiting indefinitely for all connections to return to idle and then shut down.
//
// When Shutdown is called, Serve, ListenAndServe, and ListenAndServeTLS immediately return nil.
// 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()
}
2020-04-12 14:58:05 +02:00
// Test is used for internal debugging by passing a *http.Request
// Timeout is optional and defaults to 1s, -1 will disable it completely.
2020-03-31 10:03:39 +02:00
func (app *App) Test(request *http.Request, msTimeout ...int) (*http.Response, error) {
timeout := 1000 // 1 second default
2020-03-22 17:35:12 +01:00
if len(msTimeout) > 0 {
timeout = msTimeout[0]
}
// Add Content-Length if not provided with body
if request.Body != http.NoBody && request.Header.Get("Content-Length") == "" {
request.Header.Add("Content-Length", strconv.FormatInt(request.ContentLength, 10))
}
2020-03-20 16:43:28 +01:00
// Dump raw http request
dump, err := httputil.DumpRequest(request, true)
2020-02-21 18:07:43 +01:00
if err != nil {
return nil, err
}
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
// Update server settings
2020-05-16 05:11:25 +02:00
app.init()
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-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
}
// 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-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
}
// Check for errors
if err != nil {
return nil, err
2020-03-24 03:43:13 +01: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-03-20 16:43:28 +01:00
// Convert raw http response to *http.Response
2020-02-26 19:31:43 -05:00
resp, err := http.ReadResponse(buffer, request)
2020-02-21 18:07:43 +01:00
if err != nil {
return nil, err
}
// Return *http.Response
return resp, nil
}
type disableLogger struct{}
func (dl *disableLogger) Printf(format string, args ...interface{}) {
// fmt.Println(fmt.Sprintf(format, args...))
}
func (app *App) init() *App {
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-06-12 12:29:57 +02:00
// Load view engine if provided
if app.Settings != nil {
// Templates is replaced by Views with layout support
if app.Settings.Templates != nil {
fmt.Println("`Templates` are deprecated since v1.12.x, please us `Views` instead")
}
// Only load templates if an view engine is specified
if app.Settings.Views != nil {
if err := app.Settings.Views.Load(); err != nil {
fmt.Printf("views: %v\n", err)
}
}
}
2020-05-16 05:11:25 +02:00
if app.server == nil {
app.server = &fasthttp.Server{
Logger: &disableLogger{},
LogAllErrors: false,
ErrorHandler: func(fctx *fasthttp.RequestCtx, err error) {
2020-06-06 18:54:02 +02:00
ctx := app.AcquireCtx(fctx)
2020-05-16 05:11:25 +02:00
if _, ok := err.(*fasthttp.ErrSmallBuffer); ok {
2020-06-06 19:02:28 +02:00
ctx.err = ErrRequestHeaderFieldsTooLarge
2020-05-16 05:11:25 +02:00
} else if netErr, ok := err.(*net.OpError); ok && netErr.Timeout() {
2020-06-06 19:02:28 +02:00
ctx.err = ErrRequestTimeout
2020-05-16 05:11:25 +02:00
} else if len(err.Error()) == 33 && err.Error() == "body size exceeds the given limit" {
2020-06-06 19:02:28 +02:00
ctx.err = ErrRequestEntityTooLarge
2020-05-16 05:11:25 +02:00
} else {
2020-06-06 19:02:28 +02:00
ctx.err = ErrBadRequest
2020-05-16 05:11:25 +02:00
}
2020-06-07 10:13:50 +02:00
app.Settings.ErrorHandler(ctx, ctx.err)
2020-06-06 19:02:28 +02:00
app.ReleaseCtx(ctx)
2020-05-16 05:11:25 +02:00
},
}
}
if app.server.Handler == nil {
app.server.Handler = app.handler
2020-02-21 18:07:43 +01:00
}
2020-05-16 05:11:25 +02:00
app.server.Name = app.Settings.ServerHeader
app.server.Concurrency = app.Settings.Concurrency
app.server.NoDefaultDate = app.Settings.DisableDefaultDate
app.server.NoDefaultContentType = app.Settings.DisableDefaultContentType
app.server.DisableHeaderNamesNormalizing = app.Settings.DisableHeaderNormalizing
app.server.DisableKeepalive = app.Settings.DisableKeepalive
app.server.MaxRequestBodySize = app.Settings.BodyLimit
app.server.NoDefaultServerHeader = app.Settings.ServerHeader == ""
app.server.ReadTimeout = app.Settings.ReadTimeout
app.server.WriteTimeout = app.Settings.WriteTimeout
app.server.IdleTimeout = app.Settings.IdleTimeout
2020-06-15 13:36:50 +02:00
app.server.ReadBufferSize = app.Settings.ReadBufferSize
app.server.WriteBufferSize = app.Settings.WriteBufferSize
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.Unlock()
return app
2020-02-21 18:07:43 +01:00
}
2020-06-07 21:10:38 +02:00
2020-06-23 15:04:21 +02:00
const (
2020-06-25 16:02:24 +02:00
cBlack = "\u001b[90m"
// cRed = "\u001b[91m"
2020-06-27 04:22:22 +02:00
// cGreen = "\u001b[92m"
2020-06-25 16:02:24 +02:00
// cYellow = "\u001b[93m"
// cBlue = "\u001b[94m"
// cMagenta = "\u001b[95m"
2020-06-25 22:48:49 +02:00
cCyan = "\u001b[96m"
2020-06-25 16:02:24 +02:00
// cWhite = "\u001b[97m"
cReset = "\u001b[0m"
2020-06-23 15:04:21 +02:00
)
2020-06-27 04:22:22 +02:00
func (app *App) startupMessage(addr string, tls bool, pids string) {
// ignore child processes
if utils.GetArgument(flagChild) {
return
}
//
var logo string
logo += `%s _______ __ %s` + "\n"
logo += `%s ____%s / ____(_) /_ ___ _____ %s` + "\n"
logo += `%s_____%s / /_ / / __ \/ _ \/ ___/ %s` + "\n"
logo += `%s __%s / __/ / / /_/ / __/ / %s` + "\n"
logo += `%s /_/ /_/_.___/\___/_/%s %s` + "\n"
// statup details
var (
host = strings.Split(addr, ":")[0]
port = strings.Split(addr, ":")[1]
tlsStr = "FALSE"
routesLen = len(app.Routes())
osName = utils.ToUpper(runtime.GOOS)
memTotal = utils.ByteSize(utils.MemoryTotal())
cpuCores = runtime.NumCPU()
ppid = os.Getppid()
)
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 {
tlsStr = "TRUE"
}
2020-06-23 15:07:29 +02:00
// tabwriter makes sure the spacing are consistant across different values
// colorable handles the escape sequence for stdout using ascii color codes
2020-07-02 08:50:53 +02:00
var out *tabwriter.Writer
// Check if colors are supported
if os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) {
out = tabwriter.NewWriter(colorable.NewNonColorable(os.Stdout), 0, 0, 2, ' ', 0)
} else {
out = tabwriter.NewWriter(colorable.NewColorableStdout(), 0, 0, 2, ' ', 0)
}
2020-06-27 04:22:22 +02:00
// simple Sprintf function that defaults back to black
cyan := func(v interface{}) string {
return fmt.Sprintf("%s%v%s", cCyan, v, cBlack)
}
// Build startup banner
fmt.Fprintf(out, logo, cBlack, cBlack,
cCyan, cBlack, fmt.Sprintf(" HOST %s\tOS %s", cyan(host), cyan(osName)),
cCyan, cBlack, fmt.Sprintf(" PORT %s\tCORES %s", cyan(port), cyan(cpuCores)),
cCyan, cBlack, fmt.Sprintf(" TLS %s\tMEM %s", cyan(tlsStr), cyan(memTotal)),
cBlack, cyan(Version), fmt.Sprintf(" ROUTES %s\t\t\t PPID %s%s%s\n", cyan(routesLen), cyan(ppid), pids, cReset),
)
// Write to io.write
2020-06-25 15:59:45 +02:00
_ = out.Flush()
2020-06-07 21:10:38 +02:00
}