2020-05-07 19:28:21 +02:00
|
|
|
|
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
|
2020-05-07 20:22:26 +02:00
|
|
|
|
// 🤖 Github Repository: https://github.com/gofiber/fiber
|
2020-05-07 19:28:21 +02:00
|
|
|
|
// 📌 API Documentation: https://docs.gofiber.io
|
2020-02-21 18:07:43 +01:00
|
|
|
|
|
2022-03-03 13:54:56 +03:00
|
|
|
|
// Package fiber is an Express inspired web framework built on top of Fasthttp,
|
2020-07-06 17:12:35 +02:00
|
|
|
|
// 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"
|
2022-06-12 13:33:01 -03:00
|
|
|
|
"bytes"
|
2020-12-16 02:40:28 +01:00
|
|
|
|
"crypto/tls"
|
2022-02-24 10:01:42 +03:00
|
|
|
|
"crypto/x509"
|
2020-12-16 02:40:28 +01:00
|
|
|
|
"errors"
|
2020-02-21 18:07:43 +01:00
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httputil"
|
2020-06-22 15:55:24 +02:00
|
|
|
|
"os"
|
2022-02-24 10:01:42 +03:00
|
|
|
|
"path/filepath"
|
2020-02-27 04:10:26 -05:00
|
|
|
|
"reflect"
|
2020-06-23 15:04:21 +02:00
|
|
|
|
"runtime"
|
2021-12-30 21:13:31 +08:00
|
|
|
|
"sort"
|
2020-02-21 18:07:43 +01:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2020-05-09 15:15:34 +02:00
|
|
|
|
"sync"
|
2021-05-05 23:53:54 +08:00
|
|
|
|
"sync/atomic"
|
2021-12-30 21:13:31 +08:00
|
|
|
|
"text/tabwriter"
|
2020-02-21 18:07:43 +01:00
|
|
|
|
"time"
|
|
|
|
|
|
2022-04-13 11:45:33 +03:00
|
|
|
|
"encoding/json"
|
|
|
|
|
|
2022-05-31 17:35:49 +03:00
|
|
|
|
"github.com/gofiber/fiber/v3/utils"
|
2022-06-02 17:37:53 +03:00
|
|
|
|
"github.com/mattn/go-colorable"
|
|
|
|
|
"github.com/mattn/go-isatty"
|
2020-09-13 11:20:11 +02:00
|
|
|
|
"github.com/valyala/fasthttp"
|
2020-02-21 18:07:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
2020-09-14 06:07:34 +02:00
|
|
|
|
// Version of current fiber package
|
2022-06-01 18:34:22 +03:00
|
|
|
|
const Version = "3.0.0-beta.1"
|
2020-02-21 18:07:43 +01:00
|
|
|
|
|
2020-10-31 07:51:44 +01:00
|
|
|
|
// Handler defines a function to serve HTTP requests.
|
|
|
|
|
type Handler = func(*Ctx) error
|
|
|
|
|
|
2022-05-31 17:41:38 +03:00
|
|
|
|
// Map is a shortcut for map[string]any, useful for JSON returns
|
|
|
|
|
type Map map[string]any
|
2020-03-24 05:31:51 +01:00
|
|
|
|
|
2020-11-18 08:57:27 +01:00
|
|
|
|
// Storage interface for communicating with different database/key-value
|
|
|
|
|
// providers
|
2020-10-26 00:10:35 +00:00
|
|
|
|
type Storage interface {
|
2020-11-18 08:57:27 +01:00
|
|
|
|
// Get gets the value for the given key.
|
2022-02-22 10:10:55 +03:00
|
|
|
|
// `nil, nil` is returned when the key does not exist
|
2020-10-31 06:36:02 +01:00
|
|
|
|
Get(key string) ([]byte, error)
|
|
|
|
|
|
2022-02-22 10:10:55 +03:00
|
|
|
|
// Set stores the given value for the given key along
|
|
|
|
|
// with an expiration value, 0 means no expiration.
|
2020-11-18 08:57:27 +01:00
|
|
|
|
// Empty key or value will be ignored without an error.
|
2022-02-22 10:10:55 +03:00
|
|
|
|
Set(key string, val []byte, exp time.Duration) error
|
2020-10-31 06:36:02 +01:00
|
|
|
|
|
2020-11-18 08:57:27 +01:00
|
|
|
|
// Delete deletes the value for the given key.
|
|
|
|
|
// It returns no error if the storage does not contain the key,
|
2020-10-31 06:36:02 +01:00
|
|
|
|
Delete(key string) error
|
|
|
|
|
|
2020-11-18 08:57:27 +01:00
|
|
|
|
// Reset resets the storage and delete all keys.
|
2020-11-06 01:52:05 +01:00
|
|
|
|
Reset() error
|
|
|
|
|
|
2020-11-18 08:57:27 +01:00
|
|
|
|
// Close closes the storage and will stop any running garbage
|
|
|
|
|
// collectors and open connections.
|
2020-11-06 01:52:05 +01:00
|
|
|
|
Close() error
|
2020-10-26 00:10:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// ErrorHandler defines a function that will process all errors
|
|
|
|
|
// returned from any handlers in the stack
|
2020-09-14 06:07:34 +02:00
|
|
|
|
// 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)
|
2020-09-13 11:20:11 +02:00
|
|
|
|
type ErrorHandler = func(*Ctx, error) error
|
2020-05-24 10:02:21 -04:00
|
|
|
|
|
2020-06-08 13:09:40 +02:00
|
|
|
|
// Error represents an error that occurred while handling a request.
|
|
|
|
|
type Error struct {
|
2022-06-18 20:04:23 +03:00
|
|
|
|
Code int `json:"code"`
|
2022-06-12 19:37:23 +03:00
|
|
|
|
Message any `json:"message"`
|
2020-06-08 13:09:40 +02:00
|
|
|
|
}
|
2020-06-07 20:57:55 +02:00
|
|
|
|
|
2020-03-31 10:03:39 +02:00
|
|
|
|
// App denotes the Fiber application.
|
|
|
|
|
type App struct {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
mutex sync.Mutex
|
2020-06-20 17:26:48 +02:00
|
|
|
|
// Route stack divided by HTTP methods
|
2020-05-23 09:25:18 +02:00
|
|
|
|
stack [][]*Route
|
2020-08-10 10:54:41 +02:00
|
|
|
|
// Route stack divided by HTTP methods and route prefixes
|
2020-08-09 21:53:15 +02:00
|
|
|
|
treeStack []map[string][]*Route
|
2021-01-24 13:02:21 +01:00
|
|
|
|
// contains the information if the route stack has been changed to build the optimized tree
|
|
|
|
|
routesRefreshed bool
|
2020-08-09 21:53:15 +02:00
|
|
|
|
// Amount of registered routes
|
2021-05-05 23:53:54 +08:00
|
|
|
|
routesCount uint32
|
2020-07-22 02:06:32 +02:00
|
|
|
|
// Amount of registered handlers
|
2021-12-29 17:26:50 +08:00
|
|
|
|
handlersCount uint32
|
2020-05-23 09:25:18 +02:00
|
|
|
|
// Ctx pool
|
|
|
|
|
pool sync.Pool
|
2020-05-11 04:30:31 +02:00
|
|
|
|
// 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
|
2021-05-05 17:24:26 -03:00
|
|
|
|
// Converts string to a byte slice
|
|
|
|
|
getBytes func(s string) (b []byte)
|
|
|
|
|
// Converts byte slice to a string
|
|
|
|
|
getString func(b []byte) string
|
2022-02-06 16:58:45 +03:00
|
|
|
|
// Mounted and main apps
|
|
|
|
|
appList map[string]*App
|
2022-03-10 10:35:15 +03:00
|
|
|
|
// Hooks
|
|
|
|
|
hooks *hooks
|
|
|
|
|
// Latest route & group
|
|
|
|
|
latestRoute *Route
|
|
|
|
|
latestGroup *Group
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-06-03 00:01:55 +02:00
|
|
|
|
// Default: ""
|
2020-07-02 20:26:38 +02:00
|
|
|
|
ServerHeader string `json:"server_header"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-07-12 23:29:15 +09:00
|
|
|
|
// When set to true, the router treats "/foo" and "/foo/" as different.
|
2020-05-23 09:25:18 +02:00
|
|
|
|
// By default this is disabled and both "/foo" and "/foo/" will execute the same handler.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
|
|
|
|
// Default: false
|
2020-07-02 20:26:38 +02:00
|
|
|
|
StrictRouting bool `json:"strict_routing"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-07-12 23:29:15 +09:00
|
|
|
|
// When set to true, enables case sensitive routing.
|
|
|
|
|
// E.g. "/FoO" and "/foo" are treated as different routes.
|
2020-05-23 09:25:18 +02:00
|
|
|
|
// By default this is disabled and both "/FoO" and "/foo" will execute the same handler.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
|
|
|
|
// Default: false
|
2020-07-02 20:26:38 +02:00
|
|
|
|
CaseSensitive bool `json:"case_sensitive"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-07-12 23:29:15 +09:00
|
|
|
|
// 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-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-06-03 00:01:55 +02:00
|
|
|
|
// Default: false
|
2020-07-02 20:26:38 +02:00
|
|
|
|
Immutable bool `json:"immutable"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-07-12 23:29:15 +09:00
|
|
|
|
// When set to true, converts all encoded characters in the route back
|
2021-01-06 13:28:48 +01:00
|
|
|
|
// before setting the path for the context, so that the routing,
|
|
|
|
|
// the returning of the current url from the context `ctx.Path()`
|
2021-07-17 22:20:14 +07:00
|
|
|
|
// and the parameters `ctx.Params(%key%)` with decoded characters will work
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-06-25 08:44:29 +02:00
|
|
|
|
// Default: false
|
2020-07-02 20:26:38 +02:00
|
|
|
|
UnescapePath bool `json:"unescape_path"`
|
2020-06-25 08:44:29 +02:00
|
|
|
|
|
2020-07-12 23:29:15 +09:00
|
|
|
|
// Max body size that the server accepts.
|
2020-11-18 08:57:27 +01:00
|
|
|
|
// -1 will decline any body size
|
|
|
|
|
//
|
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-05-23 09:25:18 +02:00
|
|
|
|
|
2020-04-19 16:10:19 +02:00
|
|
|
|
// Maximum number of concurrent connections.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-06-03 00:01:55 +02:00
|
|
|
|
// Default: 256 * 1024
|
2020-07-02 20:26:38 +02:00
|
|
|
|
Concurrency int `json:"concurrency"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-06-12 12:29:57 +02:00
|
|
|
|
// Views is the interface that wraps the Render function.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-06-12 12:29:57 +02:00
|
|
|
|
// Default: nil
|
2020-07-02 20:26:38 +02:00
|
|
|
|
Views Views `json:"-"`
|
2020-06-12 12:29:57 +02:00
|
|
|
|
|
2021-06-14 14:55:49 +05:45
|
|
|
|
// Views Layout is the global layout for all template render until override on Render function.
|
|
|
|
|
//
|
|
|
|
|
// Default: ""
|
|
|
|
|
ViewsLayout string `json:"views_layout"`
|
|
|
|
|
|
2022-01-24 08:29:46 +01:00
|
|
|
|
// PassLocalsToViews Enables passing of the locals set on a fiber.Ctx to the template engine
|
|
|
|
|
//
|
|
|
|
|
// Default: false
|
|
|
|
|
PassLocalsToViews bool `json:"pass_locals_to_views"`
|
|
|
|
|
|
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-09-17 01:39:42 +02:00
|
|
|
|
//
|
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-05-23 09:25:18 +02:00
|
|
|
|
|
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-09-17 01:39:42 +02:00
|
|
|
|
//
|
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-05-23 09:25:18 +02:00
|
|
|
|
|
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-09-17 01:39:42 +02:00
|
|
|
|
//
|
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).
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-07-12 23:29:15 +09:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-07-12 23:29:15 +09:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-06-07 20:35:41 +02:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
|
|
|
|
// Default: false
|
2020-09-13 11:20:11 +02:00
|
|
|
|
GETOnly bool `json:"get_only"`
|
|
|
|
|
|
|
|
|
|
// ErrorHandler is executed when an error is returned from fiber.Handler.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
|
|
|
|
// Default: DefaultErrorHandler
|
2020-09-13 11:20:11 +02:00
|
|
|
|
ErrorHandler ErrorHandler `json:"-"`
|
|
|
|
|
|
|
|
|
|
// When set to true, disables keep-alive connections.
|
|
|
|
|
// The server will close incoming connections after sending the first response to client.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// Default: false
|
|
|
|
|
DisableDefaultDate bool `json:"disable_default_date"`
|
|
|
|
|
|
|
|
|
|
// When set to true, causes the default Content-Type header to be excluded from the response.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// Default: false
|
|
|
|
|
DisableHeaderNormalizing bool `json:"disable_header_normalizing"`
|
|
|
|
|
|
|
|
|
|
// When set to true, it will not print out the «Fiber» ASCII art and listening address.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// Default: false
|
|
|
|
|
DisableStartupMessage bool `json:"disable_startup_message"`
|
|
|
|
|
|
2021-07-16 12:29:11 +05:45
|
|
|
|
// This function allows to setup app name for the app
|
|
|
|
|
//
|
|
|
|
|
// Default: nil
|
|
|
|
|
AppName string `json:"app_name"`
|
|
|
|
|
|
2021-07-11 11:45:46 +02:00
|
|
|
|
// StreamRequestBody enables request body streaming,
|
|
|
|
|
// and calls the handler sooner when given body is
|
|
|
|
|
// larger then the current limit.
|
|
|
|
|
StreamRequestBody bool
|
|
|
|
|
|
|
|
|
|
// Will not pre parse Multipart Form data if set to true.
|
|
|
|
|
//
|
|
|
|
|
// This option is useful for servers that desire to treat
|
|
|
|
|
// multipart form data as a binary blob, or choose when to parse the data.
|
|
|
|
|
//
|
|
|
|
|
// Server pre parses multipart form data by default.
|
|
|
|
|
DisablePreParseMultipartForm bool
|
|
|
|
|
|
2020-10-07 11:55:17 +02:00
|
|
|
|
// Aggressively reduces memory usage at the cost of higher CPU usage
|
|
|
|
|
// if set to true.
|
|
|
|
|
//
|
|
|
|
|
// Try enabling this option only if the server consumes too much memory
|
|
|
|
|
// serving mostly idle keep-alive connections. This may reduce memory
|
|
|
|
|
// usage by more than 50%.
|
|
|
|
|
//
|
|
|
|
|
// Default: false
|
|
|
|
|
ReduceMemoryUsage bool `json:"reduce_memory_usage"`
|
|
|
|
|
|
2020-11-11 14:18:19 +01:00
|
|
|
|
// FEATURE: v2.3.x
|
2020-05-23 09:25:18 +02:00
|
|
|
|
// 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.
|
2020-09-17 01:39:42 +02:00
|
|
|
|
//
|
|
|
|
|
// Default: false
|
2020-05-23 09:25:18 +02:00
|
|
|
|
// RedirectFixedPath bool
|
2021-01-19 19:42:51 -08:00
|
|
|
|
|
|
|
|
|
// When set by an external client of Fiber it will use the provided implementation of a
|
2021-01-23 02:15:59 -06:00
|
|
|
|
// JSONMarshal
|
2021-01-19 19:42:51 -08:00
|
|
|
|
//
|
2021-01-23 02:15:59 -06:00
|
|
|
|
// Allowing for flexibility in using another json library for encoding
|
|
|
|
|
// Default: json.Marshal
|
|
|
|
|
JSONEncoder utils.JSONMarshal `json:"-"`
|
2021-02-07 13:55:13 +08:00
|
|
|
|
|
2021-08-18 17:56:07 +05:30
|
|
|
|
// When set by an external client of Fiber it will use the provided implementation of a
|
|
|
|
|
// JSONUnmarshal
|
|
|
|
|
//
|
2022-02-01 22:42:23 +03:00
|
|
|
|
// Allowing for flexibility in using another json library for decoding
|
2021-08-18 17:56:07 +05:30
|
|
|
|
// Default: json.Unmarshal
|
|
|
|
|
JSONDecoder utils.JSONUnmarshal `json:"-"`
|
|
|
|
|
|
2021-02-07 13:55:13 +08:00
|
|
|
|
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)
|
|
|
|
|
// WARNING: When prefork is set to true, only "tcp4" and "tcp6" can be chose.
|
|
|
|
|
//
|
2021-02-08 11:35:17 +08:00
|
|
|
|
// Default: NetworkTCP4
|
2021-02-07 13:55:13 +08:00
|
|
|
|
Network string
|
2021-06-30 09:03:45 +03:00
|
|
|
|
|
|
|
|
|
// If you find yourself behind some sort of proxy, like a load balancer,
|
|
|
|
|
// then certain header information may be sent to you using special X-Forwarded-* headers or the Forwarded header.
|
|
|
|
|
// For example, the Host HTTP header is usually used to return the requested host.
|
|
|
|
|
// But when you’re behind a proxy, the actual host may be stored in an X-Forwarded-Host header.
|
|
|
|
|
//
|
|
|
|
|
// If you are behind a proxy, you should enable TrustedProxyCheck to prevent header spoofing.
|
|
|
|
|
// If you enable EnableTrustedProxyCheck and leave TrustedProxies empty Fiber will skip
|
|
|
|
|
// all headers that could be spoofed.
|
|
|
|
|
// If request ip in TrustedProxies whitelist then:
|
|
|
|
|
// 1. c.Protocol() get value from X-Forwarded-Proto, X-Forwarded-Protocol, X-Forwarded-Ssl or X-Url-Scheme header
|
|
|
|
|
// 2. c.IP() get value from ProxyHeader header.
|
|
|
|
|
// 3. c.Hostname() get value from X-Forwarded-Host header
|
|
|
|
|
// But if request ip NOT in Trusted Proxies whitelist then:
|
|
|
|
|
// 1. c.Protocol() WON't get value from X-Forwarded-Proto, X-Forwarded-Protocol, X-Forwarded-Ssl or X-Url-Scheme header,
|
|
|
|
|
// will return https in case when tls connection is handled by the app, of http otherwise
|
|
|
|
|
// 2. c.IP() WON'T get value from ProxyHeader header, will return RemoteIP() from fasthttp context
|
|
|
|
|
// 3. c.Hostname() WON'T get value from X-Forwarded-Host header, fasthttp.Request.URI().Host()
|
|
|
|
|
// will be used to get the hostname.
|
|
|
|
|
//
|
|
|
|
|
// Default: false
|
|
|
|
|
EnableTrustedProxyCheck bool `json:"enable_trusted_proxy_check"`
|
|
|
|
|
|
|
|
|
|
// Read EnableTrustedProxyCheck doc.
|
|
|
|
|
//
|
|
|
|
|
// Default: []string
|
2021-12-31 12:32:39 -05:00
|
|
|
|
TrustedProxies []string `json:"trusted_proxies"`
|
|
|
|
|
trustedProxiesMap map[string]struct{}
|
|
|
|
|
trustedProxyRanges []*net.IPNet
|
2021-12-30 21:13:31 +08:00
|
|
|
|
|
2022-02-03 08:09:11 -05:00
|
|
|
|
// If set to true, will print all routes with their method, path and handler.
|
2021-12-30 21:13:31 +08:00
|
|
|
|
// Default: false
|
2022-01-10 21:22:44 +08:00
|
|
|
|
EnablePrintRoutes bool `json:"enable_print_routes"`
|
2020-03-22 01:51:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 00:41:19 +09:00
|
|
|
|
// Static defines configuration options when defining static assets.
|
2020-04-28 21:34:34 +02:00
|
|
|
|
type Static struct {
|
2020-07-13 00:41:19 +09:00
|
|
|
|
// When set to true, the server tries minimizing CPU usage by caching compressed files.
|
|
|
|
|
// This works differently than the github.com/gofiber/compression middleware.
|
2020-04-28 21:34:34 +02:00
|
|
|
|
// Optional. Default value false
|
2020-09-13 11:20:11 +02:00
|
|
|
|
Compress bool `json:"compress"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-07-13 00:41:19 +09:00
|
|
|
|
// When set to true, enables byte range requests.
|
2020-04-28 21:34:34 +02:00
|
|
|
|
// Optional. Default value false
|
2020-09-13 11:20:11 +02:00
|
|
|
|
ByteRange bool `json:"byte_range"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-07-13 00:41:19 +09:00
|
|
|
|
// When set to true, enables directory browsing.
|
2020-04-28 21:34:34 +02:00
|
|
|
|
// Optional. Default value false.
|
2020-09-13 11:20:11 +02:00
|
|
|
|
Browse bool `json:"browse"`
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2022-02-03 08:41:45 -05:00
|
|
|
|
// When set to true, enables direct download.
|
|
|
|
|
// Optional. Default value false.
|
|
|
|
|
Download bool `json:"download"`
|
|
|
|
|
|
2020-07-13 00:41:19 +09:00
|
|
|
|
// The name of the index file for serving a directory.
|
2020-04-28 21:34:34 +02:00
|
|
|
|
// Optional. Default value "index.html".
|
2020-09-13 11:20:11 +02:00
|
|
|
|
Index string `json:"index"`
|
2020-10-07 10:20:19 +02:00
|
|
|
|
|
2020-11-11 14:18:19 +01:00
|
|
|
|
// Expiration duration for inactive file handlers.
|
|
|
|
|
// Use a negative time.Duration to disable it.
|
|
|
|
|
//
|
|
|
|
|
// Optional. Default value 10 * time.Second.
|
|
|
|
|
CacheDuration time.Duration `json:"cache_duration"`
|
|
|
|
|
|
2020-10-07 10:20:19 +02:00
|
|
|
|
// The value for the Cache-Control HTTP-header
|
|
|
|
|
// that is set on the file response. MaxAge is defined in seconds.
|
|
|
|
|
//
|
|
|
|
|
// Optional. Default value 0.
|
|
|
|
|
MaxAge int `json:"max_age"`
|
2021-01-22 23:07:22 +01:00
|
|
|
|
|
|
|
|
|
// Next defines a function to skip this middleware when returned true.
|
|
|
|
|
//
|
|
|
|
|
// Optional. Default: nil
|
|
|
|
|
Next func(c *Ctx) bool
|
2020-04-28 21:34:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 21:13:31 +08:00
|
|
|
|
// RouteMessage is some message need to be print when server starts
|
|
|
|
|
type RouteMessage struct {
|
|
|
|
|
name string
|
|
|
|
|
method string
|
|
|
|
|
path string
|
|
|
|
|
handlers string
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 11:39:55 +02:00
|
|
|
|
// Default Config values
|
2020-07-13 00:49:56 +09:00
|
|
|
|
const (
|
2020-09-13 11:20:11 +02:00
|
|
|
|
DefaultBodyLimit = 4 * 1024 * 1024
|
|
|
|
|
DefaultConcurrency = 256 * 1024
|
|
|
|
|
DefaultReadBufferSize = 4096
|
|
|
|
|
DefaultWriteBufferSize = 4096
|
|
|
|
|
DefaultCompressedFileSuffix = ".fiber.gz"
|
2020-06-08 13:09:40 +02:00
|
|
|
|
)
|
2020-05-23 09:25:18 +02:00
|
|
|
|
|
2020-10-25 12:23:10 +01:00
|
|
|
|
// DefaultErrorHandler that process return errors from handlers
|
2020-09-13 11:20:11 +02:00
|
|
|
|
var DefaultErrorHandler = func(c *Ctx, err error) error {
|
2020-07-13 00:49:56 +09:00
|
|
|
|
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-07-13 00:49:56 +09:00
|
|
|
|
}
|
|
|
|
|
|
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{
|
2020-07-13 01:01:51 +09:00
|
|
|
|
// Prefork: true,
|
|
|
|
|
// ServerHeader: "Fiber",
|
|
|
|
|
// })
|
2020-09-13 11:20:11 +02:00
|
|
|
|
func New(config ...Config) *App {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
// Create a new app
|
|
|
|
|
app := &App{
|
|
|
|
|
// Create router stack
|
2020-08-09 21:53:15 +02:00
|
|
|
|
stack: make([][]*Route, len(intMethod)),
|
|
|
|
|
treeStack: make([]map[string][]*Route, len(intMethod)),
|
2020-05-23 09:25:18 +02:00
|
|
|
|
// Create Ctx pool
|
|
|
|
|
pool: sync.Pool{
|
2022-05-31 17:41:38 +03:00
|
|
|
|
New: func() any {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
return new(Ctx)
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// Create config
|
2022-03-10 10:35:15 +03:00
|
|
|
|
config: Config{},
|
|
|
|
|
getBytes: utils.UnsafeBytes,
|
|
|
|
|
getString: utils.UnsafeString,
|
|
|
|
|
appList: make(map[string]*App),
|
|
|
|
|
latestRoute: &Route{},
|
|
|
|
|
latestGroup: &Group{},
|
2020-05-23 09:25:18 +02:00
|
|
|
|
}
|
2022-03-10 10:35:15 +03:00
|
|
|
|
|
|
|
|
|
// Define hooks
|
|
|
|
|
app.hooks = newHooks(app)
|
|
|
|
|
|
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-10-15 16:53:29 +08:00
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// Override default values
|
2020-11-18 08:57:27 +01:00
|
|
|
|
if app.config.BodyLimit == 0 {
|
2020-09-13 11:20:11 +02:00
|
|
|
|
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 {
|
2021-05-05 17:24:26 -03:00
|
|
|
|
app.getBytes, app.getString = getBytesImmutable, getStringImmutable
|
2020-06-07 20:57:55 +02:00
|
|
|
|
}
|
2021-10-05 08:03:20 -04:00
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
|
if app.config.ErrorHandler == nil {
|
|
|
|
|
app.config.ErrorHandler = DefaultErrorHandler
|
|
|
|
|
}
|
2021-10-05 08:03:20 -04:00
|
|
|
|
|
2021-01-23 02:15:59 -06:00
|
|
|
|
if app.config.JSONEncoder == nil {
|
|
|
|
|
app.config.JSONEncoder = json.Marshal
|
2021-01-19 19:42:51 -08:00
|
|
|
|
}
|
2021-08-18 17:56:07 +05:30
|
|
|
|
if app.config.JSONDecoder == nil {
|
|
|
|
|
app.config.JSONDecoder = json.Unmarshal
|
|
|
|
|
}
|
2021-02-07 13:55:13 +08:00
|
|
|
|
if app.config.Network == "" {
|
2021-02-08 11:35:17 +08:00
|
|
|
|
app.config.Network = NetworkTCP4
|
2021-02-07 13:55:13 +08:00
|
|
|
|
}
|
2020-11-18 08:57:27 +01:00
|
|
|
|
|
2021-06-30 09:03:45 +03:00
|
|
|
|
app.config.trustedProxiesMap = make(map[string]struct{}, len(app.config.TrustedProxies))
|
2021-12-31 12:32:39 -05:00
|
|
|
|
for _, ipAddress := range app.config.TrustedProxies {
|
|
|
|
|
app.handleTrustedProxy(ipAddress)
|
2021-06-30 09:03:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 16:58:45 +03:00
|
|
|
|
// Init appList
|
|
|
|
|
app.appList[""] = app
|
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// Init app
|
|
|
|
|
app.init()
|
2020-11-18 08:57:27 +01:00
|
|
|
|
|
2020-06-12 12:29:57 +02:00
|
|
|
|
// Return app
|
|
|
|
|
return app
|
2020-02-21 18:07:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-31 12:32:39 -05:00
|
|
|
|
// Adds an ip address to trustedProxyRanges or trustedProxiesMap based on whether it is an IP range or not
|
2021-10-28 01:46:39 +03:00
|
|
|
|
func (app *App) handleTrustedProxy(ipAddress string) {
|
|
|
|
|
if strings.Contains(ipAddress, "/") {
|
2021-12-31 12:32:39 -05:00
|
|
|
|
_, ipNet, err := net.ParseCIDR(ipAddress)
|
|
|
|
|
|
2021-10-28 01:46:39 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("[Warning] IP range `%s` could not be parsed. \n", ipAddress)
|
|
|
|
|
}
|
2021-12-31 12:32:39 -05:00
|
|
|
|
|
|
|
|
|
app.config.trustedProxyRanges = append(app.config.trustedProxyRanges, ipNet)
|
|
|
|
|
} else {
|
|
|
|
|
app.config.trustedProxiesMap[ipAddress] = struct{}{}
|
2021-10-28 01:46:39 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 13:49:13 +08:00
|
|
|
|
// Mount attaches another app instance as a sub-router along a routing path.
|
2020-09-26 11:24:25 +02:00
|
|
|
|
// It's very useful to split up a large API as many independent routers and
|
2021-10-05 08:03:20 -04:00
|
|
|
|
// compose them as a single service using Mount. The fiber's error handler and
|
|
|
|
|
// any of the fiber's sub apps are added to the application's error handlers
|
|
|
|
|
// to be invoked on errors that happen within the prefix route.
|
2020-09-26 11:24:25 +02:00
|
|
|
|
func (app *App) Mount(prefix string, fiber *App) Router {
|
|
|
|
|
stack := fiber.Stack()
|
2022-02-06 16:58:45 +03:00
|
|
|
|
prefix = strings.TrimRight(prefix, "/")
|
2022-04-05 08:40:22 +02:00
|
|
|
|
if prefix == "" {
|
|
|
|
|
prefix = "/"
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 11:24:25 +02:00
|
|
|
|
for m := range stack {
|
|
|
|
|
for r := range stack[m] {
|
|
|
|
|
route := app.copyRoute(stack[m][r])
|
|
|
|
|
app.addRoute(route.Method, app.addPrefixToRoute(prefix, route))
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-04 09:21:04 +02:00
|
|
|
|
|
2022-02-06 16:58:45 +03:00
|
|
|
|
// Support for configs of mounted-apps and sub-mounted-apps
|
|
|
|
|
for mountedPrefixes, subApp := range fiber.appList {
|
|
|
|
|
app.appList[prefix+mountedPrefixes] = subApp
|
|
|
|
|
subApp.init()
|
2021-10-05 08:03:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 17:26:50 +08:00
|
|
|
|
atomic.AddUint32(&app.handlersCount, fiber.handlersCount)
|
2021-05-04 09:21:04 +02:00
|
|
|
|
|
2020-09-26 11:24:25 +02:00
|
|
|
|
return app
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 16:04:04 +03:00
|
|
|
|
// Assign name to specific route.
|
|
|
|
|
func (app *App) Name(name string) Router {
|
2022-03-10 10:35:15 +03:00
|
|
|
|
app.mutex.Lock()
|
|
|
|
|
if strings.HasPrefix(app.latestRoute.path, app.latestGroup.Prefix) {
|
|
|
|
|
app.latestRoute.Name = app.latestGroup.name + name
|
2021-12-28 16:04:04 +03:00
|
|
|
|
} else {
|
2022-03-10 10:35:15 +03:00
|
|
|
|
app.latestRoute.Name = name
|
2021-12-28 16:04:04 +03:00
|
|
|
|
}
|
2022-03-10 10:35:15 +03:00
|
|
|
|
|
|
|
|
|
if err := app.hooks.executeOnNameHooks(*app.latestRoute); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
app.mutex.Unlock()
|
|
|
|
|
|
2021-12-28 16:04:04 +03:00
|
|
|
|
return app
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get route by name
|
|
|
|
|
func (app *App) GetRoute(name string) Route {
|
|
|
|
|
for _, routes := range app.stack {
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
if route.Name == name {
|
|
|
|
|
return *route
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Route{}
|
|
|
|
|
}
|
|
|
|
|
|
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...
|
2022-05-31 17:41:38 +03:00
|
|
|
|
func (app *App) Use(args ...any) Router {
|
2020-05-16 05:11:25 +02:00
|
|
|
|
var prefix string
|
2020-05-24 10:02:21 -04:00
|
|
|
|
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
|
2020-05-24 10:02:21 -04:00
|
|
|
|
case Handler:
|
2020-02-27 04:10:26 -05:00
|
|
|
|
handlers = append(handlers, arg)
|
|
|
|
|
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...)
|
2020-07-19 10:04:21 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Get(path string, handlers ...Handler) Router {
|
2022-05-01 10:34:53 +03:00
|
|
|
|
return app.Head(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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Head(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Post(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Put(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Delete(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Connect(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Options(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Trace(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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.
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Patch(path string, handlers ...Handler) Router {
|
2020-05-23 09:25:18 +02:00
|
|
|
|
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
|
2020-07-19 10:04:21 +02:00
|
|
|
|
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
|
2020-07-19 10:04:21 +02:00
|
|
|
|
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
|
2020-07-19 10:04:21 +02:00
|
|
|
|
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...)
|
2020-05-23 09:25:18 +02:00
|
|
|
|
}
|
2020-07-19 10:04:21 +02:00
|
|
|
|
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)
|
2020-07-19 10:04:21 +02:00
|
|
|
|
func (app *App) Group(prefix string, handlers ...Handler) Router {
|
2020-03-22 01:51:53 +01:00
|
|
|
|
if len(handlers) > 0 {
|
2020-07-12 23:54:22 +08:00
|
|
|
|
app.register(methodUse, prefix, handlers...)
|
2020-05-16 05:11:25 +02:00
|
|
|
|
}
|
2022-03-10 10:35:15 +03:00
|
|
|
|
grp := &Group{Prefix: prefix, app: app}
|
|
|
|
|
if err := app.hooks.executeOnGroupHooks(*grp); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return grp
|
2020-03-22 01:51:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 18:37:28 +03:00
|
|
|
|
// Route is used to define routes with a common prefix inside the common function.
|
|
|
|
|
// Uses Group method to define new sub-router.
|
|
|
|
|
func (app *App) Route(prefix string, fn func(router Router), name ...string) Router {
|
|
|
|
|
// Create new group
|
|
|
|
|
group := app.Group(prefix)
|
|
|
|
|
if len(name) > 0 {
|
|
|
|
|
group.Name(name[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Define routes
|
|
|
|
|
fn(group)
|
|
|
|
|
|
|
|
|
|
return group
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2022-06-12 19:37:23 +03:00
|
|
|
|
return fmt.Sprint(e.Message)
|
2020-06-22 15:12:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-12 19:37:23 +03:00
|
|
|
|
// NewErrors creates multiple/single new Error instances.
|
|
|
|
|
// If you want to pass single message, you have to pass 1 message.
|
|
|
|
|
// To pass multiple error messages, you have to pass +2 messages.
|
|
|
|
|
func NewErrors(code int, messages ...any) *Error {
|
|
|
|
|
e := &Error{
|
2022-01-27 14:41:10 +03:00
|
|
|
|
Code: code,
|
|
|
|
|
Message: utils.StatusMessage(code),
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
2022-06-12 19:37:23 +03:00
|
|
|
|
|
|
|
|
|
if len(messages) == 1 {
|
|
|
|
|
e.Message = messages[0]
|
|
|
|
|
} else if len(messages) > 1 {
|
|
|
|
|
e.Message = messages
|
2020-06-22 15:12:50 +02:00
|
|
|
|
}
|
2022-06-18 20:04:23 +03:00
|
|
|
|
|
2022-06-12 19:37:23 +03:00
|
|
|
|
return e
|
2022-01-10 21:22:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 01:01:51 +09:00
|
|
|
|
// 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 {
|
2021-02-07 13:55:13 +08:00
|
|
|
|
addr, tlsConfig := lnMetadata(app.config.Network, ln)
|
|
|
|
|
return app.prefork(app.config.Network, addr, tlsConfig)
|
2020-04-12 14:58:05 +02:00
|
|
|
|
}
|
2021-01-24 13:02:21 +01:00
|
|
|
|
// prepare the server for the start
|
|
|
|
|
app.startupProcess()
|
2020-05-29 23:41:03 +02:00
|
|
|
|
// Print startup message
|
2020-09-13 11:20:11 +02:00
|
|
|
|
if !app.config.DisableStartupMessage {
|
2021-02-23 16:40:17 +08:00
|
|
|
|
app.startupMessage(ln.Addr().String(), getTlsConfig(ln) != nil, "")
|
2020-04-27 23:34:35 +02:00
|
|
|
|
}
|
2021-12-30 21:13:31 +08:00
|
|
|
|
// Print routes
|
|
|
|
|
if app.config.EnablePrintRoutes {
|
|
|
|
|
app.printRoutesMessage()
|
|
|
|
|
}
|
2020-12-16 02:40:28 +01:00
|
|
|
|
// Start listening
|
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
|
|
|
|
//
|
2020-07-13 01:01:51 +09: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 {
|
2021-02-07 13:55:13 +08:00
|
|
|
|
return app.prefork(app.config.Network, addr, nil)
|
2020-07-18 01:17:52 +02:00
|
|
|
|
}
|
2020-06-22 15:12:50 +02:00
|
|
|
|
// Setup listener
|
2021-02-07 13:55:13 +08:00
|
|
|
|
ln, err := net.Listen(app.config.Network, addr)
|
2020-06-22 15:12:50 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-01-24 13:02:21 +01:00
|
|
|
|
// prepare the server for the start
|
|
|
|
|
app.startupProcess()
|
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
|
|
|
|
}
|
2021-12-30 21:13:31 +08:00
|
|
|
|
// Print routes
|
|
|
|
|
if app.config.EnablePrintRoutes {
|
|
|
|
|
app.printRoutesMessage()
|
|
|
|
|
}
|
2020-06-22 15:12:50 +02:00
|
|
|
|
// Start listening
|
2020-02-21 18:07:43 +01:00
|
|
|
|
return app.server.Serve(ln)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 15:55:00 +03:00
|
|
|
|
// ListenTLS serves HTTPS requests from the given addr.
|
|
|
|
|
// certFile and keyFile are the paths to TLS certificate and key file:
|
2020-12-16 03:03:15 +01:00
|
|
|
|
// app.ListenTLS(":8080", "./cert.pem", "./cert.key")
|
2020-12-16 02:40:28 +01:00
|
|
|
|
func (app *App) ListenTLS(addr, certFile, keyFile string) error {
|
|
|
|
|
// Check for valid cert/key path
|
2020-12-16 03:03:15 +01:00
|
|
|
|
if len(certFile) == 0 || len(keyFile) == 0 {
|
2020-12-16 02:54:57 +01:00
|
|
|
|
return errors.New("tls: provide a valid cert or key path")
|
2020-12-16 02:40:28 +01:00
|
|
|
|
}
|
|
|
|
|
// Prefork is supported
|
|
|
|
|
if app.config.Prefork {
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
|
|
|
|
if err != nil {
|
2020-12-16 02:54:57 +01:00
|
|
|
|
return fmt.Errorf("tls: cannot load TLS key pair from certFile=%q and keyFile=%q: %s", certFile, keyFile, err)
|
2020-12-16 02:40:28 +01:00
|
|
|
|
}
|
|
|
|
|
config := &tls.Config{
|
2022-02-24 10:01:42 +03:00
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2020-12-16 02:40:28 +01:00
|
|
|
|
Certificates: []tls.Certificate{
|
|
|
|
|
cert,
|
|
|
|
|
},
|
|
|
|
|
}
|
2021-02-07 13:55:13 +08:00
|
|
|
|
return app.prefork(app.config.Network, addr, config)
|
2020-12-16 02:40:28 +01:00
|
|
|
|
}
|
|
|
|
|
// Setup listener
|
2021-02-07 13:55:13 +08:00
|
|
|
|
ln, err := net.Listen(app.config.Network, addr)
|
2020-12-16 02:40:28 +01:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-01-24 13:02:21 +01:00
|
|
|
|
// prepare the server for the start
|
|
|
|
|
app.startupProcess()
|
2020-12-16 02:45:34 +01:00
|
|
|
|
// Print startup message
|
|
|
|
|
if !app.config.DisableStartupMessage {
|
|
|
|
|
app.startupMessage(ln.Addr().String(), true, "")
|
|
|
|
|
}
|
2021-12-30 21:13:31 +08:00
|
|
|
|
// Print routes
|
|
|
|
|
if app.config.EnablePrintRoutes {
|
|
|
|
|
app.printRoutesMessage()
|
|
|
|
|
}
|
2020-12-16 02:40:28 +01:00
|
|
|
|
// Start listening
|
|
|
|
|
return app.server.ServeTLS(ln, certFile, keyFile)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 15:55:00 +03:00
|
|
|
|
// ListenMutualTLS serves HTTPS requests from the given addr.
|
|
|
|
|
// certFile, keyFile and clientCertFile are the paths to TLS certificate and key file:
|
2022-02-24 10:01:42 +03:00
|
|
|
|
// app.ListenMutualTLS(":8080", "./cert.pem", "./cert.key", "./client.pem")
|
|
|
|
|
func (app *App) ListenMutualTLS(addr, certFile, keyFile, clientCertFile string) error {
|
|
|
|
|
// Check for valid cert/key path
|
|
|
|
|
if len(certFile) == 0 || len(keyFile) == 0 {
|
|
|
|
|
return errors.New("tls: provide a valid cert or key path")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("tls: cannot load TLS key pair from certFile=%q and keyFile=%q: %s", certFile, keyFile, err)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 18:34:22 +03:00
|
|
|
|
clientCACert, err := os.ReadFile(filepath.Clean(clientCertFile))
|
2022-02-24 10:01:42 +03:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
clientCertPool := x509.NewCertPool()
|
|
|
|
|
clientCertPool.AppendCertsFromPEM(clientCACert)
|
|
|
|
|
|
|
|
|
|
config := &tls.Config{
|
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
|
ClientCAs: clientCertPool,
|
|
|
|
|
Certificates: []tls.Certificate{
|
|
|
|
|
cert,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prefork is supported
|
|
|
|
|
if app.config.Prefork {
|
|
|
|
|
return app.prefork(app.config.Network, addr, config)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup listener
|
|
|
|
|
ln, err := tls.Listen(app.config.Network, addr, config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// prepare the server for the start
|
|
|
|
|
app.startupProcess()
|
|
|
|
|
|
|
|
|
|
// Print startup message
|
|
|
|
|
if !app.config.DisableStartupMessage {
|
|
|
|
|
app.startupMessage(ln.Addr().String(), true, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print routes
|
|
|
|
|
if app.config.EnablePrintRoutes {
|
|
|
|
|
app.printRoutesMessage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start listening
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 01:01:51 +09:00
|
|
|
|
// Handler returns the server handler.
|
2020-06-24 21:36:40 +02:00
|
|
|
|
func (app *App) Handler() fasthttp.RequestHandler {
|
2021-01-24 13:02:21 +01:00
|
|
|
|
// prepare the server for the start
|
|
|
|
|
app.startupProcess()
|
2020-06-24 21:36:40 +02:00
|
|
|
|
return app.handler
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// Stack returns the raw router stack.
|
2020-07-21 22:49:49 +02:00
|
|
|
|
func (app *App) Stack() [][]*Route {
|
|
|
|
|
return app.stack
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 09:23:14 +03:00
|
|
|
|
// HandlersCount returns the amount of registered handlers.
|
|
|
|
|
func (app *App) HandlersCount() uint32 {
|
2021-12-29 17:26:50 +08:00
|
|
|
|
return app.handlersCount
|
2021-12-29 09:23:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2022-03-10 10:35:15 +03:00
|
|
|
|
if app.hooks != nil {
|
|
|
|
|
defer app.hooks.executeOnShutdownHooks()
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 15:15:34 +02:00
|
|
|
|
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-09-27 23:23:28 +02:00
|
|
|
|
// Server returns the underlying fasthttp server
|
|
|
|
|
func (app *App) Server() *fasthttp.Server {
|
|
|
|
|
return app.server
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 10:35:15 +03:00
|
|
|
|
// Hooks returns the hook struct to register hooks.
|
|
|
|
|
func (app *App) Hooks() *hooks {
|
|
|
|
|
return app.hooks
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 01:01:51 +09:00
|
|
|
|
// Test is used for internal debugging by passing a *http.Request.
|
2020-04-27 23:34:35 +02:00
|
|
|
|
// 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
|
|
|
|
|
2020-05-16 08:12:43 +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-05-16 08:12:43 +02:00
|
|
|
|
}
|
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
|
|
|
|
|
2022-06-12 13:33:01 -03:00
|
|
|
|
// adding back the query from URL, since dump cleans it
|
|
|
|
|
dumps := bytes.Split(dump, []byte(" "))
|
|
|
|
|
dumps[1] = []byte(req.URL.String())
|
|
|
|
|
dump = bytes.Join(dumps, []byte(" "))
|
|
|
|
|
|
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
|
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
|
|
|
|
|
}
|
2021-01-24 13:02:21 +01:00
|
|
|
|
// prepare the server for the start
|
|
|
|
|
app.startupProcess()
|
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() {
|
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
|
2020-04-27 23:34:35 +02:00
|
|
|
|
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
|
|
|
|
}
|
2020-04-27 23:34:35 +02:00
|
|
|
|
} else {
|
|
|
|
|
// Without timeout
|
2020-05-12 23:24:04 +02:00
|
|
|
|
err = <-channel
|
2020-04-27 23:34:35 +02:00
|
|
|
|
}
|
2020-09-13 11:20:11 +02:00
|
|
|
|
|
2020-04-27 23:34:35 +02:00
|
|
|
|
// Check for errors
|
2020-09-13 11:20:11 +02:00
|
|
|
|
if err != nil && err != fasthttp.ErrGetOnly {
|
2020-04-27 23:34:35 +02:00
|
|
|
|
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
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 14:13:02 +02:00
|
|
|
|
type disableLogger struct{}
|
|
|
|
|
|
2022-05-31 17:41:38 +03:00
|
|
|
|
func (dl *disableLogger) Printf(_ string, _ ...any) {
|
2020-03-29 14:13:02 +02:00
|
|
|
|
// fmt.Println(fmt.Sprintf(format, args...))
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-23 09:25:18 +02:00
|
|
|
|
func (app *App) init() *App {
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// lock application
|
2020-05-12 19:24:04 +02:00
|
|
|
|
app.mutex.Lock()
|
2020-08-11 00:08:04 +02:00
|
|
|
|
|
2021-10-05 08:03:20 -04:00
|
|
|
|
// Only load templates if a view engine is specified
|
2020-09-13 11:20:11 +02:00
|
|
|
|
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,
|
2021-10-05 08:03:20 -04:00
|
|
|
|
ErrorHandler: app.serverErrorHandler,
|
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
|
2020-10-07 11:55:17 +02:00
|
|
|
|
app.server.ReduceMemoryUsage = app.config.ReduceMemoryUsage
|
2021-07-11 11:45:46 +02:00
|
|
|
|
app.server.StreamRequestBody = app.config.StreamRequestBody
|
|
|
|
|
app.server.DisablePreParseMultipartForm = app.config.DisablePreParseMultipartForm
|
2020-09-13 11:20:11 +02:00
|
|
|
|
|
|
|
|
|
// unlock application
|
|
|
|
|
app.mutex.Unlock()
|
2020-05-23 09:25:18 +02:00
|
|
|
|
return app
|
2020-02-21 18:07:43 +01:00
|
|
|
|
}
|
2020-06-07 21:10:38 +02:00
|
|
|
|
|
2021-10-05 08:03:20 -04:00
|
|
|
|
// ErrorHandler is the application's method in charge of finding the
|
2021-11-05 08:00:03 +01:00
|
|
|
|
// appropriate handler for the given request. It searches any mounted
|
2021-10-05 08:03:20 -04:00
|
|
|
|
// sub fibers by their prefixes and if it finds a match, it uses that
|
|
|
|
|
// error handler. Otherwise it uses the configured error handler for
|
|
|
|
|
// the app, which if not set is the DefaultErrorHandler.
|
|
|
|
|
func (app *App) ErrorHandler(ctx *Ctx, err error) error {
|
|
|
|
|
var (
|
|
|
|
|
mountedErrHandler ErrorHandler
|
|
|
|
|
mountedPrefixParts int
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-06 16:58:45 +03:00
|
|
|
|
for prefix, subApp := range app.appList {
|
2022-04-05 08:40:22 +02:00
|
|
|
|
if prefix != "" && strings.HasPrefix(ctx.path, prefix) {
|
2021-10-05 08:03:20 -04:00
|
|
|
|
parts := len(strings.Split(prefix, "/"))
|
|
|
|
|
if mountedPrefixParts <= parts {
|
2022-02-06 16:58:45 +03:00
|
|
|
|
mountedErrHandler = subApp.config.ErrorHandler
|
2021-10-05 08:03:20 -04:00
|
|
|
|
mountedPrefixParts = parts
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if mountedErrHandler != nil {
|
|
|
|
|
return mountedErrHandler(ctx, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return app.config.ErrorHandler(ctx, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// serverErrorHandler is a wrapper around the application's error handler method
|
|
|
|
|
// user for the fasthttp server configuration. It maps a set of fasthttp errors to fiber
|
|
|
|
|
// errors before calling the application's error handler method.
|
|
|
|
|
func (app *App) serverErrorHandler(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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if catch := app.ErrorHandler(c, err); catch != nil {
|
|
|
|
|
_ = c.SendStatus(StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.ReleaseCtx(c)
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 13:02:21 +01:00
|
|
|
|
// startupProcess Is the method which executes all the necessary processes just before the start of the server.
|
|
|
|
|
func (app *App) startupProcess() *App {
|
2022-03-10 10:35:15 +03:00
|
|
|
|
if err := app.hooks.executeOnListenHooks(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 13:02:21 +01:00
|
|
|
|
app.mutex.Lock()
|
|
|
|
|
app.buildTree()
|
|
|
|
|
app.mutex.Unlock()
|
|
|
|
|
return app
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// startupMessage prepares the startup message with the handler number, port, address and other information
|
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
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
cBlack = "\u001b[90m"
|
2020-09-14 08:05:34 +02:00
|
|
|
|
// cRed = "\u001b[91m"
|
|
|
|
|
cCyan = "\u001b[96m"
|
|
|
|
|
// cGreen = "\u001b[92m"
|
2020-09-13 11:20:11 +02:00
|
|
|
|
// cYellow = "\u001b[93m"
|
|
|
|
|
// cBlue = "\u001b[94m"
|
|
|
|
|
// cMagenta = "\u001b[95m"
|
|
|
|
|
// cWhite = "\u001b[97m"
|
|
|
|
|
cReset = "\u001b[0m"
|
|
|
|
|
)
|
|
|
|
|
|
2020-09-14 08:05:34 +02:00
|
|
|
|
value := func(s string, width int) string {
|
|
|
|
|
pad := width - len(s)
|
|
|
|
|
str := ""
|
|
|
|
|
for i := 0; i < pad; i++ {
|
|
|
|
|
str += "."
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
2020-09-14 08:05:34 +02:00
|
|
|
|
if s == "Disabled" {
|
|
|
|
|
str += " " + s
|
|
|
|
|
} else {
|
|
|
|
|
str += fmt.Sprintf(" %s%s%s", cCyan, s, cBlack)
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
2020-09-14 08:05:34 +02:00
|
|
|
|
return str
|
2020-09-01 14:31:43 +02:00
|
|
|
|
}
|
2020-09-14 08:05:34 +02:00
|
|
|
|
|
|
|
|
|
center := func(s string, width int) string {
|
|
|
|
|
pad := strconv.Itoa((width - len(s)) / 2)
|
|
|
|
|
str := fmt.Sprintf("%"+pad+"s", " ")
|
|
|
|
|
str += s
|
|
|
|
|
str += fmt.Sprintf("%"+pad+"s", " ")
|
|
|
|
|
if len(str) < width {
|
|
|
|
|
str += " "
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
2020-09-14 08:05:34 +02:00
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
centerValue := func(s string, width int) string {
|
|
|
|
|
pad := strconv.Itoa((width - len(s)) / 2)
|
|
|
|
|
str := fmt.Sprintf("%"+pad+"s", " ")
|
|
|
|
|
str += fmt.Sprintf("%s%s%s", cCyan, s, cBlack)
|
|
|
|
|
str += fmt.Sprintf("%"+pad+"s", " ")
|
|
|
|
|
if len(str)-10 < width {
|
|
|
|
|
str += " "
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
2020-09-14 08:05:34 +02:00
|
|
|
|
return str
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 09:40:49 +01:00
|
|
|
|
pad := func(s string, width int) (str string) {
|
|
|
|
|
toAdd := width - len(s)
|
|
|
|
|
str += s
|
|
|
|
|
for i := 0; i < toAdd; i++ {
|
|
|
|
|
str += " "
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 11:20:11 +02:00
|
|
|
|
host, port := parseAddr(addr)
|
2021-03-09 17:14:52 +13:00
|
|
|
|
if host == "" {
|
2021-09-03 17:53:03 +05:30
|
|
|
|
if app.config.Network == NetworkTCP6 {
|
|
|
|
|
host = "[::1]"
|
|
|
|
|
} else {
|
|
|
|
|
host = "0.0.0.0"
|
|
|
|
|
}
|
2020-06-25 22:48:49 +02:00
|
|
|
|
}
|
2021-03-09 17:14:52 +13:00
|
|
|
|
|
|
|
|
|
scheme := "http"
|
2020-06-27 04:22:22 +02:00
|
|
|
|
if tls {
|
2021-03-09 17:14:52 +13:00
|
|
|
|
scheme = "https"
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
2020-09-14 08:05:34 +02:00
|
|
|
|
|
2020-09-15 09:02:24 +08:00
|
|
|
|
isPrefork := "Disabled"
|
2020-09-13 11:20:11 +02:00
|
|
|
|
if app.config.Prefork {
|
2020-09-15 09:02:24 +08:00
|
|
|
|
isPrefork = "Enabled"
|
2020-09-13 11:20:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 22:51:43 +01:00
|
|
|
|
procs := strconv.Itoa(runtime.GOMAXPROCS(0))
|
|
|
|
|
if !app.config.Prefork {
|
|
|
|
|
procs = "1"
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 12:29:11 +05:45
|
|
|
|
mainLogo := cBlack + " ┌───────────────────────────────────────────────────┐\n"
|
|
|
|
|
if app.config.AppName != "" {
|
|
|
|
|
mainLogo += " │ " + centerValue(app.config.AppName, 49) + " │\n"
|
|
|
|
|
}
|
|
|
|
|
mainLogo += " │ " + centerValue(" Fiber v"+Version, 49) + " │\n"
|
2021-03-09 00:35:51 -08:00
|
|
|
|
|
2021-03-09 17:14:52 +13:00
|
|
|
|
if host == "0.0.0.0" {
|
2021-03-09 00:35:51 -08:00
|
|
|
|
mainLogo +=
|
2021-03-13 12:49:51 +01:00
|
|
|
|
" │ " + center(fmt.Sprintf("%s://127.0.0.1:%s", scheme, port), 49) + " │\n" +
|
|
|
|
|
" │ " + center(fmt.Sprintf("(bound on host 0.0.0.0 and port %s)", port), 49) + " │\n"
|
2021-03-09 17:14:52 +13:00
|
|
|
|
} else {
|
2021-03-09 00:35:51 -08:00
|
|
|
|
mainLogo +=
|
2021-03-13 12:49:51 +01:00
|
|
|
|
" │ " + center(fmt.Sprintf("%s://%s:%s", scheme, host, port), 49) + " │\n"
|
2021-03-09 00:35:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mainLogo += fmt.Sprintf(
|
|
|
|
|
" │ │\n"+
|
2021-03-13 12:49:51 +01:00
|
|
|
|
" │ Handlers %s Processes %s │\n"+
|
|
|
|
|
" │ Prefork .%s PID ....%s │\n"+
|
|
|
|
|
" └───────────────────────────────────────────────────┘"+
|
|
|
|
|
cReset,
|
2021-12-29 17:26:50 +08:00
|
|
|
|
value(strconv.Itoa(int(app.handlersCount)), 14), value(procs, 12),
|
2021-03-09 00:35:51 -08:00
|
|
|
|
value(isPrefork, 14), value(strconv.Itoa(os.Getpid()), 14),
|
|
|
|
|
)
|
2020-09-13 11:20:11 +02:00
|
|
|
|
|
2020-10-09 09:40:49 +01:00
|
|
|
|
var childPidsLogo string
|
|
|
|
|
if app.config.Prefork {
|
|
|
|
|
var childPidsTemplate string
|
|
|
|
|
childPidsTemplate += "%s"
|
|
|
|
|
childPidsTemplate += " ┌───────────────────────────────────────────────────┐\n%s"
|
|
|
|
|
childPidsTemplate += " └───────────────────────────────────────────────────┘"
|
|
|
|
|
childPidsTemplate += "%s"
|
|
|
|
|
|
|
|
|
|
newLine := " │ %s%s%s │"
|
|
|
|
|
|
|
|
|
|
// Turn the `pids` variable (in the form ",a,b,c,d,e,f,etc") into a slice of PIDs
|
|
|
|
|
var pidSlice []string
|
|
|
|
|
for _, v := range strings.Split(pids, ",") {
|
|
|
|
|
if v != "" {
|
|
|
|
|
pidSlice = append(pidSlice, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
|
thisLine := "Child PIDs ... "
|
|
|
|
|
var itemsOnThisLine []string
|
|
|
|
|
|
|
|
|
|
addLine := func() {
|
|
|
|
|
lines = append(lines,
|
|
|
|
|
fmt.Sprintf(
|
|
|
|
|
newLine,
|
|
|
|
|
cBlack,
|
|
|
|
|
thisLine+cCyan+pad(strings.Join(itemsOnThisLine, ", "), 49-len(thisLine)),
|
|
|
|
|
cBlack,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, pid := range pidSlice {
|
|
|
|
|
if len(thisLine+strings.Join(append(itemsOnThisLine, pid), ", ")) > 49 {
|
|
|
|
|
addLine()
|
|
|
|
|
thisLine = ""
|
2020-10-10 16:57:15 -04:00
|
|
|
|
itemsOnThisLine = []string{pid}
|
2020-10-09 09:40:49 +01:00
|
|
|
|
} else {
|
|
|
|
|
itemsOnThisLine = append(itemsOnThisLine, pid)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add left over items to their own line
|
|
|
|
|
if len(itemsOnThisLine) != 0 {
|
|
|
|
|
addLine()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Form logo
|
|
|
|
|
childPidsLogo = fmt.Sprintf(childPidsTemplate,
|
2020-10-10 16:57:15 -04:00
|
|
|
|
cBlack,
|
|
|
|
|
strings.Join(lines, "\n")+"\n",
|
2020-10-09 09:40:49 +01:00
|
|
|
|
cReset,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Combine both the child PID logo and the main Fiber logo
|
|
|
|
|
|
|
|
|
|
// Pad the shorter logo to the length of the longer one
|
|
|
|
|
splitMainLogo := strings.Split(mainLogo, "\n")
|
|
|
|
|
splitChildPidsLogo := strings.Split(childPidsLogo, "\n")
|
|
|
|
|
|
|
|
|
|
mainLen := len(splitMainLogo)
|
|
|
|
|
childLen := len(splitChildPidsLogo)
|
|
|
|
|
|
|
|
|
|
if mainLen > childLen {
|
|
|
|
|
diff := mainLen - childLen
|
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
|
splitChildPidsLogo = append(splitChildPidsLogo, "")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
diff := childLen - mainLen
|
|
|
|
|
for i := 0; i < diff; i++ {
|
|
|
|
|
splitMainLogo = append(splitMainLogo, "")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Combine the two logos, line by line
|
|
|
|
|
output := "\n"
|
|
|
|
|
for i := range splitMainLogo {
|
|
|
|
|
output += cBlack + splitMainLogo[i] + " " + splitChildPidsLogo[i] + "\n"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out := colorable.NewColorableStdout()
|
2021-12-07 14:50:29 +03:00
|
|
|
|
if os.Getenv("TERM") == "dumb" || os.Getenv("NO_COLOR") == "1" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) {
|
2020-10-09 09:40:49 +01:00
|
|
|
|
out = colorable.NewNonColorable(os.Stdout)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 13:49:13 +08:00
|
|
|
|
_, _ = fmt.Fprintln(out, output)
|
2020-06-07 21:10:38 +02:00
|
|
|
|
}
|
2021-12-30 21:13:31 +08:00
|
|
|
|
|
|
|
|
|
// printRoutesMessage print all routes with method, path, name and handlers
|
|
|
|
|
// in a format of table, like this:
|
|
|
|
|
// method | path | name | handlers
|
2022-05-31 17:35:49 +03:00
|
|
|
|
// GET | / | routeName | github.com/gofiber/fiber/v3.emptyHandler
|
|
|
|
|
// HEAD | / | | github.com/gofiber/fiber/v3.emptyHandler
|
2021-12-30 21:13:31 +08:00
|
|
|
|
func (app *App) printRoutesMessage() {
|
|
|
|
|
// ignore child processes
|
|
|
|
|
if IsChild() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
var routes []RouteMessage
|
|
|
|
|
for _, routeStack := range app.stack {
|
|
|
|
|
for _, route := range routeStack {
|
|
|
|
|
var newRoute = RouteMessage{}
|
|
|
|
|
newRoute.name = route.Name
|
|
|
|
|
newRoute.method = route.Method
|
|
|
|
|
newRoute.path = route.Path
|
|
|
|
|
for _, handler := range route.Handlers {
|
|
|
|
|
newRoute.handlers += runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name() + " "
|
|
|
|
|
}
|
|
|
|
|
routes = append(routes, newRoute)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-31 01:03:44 +03:00
|
|
|
|
|
|
|
|
|
out := colorable.NewColorableStdout()
|
|
|
|
|
if os.Getenv("TERM") == "dumb" || os.Getenv("NO_COLOR") == "1" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) {
|
|
|
|
|
out = colorable.NewNonColorable(os.Stdout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(out, 1, 1, 1, ' ', 0)
|
2021-12-30 21:13:31 +08:00
|
|
|
|
// Sort routes by path
|
|
|
|
|
sort.Slice(routes, func(i, j int) bool {
|
|
|
|
|
return routes[i].path < routes[j].path
|
|
|
|
|
})
|
|
|
|
|
_, _ = fmt.Fprintf(w, "%smethod\t%s| %spath\t%s| %sname\t%s| %shandlers\n", cBlue, cWhite, cGreen, cWhite, cCyan, cWhite, cYellow)
|
|
|
|
|
_, _ = fmt.Fprintf(w, "%s------\t%s| %s----\t%s| %s----\t%s| %s--------\n", cBlue, cWhite, cGreen, cWhite, cCyan, cWhite, cYellow)
|
|
|
|
|
for _, route := range routes {
|
|
|
|
|
_, _ = fmt.Fprintf(w, "%s%s\t%s| %s%s\t%s| %s%s\t%s| %s%s\n", cBlue, route.method, cWhite, cGreen, route.path, cWhite, cCyan, route.name, cWhite, cYellow, route.handlers)
|
|
|
|
|
}
|
2021-12-31 01:03:44 +03:00
|
|
|
|
|
2021-12-30 21:13:31 +08:00
|
|
|
|
_ = w.Flush()
|
|
|
|
|
}
|