app.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 Github Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. // Package fiber is an Express inspired web framework built on top of Fasthttp,
  5. // the fastest HTTP engine for Go. Designed to ease things up for fast
  6. // development with zero memory allocation and performance in mind.
  7. package fiber
  8. import (
  9. "bufio"
  10. "context"
  11. "encoding/json"
  12. "encoding/xml"
  13. "errors"
  14. "fmt"
  15. "net"
  16. "net/http"
  17. "net/http/httputil"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "sync"
  22. "time"
  23. "github.com/gofiber/fiber/v2/log"
  24. "github.com/gofiber/fiber/v2/utils"
  25. "github.com/valyala/fasthttp"
  26. )
  27. // Version of current fiber package
  28. const Version = "2.52.0"
  29. // Handler defines a function to serve HTTP requests.
  30. type Handler = func(*Ctx) error
  31. // Map is a shortcut for map[string]interface{}, useful for JSON returns
  32. type Map map[string]interface{}
  33. // Storage interface for communicating with different database/key-value
  34. // providers
  35. type Storage interface {
  36. // Get gets the value for the given key.
  37. // `nil, nil` is returned when the key does not exist
  38. Get(key string) ([]byte, error)
  39. // Set stores the given value for the given key along
  40. // with an expiration value, 0 means no expiration.
  41. // Empty key or value will be ignored without an error.
  42. Set(key string, val []byte, exp time.Duration) error
  43. // Delete deletes the value for the given key.
  44. // It returns no error if the storage does not contain the key,
  45. Delete(key string) error
  46. // Reset resets the storage and delete all keys.
  47. Reset() error
  48. // Close closes the storage and will stop any running garbage
  49. // collectors and open connections.
  50. Close() error
  51. }
  52. // ErrorHandler defines a function that will process all errors
  53. // returned from any handlers in the stack
  54. //
  55. // cfg := fiber.Config{}
  56. // cfg.ErrorHandler = func(c *Ctx, err error) error {
  57. // code := StatusInternalServerError
  58. // var e *fiber.Error
  59. // if errors.As(err, &e) {
  60. // code = e.Code
  61. // }
  62. // c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
  63. // return c.Status(code).SendString(err.Error())
  64. // }
  65. // app := fiber.New(cfg)
  66. type ErrorHandler = func(*Ctx, error) error
  67. // Error represents an error that occurred while handling a request.
  68. type Error struct {
  69. Code int `json:"code"`
  70. Message string `json:"message"`
  71. }
  72. // App denotes the Fiber application.
  73. type App struct {
  74. mutex sync.Mutex
  75. // Route stack divided by HTTP methods
  76. stack [][]*Route
  77. // Route stack divided by HTTP methods and route prefixes
  78. treeStack []map[string][]*Route
  79. // contains the information if the route stack has been changed to build the optimized tree
  80. routesRefreshed bool
  81. // Amount of registered routes
  82. routesCount uint32
  83. // Amount of registered handlers
  84. handlersCount uint32
  85. // Ctx pool
  86. pool sync.Pool
  87. // Fasthttp server
  88. server *fasthttp.Server
  89. // App config
  90. config Config
  91. // Converts string to a byte slice
  92. getBytes func(s string) (b []byte)
  93. // Converts byte slice to a string
  94. getString func(b []byte) string
  95. // Hooks
  96. hooks *Hooks
  97. // Latest route & group
  98. latestRoute *Route
  99. // TLS handler
  100. tlsHandler *TLSHandler
  101. // Mount fields
  102. mountFields *mountFields
  103. // Indicates if the value was explicitly configured
  104. configured Config
  105. }
  106. // Config is a struct holding the server settings.
  107. type Config struct {
  108. // When set to true, this will spawn multiple Go processes listening on the same port.
  109. //
  110. // Default: false
  111. Prefork bool `json:"prefork"`
  112. // Enables the "Server: value" HTTP header.
  113. //
  114. // Default: ""
  115. ServerHeader string `json:"server_header"`
  116. // When set to true, the router treats "/foo" and "/foo/" as different.
  117. // By default this is disabled and both "/foo" and "/foo/" will execute the same handler.
  118. //
  119. // Default: false
  120. StrictRouting bool `json:"strict_routing"`
  121. // When set to true, enables case sensitive routing.
  122. // E.g. "/FoO" and "/foo" are treated as different routes.
  123. // By default this is disabled and both "/FoO" and "/foo" will execute the same handler.
  124. //
  125. // Default: false
  126. CaseSensitive bool `json:"case_sensitive"`
  127. // When set to true, this relinquishes the 0-allocation promise in certain
  128. // cases in order to access the handler values (e.g. request bodies) in an
  129. // immutable fashion so that these values are available even if you return
  130. // from handler.
  131. //
  132. // Default: false
  133. Immutable bool `json:"immutable"`
  134. // When set to true, converts all encoded characters in the route back
  135. // before setting the path for the context, so that the routing,
  136. // the returning of the current url from the context `ctx.Path()`
  137. // and the parameters `ctx.Params(%key%)` with decoded characters will work
  138. //
  139. // Default: false
  140. UnescapePath bool `json:"unescape_path"`
  141. // Enable or disable ETag header generation, since both weak and strong etags are generated
  142. // using the same hashing method (CRC-32). Weak ETags are the default when enabled.
  143. //
  144. // Default: false
  145. ETag bool `json:"etag"`
  146. // Max body size that the server accepts.
  147. // -1 will decline any body size
  148. //
  149. // Default: 4 * 1024 * 1024
  150. BodyLimit int `json:"body_limit"`
  151. // Maximum number of concurrent connections.
  152. //
  153. // Default: 256 * 1024
  154. Concurrency int `json:"concurrency"`
  155. // Views is the interface that wraps the Render function.
  156. //
  157. // Default: nil
  158. Views Views `json:"-"`
  159. // Views Layout is the global layout for all template render until override on Render function.
  160. //
  161. // Default: ""
  162. ViewsLayout string `json:"views_layout"`
  163. // PassLocalsToViews Enables passing of the locals set on a fiber.Ctx to the template engine
  164. //
  165. // Default: false
  166. PassLocalsToViews bool `json:"pass_locals_to_views"`
  167. // The amount of time allowed to read the full request including body.
  168. // It is reset after the request handler has returned.
  169. // The connection's read deadline is reset when the connection opens.
  170. //
  171. // Default: unlimited
  172. ReadTimeout time.Duration `json:"read_timeout"`
  173. // The maximum duration before timing out writes of the response.
  174. // It is reset after the request handler has returned.
  175. //
  176. // Default: unlimited
  177. WriteTimeout time.Duration `json:"write_timeout"`
  178. // The maximum amount of time to wait for the next request when keep-alive is enabled.
  179. // If IdleTimeout is zero, the value of ReadTimeout is used.
  180. //
  181. // Default: unlimited
  182. IdleTimeout time.Duration `json:"idle_timeout"`
  183. // Per-connection buffer size for requests' reading.
  184. // This also limits the maximum header size.
  185. // Increase this buffer if your clients send multi-KB RequestURIs
  186. // and/or multi-KB headers (for example, BIG cookies).
  187. //
  188. // Default: 4096
  189. ReadBufferSize int `json:"read_buffer_size"`
  190. // Per-connection buffer size for responses' writing.
  191. //
  192. // Default: 4096
  193. WriteBufferSize int `json:"write_buffer_size"`
  194. // CompressedFileSuffix adds suffix to the original file name and
  195. // tries saving the resulting compressed file under the new file name.
  196. //
  197. // Default: ".fiber.gz"
  198. CompressedFileSuffix string `json:"compressed_file_suffix"`
  199. // ProxyHeader will enable c.IP() to return the value of the given header key
  200. // By default c.IP() will return the Remote IP from the TCP connection
  201. // This property can be useful if you are behind a load balancer: X-Forwarded-*
  202. // NOTE: headers are easily spoofed and the detected IP addresses are unreliable.
  203. //
  204. // Default: ""
  205. ProxyHeader string `json:"proxy_header"`
  206. // GETOnly rejects all non-GET requests if set to true.
  207. // This option is useful as anti-DoS protection for servers
  208. // accepting only GET requests. The request size is limited
  209. // by ReadBufferSize if GETOnly is set.
  210. //
  211. // Default: false
  212. GETOnly bool `json:"get_only"`
  213. // ErrorHandler is executed when an error is returned from fiber.Handler.
  214. //
  215. // Default: DefaultErrorHandler
  216. ErrorHandler ErrorHandler `json:"-"`
  217. // When set to true, disables keep-alive connections.
  218. // The server will close incoming connections after sending the first response to client.
  219. //
  220. // Default: false
  221. DisableKeepalive bool `json:"disable_keepalive"`
  222. // When set to true, causes the default date header to be excluded from the response.
  223. //
  224. // Default: false
  225. DisableDefaultDate bool `json:"disable_default_date"`
  226. // When set to true, causes the default Content-Type header to be excluded from the response.
  227. //
  228. // Default: false
  229. DisableDefaultContentType bool `json:"disable_default_content_type"`
  230. // When set to true, disables header normalization.
  231. // By default all header names are normalized: conteNT-tYPE -> Content-Type.
  232. //
  233. // Default: false
  234. DisableHeaderNormalizing bool `json:"disable_header_normalizing"`
  235. // When set to true, it will not print out the «Fiber» ASCII art and listening address.
  236. //
  237. // Default: false
  238. DisableStartupMessage bool `json:"disable_startup_message"`
  239. // This function allows to setup app name for the app
  240. //
  241. // Default: nil
  242. AppName string `json:"app_name"`
  243. // StreamRequestBody enables request body streaming,
  244. // and calls the handler sooner when given body is
  245. // larger then the current limit.
  246. StreamRequestBody bool
  247. // Will not pre parse Multipart Form data if set to true.
  248. //
  249. // This option is useful for servers that desire to treat
  250. // multipart form data as a binary blob, or choose when to parse the data.
  251. //
  252. // Server pre parses multipart form data by default.
  253. DisablePreParseMultipartForm bool
  254. // Aggressively reduces memory usage at the cost of higher CPU usage
  255. // if set to true.
  256. //
  257. // Try enabling this option only if the server consumes too much memory
  258. // serving mostly idle keep-alive connections. This may reduce memory
  259. // usage by more than 50%.
  260. //
  261. // Default: false
  262. ReduceMemoryUsage bool `json:"reduce_memory_usage"`
  263. // FEATURE: v2.3.x
  264. // The router executes the same handler by default if StrictRouting or CaseSensitive is disabled.
  265. // Enabling RedirectFixedPath will change this behavior into a client redirect to the original route path.
  266. // Using the status code 301 for GET requests and 308 for all other request methods.
  267. //
  268. // Default: false
  269. // RedirectFixedPath bool
  270. // When set by an external client of Fiber it will use the provided implementation of a
  271. // JSONMarshal
  272. //
  273. // Allowing for flexibility in using another json library for encoding
  274. // Default: json.Marshal
  275. JSONEncoder utils.JSONMarshal `json:"-"`
  276. // When set by an external client of Fiber it will use the provided implementation of a
  277. // JSONUnmarshal
  278. //
  279. // Allowing for flexibility in using another json library for decoding
  280. // Default: json.Unmarshal
  281. JSONDecoder utils.JSONUnmarshal `json:"-"`
  282. // XMLEncoder set by an external client of Fiber it will use the provided implementation of a
  283. // XMLMarshal
  284. //
  285. // Allowing for flexibility in using another XML library for encoding
  286. // Default: xml.Marshal
  287. XMLEncoder utils.XMLMarshal `json:"-"`
  288. // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)
  289. // WARNING: When prefork is set to true, only "tcp4" and "tcp6" can be chose.
  290. //
  291. // Default: NetworkTCP4
  292. Network string
  293. // If you find yourself behind some sort of proxy, like a load balancer,
  294. // then certain header information may be sent to you using special X-Forwarded-* headers or the Forwarded header.
  295. // For example, the Host HTTP header is usually used to return the requested host.
  296. // But when you’re behind a proxy, the actual host may be stored in an X-Forwarded-Host header.
  297. //
  298. // If you are behind a proxy, you should enable TrustedProxyCheck to prevent header spoofing.
  299. // If you enable EnableTrustedProxyCheck and leave TrustedProxies empty Fiber will skip
  300. // all headers that could be spoofed.
  301. // If request ip in TrustedProxies whitelist then:
  302. // 1. c.Protocol() get value from X-Forwarded-Proto, X-Forwarded-Protocol, X-Forwarded-Ssl or X-Url-Scheme header
  303. // 2. c.IP() get value from ProxyHeader header.
  304. // 3. c.Hostname() get value from X-Forwarded-Host header
  305. // But if request ip NOT in Trusted Proxies whitelist then:
  306. // 1. c.Protocol() WON't get value from X-Forwarded-Proto, X-Forwarded-Protocol, X-Forwarded-Ssl or X-Url-Scheme header,
  307. // will return https in case when tls connection is handled by the app, of http otherwise
  308. // 2. c.IP() WON'T get value from ProxyHeader header, will return RemoteIP() from fasthttp context
  309. // 3. c.Hostname() WON'T get value from X-Forwarded-Host header, fasthttp.Request.URI().Host()
  310. // will be used to get the hostname.
  311. //
  312. // Default: false
  313. EnableTrustedProxyCheck bool `json:"enable_trusted_proxy_check"`
  314. // Read EnableTrustedProxyCheck doc.
  315. //
  316. // Default: []string
  317. TrustedProxies []string `json:"trusted_proxies"`
  318. trustedProxiesMap map[string]struct{}
  319. trustedProxyRanges []*net.IPNet
  320. // If set to true, c.IP() and c.IPs() will validate IP addresses before returning them.
  321. // Also, c.IP() will return only the first valid IP rather than just the raw header
  322. // WARNING: this has a performance cost associated with it.
  323. //
  324. // Default: false
  325. EnableIPValidation bool `json:"enable_ip_validation"`
  326. // If set to true, will print all routes with their method, path and handler.
  327. // Default: false
  328. EnablePrintRoutes bool `json:"enable_print_routes"`
  329. // You can define custom color scheme. They'll be used for startup message, route list and some middlewares.
  330. //
  331. // Optional. Default: DefaultColors
  332. ColorScheme Colors `json:"color_scheme"`
  333. // RequestMethods provides customizibility for HTTP methods. You can add/remove methods as you wish.
  334. //
  335. // Optional. Default: DefaultMethods
  336. RequestMethods []string
  337. // EnableSplittingOnParsers splits the query/body/header parameters by comma when it's true.
  338. // For example, you can use it to parse multiple values from a query parameter like this:
  339. // /api?foo=bar,baz == foo[]=bar&foo[]=baz
  340. //
  341. // Optional. Default: false
  342. EnableSplittingOnParsers bool `json:"enable_splitting_on_parsers"`
  343. }
  344. // Static defines configuration options when defining static assets.
  345. type Static struct {
  346. // When set to true, the server tries minimizing CPU usage by caching compressed files.
  347. // This works differently than the github.com/gofiber/compression middleware.
  348. // Optional. Default value false
  349. Compress bool `json:"compress"`
  350. // When set to true, enables byte range requests.
  351. // Optional. Default value false
  352. ByteRange bool `json:"byte_range"`
  353. // When set to true, enables directory browsing.
  354. // Optional. Default value false.
  355. Browse bool `json:"browse"`
  356. // When set to true, enables direct download.
  357. // Optional. Default value false.
  358. Download bool `json:"download"`
  359. // The name of the index file for serving a directory.
  360. // Optional. Default value "index.html".
  361. Index string `json:"index"`
  362. // Expiration duration for inactive file handlers.
  363. // Use a negative time.Duration to disable it.
  364. //
  365. // Optional. Default value 10 * time.Second.
  366. CacheDuration time.Duration `json:"cache_duration"`
  367. // The value for the Cache-Control HTTP-header
  368. // that is set on the file response. MaxAge is defined in seconds.
  369. //
  370. // Optional. Default value 0.
  371. MaxAge int `json:"max_age"`
  372. // ModifyResponse defines a function that allows you to alter the response.
  373. //
  374. // Optional. Default: nil
  375. ModifyResponse Handler
  376. // Next defines a function to skip this middleware when returned true.
  377. //
  378. // Optional. Default: nil
  379. Next func(c *Ctx) bool
  380. }
  381. // RouteMessage is some message need to be print when server starts
  382. type RouteMessage struct {
  383. name string
  384. method string
  385. path string
  386. handlers string
  387. }
  388. // Default Config values
  389. const (
  390. DefaultBodyLimit = 4 * 1024 * 1024
  391. DefaultConcurrency = 256 * 1024
  392. DefaultReadBufferSize = 4096
  393. DefaultWriteBufferSize = 4096
  394. DefaultCompressedFileSuffix = ".fiber.gz"
  395. )
  396. // HTTP methods enabled by default
  397. var DefaultMethods = []string{
  398. MethodGet,
  399. MethodHead,
  400. MethodPost,
  401. MethodPut,
  402. MethodDelete,
  403. MethodConnect,
  404. MethodOptions,
  405. MethodTrace,
  406. MethodPatch,
  407. }
  408. // DefaultErrorHandler that process return errors from handlers
  409. func DefaultErrorHandler(c *Ctx, err error) error {
  410. code := StatusInternalServerError
  411. var e *Error
  412. if errors.As(err, &e) {
  413. code = e.Code
  414. }
  415. c.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
  416. return c.Status(code).SendString(err.Error())
  417. }
  418. // New creates a new Fiber named instance.
  419. //
  420. // app := fiber.New()
  421. //
  422. // You can pass optional configuration options by passing a Config struct:
  423. //
  424. // app := fiber.New(fiber.Config{
  425. // Prefork: true,
  426. // ServerHeader: "Fiber",
  427. // })
  428. func New(config ...Config) *App {
  429. // Create a new app
  430. app := &App{
  431. // Create Ctx pool
  432. pool: sync.Pool{
  433. New: func() interface{} {
  434. return new(Ctx)
  435. },
  436. },
  437. // Create config
  438. config: Config{},
  439. getBytes: utils.UnsafeBytes,
  440. getString: utils.UnsafeString,
  441. latestRoute: &Route{},
  442. }
  443. // Define hooks
  444. app.hooks = newHooks(app)
  445. // Define mountFields
  446. app.mountFields = newMountFields(app)
  447. // Override config if provided
  448. if len(config) > 0 {
  449. app.config = config[0]
  450. }
  451. // Initialize configured before defaults are set
  452. app.configured = app.config
  453. if app.config.ETag {
  454. if !IsChild() {
  455. log.Warn("Config.ETag is deprecated since v2.0.6, please use 'middleware/etag'.")
  456. }
  457. }
  458. // Override default values
  459. if app.config.BodyLimit == 0 {
  460. app.config.BodyLimit = DefaultBodyLimit
  461. }
  462. if app.config.Concurrency <= 0 {
  463. app.config.Concurrency = DefaultConcurrency
  464. }
  465. if app.config.ReadBufferSize <= 0 {
  466. app.config.ReadBufferSize = DefaultReadBufferSize
  467. }
  468. if app.config.WriteBufferSize <= 0 {
  469. app.config.WriteBufferSize = DefaultWriteBufferSize
  470. }
  471. if app.config.CompressedFileSuffix == "" {
  472. app.config.CompressedFileSuffix = DefaultCompressedFileSuffix
  473. }
  474. if app.config.Immutable {
  475. app.getBytes, app.getString = getBytesImmutable, getStringImmutable
  476. }
  477. if app.config.ErrorHandler == nil {
  478. app.config.ErrorHandler = DefaultErrorHandler
  479. }
  480. if app.config.JSONEncoder == nil {
  481. app.config.JSONEncoder = json.Marshal
  482. }
  483. if app.config.JSONDecoder == nil {
  484. app.config.JSONDecoder = json.Unmarshal
  485. }
  486. if app.config.XMLEncoder == nil {
  487. app.config.XMLEncoder = xml.Marshal
  488. }
  489. if app.config.Network == "" {
  490. app.config.Network = NetworkTCP4
  491. }
  492. if len(app.config.RequestMethods) == 0 {
  493. app.config.RequestMethods = DefaultMethods
  494. }
  495. app.config.trustedProxiesMap = make(map[string]struct{}, len(app.config.TrustedProxies))
  496. for _, ipAddress := range app.config.TrustedProxies {
  497. app.handleTrustedProxy(ipAddress)
  498. }
  499. // Create router stack
  500. app.stack = make([][]*Route, len(app.config.RequestMethods))
  501. app.treeStack = make([]map[string][]*Route, len(app.config.RequestMethods))
  502. // Override colors
  503. app.config.ColorScheme = defaultColors(app.config.ColorScheme)
  504. // Init app
  505. app.init()
  506. // Return app
  507. return app
  508. }
  509. // Adds an ip address to trustedProxyRanges or trustedProxiesMap based on whether it is an IP range or not
  510. func (app *App) handleTrustedProxy(ipAddress string) {
  511. if strings.Contains(ipAddress, "/") {
  512. _, ipNet, err := net.ParseCIDR(ipAddress)
  513. if err != nil {
  514. log.Warnf("IP range %q could not be parsed: %v", ipAddress, err)
  515. } else {
  516. app.config.trustedProxyRanges = append(app.config.trustedProxyRanges, ipNet)
  517. }
  518. } else {
  519. app.config.trustedProxiesMap[ipAddress] = struct{}{}
  520. }
  521. }
  522. // SetTLSHandler You can use SetTLSHandler to use ClientHelloInfo when using TLS with Listener.
  523. func (app *App) SetTLSHandler(tlsHandler *TLSHandler) {
  524. // Attach the tlsHandler to the config
  525. app.mutex.Lock()
  526. app.tlsHandler = tlsHandler
  527. app.mutex.Unlock()
  528. }
  529. // Name Assign name to specific route.
  530. func (app *App) Name(name string) Router {
  531. app.mutex.Lock()
  532. defer app.mutex.Unlock()
  533. for _, routes := range app.stack {
  534. for _, route := range routes {
  535. isMethodValid := route.Method == app.latestRoute.Method || app.latestRoute.use ||
  536. (app.latestRoute.Method == MethodGet && route.Method == MethodHead)
  537. if route.Path == app.latestRoute.Path && isMethodValid {
  538. route.Name = name
  539. if route.group != nil {
  540. route.Name = route.group.name + route.Name
  541. }
  542. }
  543. }
  544. }
  545. if err := app.hooks.executeOnNameHooks(*app.latestRoute); err != nil {
  546. panic(err)
  547. }
  548. return app
  549. }
  550. // GetRoute Get route by name
  551. func (app *App) GetRoute(name string) Route {
  552. for _, routes := range app.stack {
  553. for _, route := range routes {
  554. if route.Name == name {
  555. return *route
  556. }
  557. }
  558. }
  559. return Route{}
  560. }
  561. // GetRoutes Get all routes. When filterUseOption equal to true, it will filter the routes registered by the middleware.
  562. func (app *App) GetRoutes(filterUseOption ...bool) []Route {
  563. var rs []Route
  564. var filterUse bool
  565. if len(filterUseOption) != 0 {
  566. filterUse = filterUseOption[0]
  567. }
  568. for _, routes := range app.stack {
  569. for _, route := range routes {
  570. if filterUse && route.use {
  571. continue
  572. }
  573. rs = append(rs, *route)
  574. }
  575. }
  576. return rs
  577. }
  578. // Use registers a middleware route that will match requests
  579. // with the provided prefix (which is optional and defaults to "/").
  580. //
  581. // app.Use(func(c *fiber.Ctx) error {
  582. // return c.Next()
  583. // })
  584. // app.Use("/api", func(c *fiber.Ctx) error {
  585. // return c.Next()
  586. // })
  587. // app.Use("/api", handler, func(c *fiber.Ctx) error {
  588. // return c.Next()
  589. // })
  590. //
  591. // This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...
  592. func (app *App) Use(args ...interface{}) Router {
  593. var prefix string
  594. var prefixes []string
  595. var handlers []Handler
  596. for i := 0; i < len(args); i++ {
  597. switch arg := args[i].(type) {
  598. case string:
  599. prefix = arg
  600. case []string:
  601. prefixes = arg
  602. case Handler:
  603. handlers = append(handlers, arg)
  604. default:
  605. panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg)))
  606. }
  607. }
  608. if len(prefixes) == 0 {
  609. prefixes = append(prefixes, prefix)
  610. }
  611. for _, prefix := range prefixes {
  612. app.register(methodUse, prefix, nil, handlers...)
  613. }
  614. return app
  615. }
  616. // Get registers a route for GET methods that requests a representation
  617. // of the specified resource. Requests using GET should only retrieve data.
  618. func (app *App) Get(path string, handlers ...Handler) Router {
  619. return app.Head(path, handlers...).Add(MethodGet, path, handlers...)
  620. }
  621. // Head registers a route for HEAD methods that asks for a response identical
  622. // to that of a GET request, but without the response body.
  623. func (app *App) Head(path string, handlers ...Handler) Router {
  624. return app.Add(MethodHead, path, handlers...)
  625. }
  626. // Post registers a route for POST methods that is used to submit an entity to the
  627. // specified resource, often causing a change in state or side effects on the server.
  628. func (app *App) Post(path string, handlers ...Handler) Router {
  629. return app.Add(MethodPost, path, handlers...)
  630. }
  631. // Put registers a route for PUT methods that replaces all current representations
  632. // of the target resource with the request payload.
  633. func (app *App) Put(path string, handlers ...Handler) Router {
  634. return app.Add(MethodPut, path, handlers...)
  635. }
  636. // Delete registers a route for DELETE methods that deletes the specified resource.
  637. func (app *App) Delete(path string, handlers ...Handler) Router {
  638. return app.Add(MethodDelete, path, handlers...)
  639. }
  640. // Connect registers a route for CONNECT methods that establishes a tunnel to the
  641. // server identified by the target resource.
  642. func (app *App) Connect(path string, handlers ...Handler) Router {
  643. return app.Add(MethodConnect, path, handlers...)
  644. }
  645. // Options registers a route for OPTIONS methods that is used to describe the
  646. // communication options for the target resource.
  647. func (app *App) Options(path string, handlers ...Handler) Router {
  648. return app.Add(MethodOptions, path, handlers...)
  649. }
  650. // Trace registers a route for TRACE methods that performs a message loop-back
  651. // test along the path to the target resource.
  652. func (app *App) Trace(path string, handlers ...Handler) Router {
  653. return app.Add(MethodTrace, path, handlers...)
  654. }
  655. // Patch registers a route for PATCH methods that is used to apply partial
  656. // modifications to a resource.
  657. func (app *App) Patch(path string, handlers ...Handler) Router {
  658. return app.Add(MethodPatch, path, handlers...)
  659. }
  660. // Add allows you to specify a HTTP method to register a route
  661. func (app *App) Add(method, path string, handlers ...Handler) Router {
  662. app.register(method, path, nil, handlers...)
  663. return app
  664. }
  665. // Static will create a file server serving static files
  666. func (app *App) Static(prefix, root string, config ...Static) Router {
  667. app.registerStatic(prefix, root, config...)
  668. return app
  669. }
  670. // All will register the handler on all HTTP methods
  671. func (app *App) All(path string, handlers ...Handler) Router {
  672. for _, method := range app.config.RequestMethods {
  673. _ = app.Add(method, path, handlers...)
  674. }
  675. return app
  676. }
  677. // Group is used for Routes with common prefix to define a new sub-router with optional middleware.
  678. //
  679. // api := app.Group("/api")
  680. // api.Get("/users", handler)
  681. func (app *App) Group(prefix string, handlers ...Handler) Router {
  682. grp := &Group{Prefix: prefix, app: app}
  683. if len(handlers) > 0 {
  684. app.register(methodUse, prefix, grp, handlers...)
  685. }
  686. if err := app.hooks.executeOnGroupHooks(*grp); err != nil {
  687. panic(err)
  688. }
  689. return grp
  690. }
  691. // Route is used to define routes with a common prefix inside the common function.
  692. // Uses Group method to define new sub-router.
  693. func (app *App) Route(prefix string, fn func(router Router), name ...string) Router {
  694. // Create new group
  695. group := app.Group(prefix)
  696. if len(name) > 0 {
  697. group.Name(name[0])
  698. }
  699. // Define routes
  700. fn(group)
  701. return group
  702. }
  703. // Error makes it compatible with the `error` interface.
  704. func (e *Error) Error() string {
  705. return e.Message
  706. }
  707. // NewError creates a new Error instance with an optional message
  708. func NewError(code int, message ...string) *Error {
  709. err := &Error{
  710. Code: code,
  711. Message: utils.StatusMessage(code),
  712. }
  713. if len(message) > 0 {
  714. err.Message = message[0]
  715. }
  716. return err
  717. }
  718. // Config returns the app config as value ( read-only ).
  719. func (app *App) Config() Config {
  720. return app.config
  721. }
  722. // Handler returns the server handler.
  723. func (app *App) Handler() fasthttp.RequestHandler { //revive:disable-line:confusing-naming // Having both a Handler() (uppercase) and a handler() (lowercase) is fine. TODO: Use nolint:revive directive instead. See https://github.com/golangci/golangci-lint/issues/3476
  724. // prepare the server for the start
  725. app.startupProcess()
  726. return app.handler
  727. }
  728. // Stack returns the raw router stack.
  729. func (app *App) Stack() [][]*Route {
  730. return app.stack
  731. }
  732. // HandlersCount returns the amount of registered handlers.
  733. func (app *App) HandlersCount() uint32 {
  734. return app.handlersCount
  735. }
  736. // Shutdown gracefully shuts down the server without interrupting any active connections.
  737. // Shutdown works by first closing all open listeners and then waiting indefinitely for all connections to return to idle before shutting down.
  738. //
  739. // Make sure the program doesn't exit and waits instead for Shutdown to return.
  740. //
  741. // Shutdown does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
  742. func (app *App) Shutdown() error {
  743. return app.ShutdownWithContext(context.Background())
  744. }
  745. // ShutdownWithTimeout gracefully shuts down the server without interrupting any active connections. However, if the timeout is exceeded,
  746. // ShutdownWithTimeout will forcefully close any active connections.
  747. // ShutdownWithTimeout works by first closing all open listeners and then waiting for all connections to return to idle before shutting down.
  748. //
  749. // Make sure the program doesn't exit and waits instead for ShutdownWithTimeout to return.
  750. //
  751. // ShutdownWithTimeout does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
  752. func (app *App) ShutdownWithTimeout(timeout time.Duration) error {
  753. ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
  754. defer cancelFunc()
  755. return app.ShutdownWithContext(ctx)
  756. }
  757. // ShutdownWithContext shuts down the server including by force if the context's deadline is exceeded.
  758. //
  759. // Make sure the program doesn't exit and waits instead for ShutdownWithTimeout to return.
  760. //
  761. // ShutdownWithContext does not close keepalive connections so its recommended to set ReadTimeout to something else than 0.
  762. func (app *App) ShutdownWithContext(ctx context.Context) error {
  763. if app.hooks != nil {
  764. defer app.hooks.executeOnShutdownHooks()
  765. }
  766. app.mutex.Lock()
  767. defer app.mutex.Unlock()
  768. if app.server == nil {
  769. return fmt.Errorf("shutdown: server is not running")
  770. }
  771. return app.server.ShutdownWithContext(ctx)
  772. }
  773. // Server returns the underlying fasthttp server
  774. func (app *App) Server() *fasthttp.Server {
  775. return app.server
  776. }
  777. // Hooks returns the hook struct to register hooks.
  778. func (app *App) Hooks() *Hooks {
  779. return app.hooks
  780. }
  781. // Test is used for internal debugging by passing a *http.Request.
  782. // Timeout is optional and defaults to 1s, -1 will disable it completely.
  783. func (app *App) Test(req *http.Request, msTimeout ...int) (*http.Response, error) {
  784. // Set timeout
  785. timeout := 1000
  786. if len(msTimeout) > 0 {
  787. timeout = msTimeout[0]
  788. }
  789. // Add Content-Length if not provided with body
  790. if req.Body != http.NoBody && req.Header.Get(HeaderContentLength) == "" {
  791. req.Header.Add(HeaderContentLength, strconv.FormatInt(req.ContentLength, 10))
  792. }
  793. // Dump raw http request
  794. dump, err := httputil.DumpRequest(req, true)
  795. if err != nil {
  796. return nil, fmt.Errorf("failed to dump request: %w", err)
  797. }
  798. // Create test connection
  799. conn := new(testConn)
  800. // Write raw http request
  801. if _, err := conn.r.Write(dump); err != nil {
  802. return nil, fmt.Errorf("failed to write: %w", err)
  803. }
  804. // prepare the server for the start
  805. app.startupProcess()
  806. // Serve conn to server
  807. channel := make(chan error)
  808. go func() {
  809. var returned bool
  810. defer func() {
  811. if !returned {
  812. channel <- fmt.Errorf("runtime.Goexit() called in handler or server panic")
  813. }
  814. }()
  815. channel <- app.server.ServeConn(conn)
  816. returned = true
  817. }()
  818. // Wait for callback
  819. if timeout >= 0 {
  820. // With timeout
  821. select {
  822. case err = <-channel:
  823. case <-time.After(time.Duration(timeout) * time.Millisecond):
  824. return nil, fmt.Errorf("test: timeout error %vms", timeout)
  825. }
  826. } else {
  827. // Without timeout
  828. err = <-channel
  829. }
  830. // Check for errors
  831. if err != nil && !errors.Is(err, fasthttp.ErrGetOnly) {
  832. return nil, err
  833. }
  834. // Read response
  835. buffer := bufio.NewReader(&conn.w)
  836. // Convert raw http response to *http.Response
  837. res, err := http.ReadResponse(buffer, req)
  838. if err != nil {
  839. return nil, fmt.Errorf("failed to read response: %w", err)
  840. }
  841. return res, nil
  842. }
  843. type disableLogger struct{}
  844. func (*disableLogger) Printf(_ string, _ ...interface{}) {
  845. // fmt.Println(fmt.Sprintf(format, args...))
  846. }
  847. func (app *App) init() *App {
  848. // lock application
  849. app.mutex.Lock()
  850. // Only load templates if a view engine is specified
  851. if app.config.Views != nil {
  852. if err := app.config.Views.Load(); err != nil {
  853. log.Warnf("failed to load views: %v", err)
  854. }
  855. }
  856. // create fasthttp server
  857. app.server = &fasthttp.Server{
  858. Logger: &disableLogger{},
  859. LogAllErrors: false,
  860. ErrorHandler: app.serverErrorHandler,
  861. }
  862. // fasthttp server settings
  863. app.server.Handler = app.handler
  864. app.server.Name = app.config.ServerHeader
  865. app.server.Concurrency = app.config.Concurrency
  866. app.server.NoDefaultDate = app.config.DisableDefaultDate
  867. app.server.NoDefaultContentType = app.config.DisableDefaultContentType
  868. app.server.DisableHeaderNamesNormalizing = app.config.DisableHeaderNormalizing
  869. app.server.DisableKeepalive = app.config.DisableKeepalive
  870. app.server.MaxRequestBodySize = app.config.BodyLimit
  871. app.server.NoDefaultServerHeader = app.config.ServerHeader == ""
  872. app.server.ReadTimeout = app.config.ReadTimeout
  873. app.server.WriteTimeout = app.config.WriteTimeout
  874. app.server.IdleTimeout = app.config.IdleTimeout
  875. app.server.ReadBufferSize = app.config.ReadBufferSize
  876. app.server.WriteBufferSize = app.config.WriteBufferSize
  877. app.server.GetOnly = app.config.GETOnly
  878. app.server.ReduceMemoryUsage = app.config.ReduceMemoryUsage
  879. app.server.StreamRequestBody = app.config.StreamRequestBody
  880. app.server.DisablePreParseMultipartForm = app.config.DisablePreParseMultipartForm
  881. // unlock application
  882. app.mutex.Unlock()
  883. return app
  884. }
  885. // ErrorHandler is the application's method in charge of finding the
  886. // appropriate handler for the given request. It searches any mounted
  887. // sub fibers by their prefixes and if it finds a match, it uses that
  888. // error handler. Otherwise it uses the configured error handler for
  889. // the app, which if not set is the DefaultErrorHandler.
  890. func (app *App) ErrorHandler(ctx *Ctx, err error) error {
  891. var (
  892. mountedErrHandler ErrorHandler
  893. mountedPrefixParts int
  894. )
  895. for prefix, subApp := range app.mountFields.appList {
  896. if prefix != "" && strings.HasPrefix(ctx.path, prefix) {
  897. parts := len(strings.Split(prefix, "/"))
  898. if mountedPrefixParts <= parts {
  899. if subApp.configured.ErrorHandler != nil {
  900. mountedErrHandler = subApp.config.ErrorHandler
  901. }
  902. mountedPrefixParts = parts
  903. }
  904. }
  905. }
  906. if mountedErrHandler != nil {
  907. return mountedErrHandler(ctx, err)
  908. }
  909. return app.config.ErrorHandler(ctx, err)
  910. }
  911. // serverErrorHandler is a wrapper around the application's error handler method
  912. // user for the fasthttp server configuration. It maps a set of fasthttp errors to fiber
  913. // errors before calling the application's error handler method.
  914. func (app *App) serverErrorHandler(fctx *fasthttp.RequestCtx, err error) {
  915. c := app.AcquireCtx(fctx)
  916. defer app.ReleaseCtx(c)
  917. var (
  918. errNetOP *net.OpError
  919. netErr net.Error
  920. )
  921. switch {
  922. case errors.As(err, new(*fasthttp.ErrSmallBuffer)):
  923. err = ErrRequestHeaderFieldsTooLarge
  924. case errors.As(err, &errNetOP) && errNetOP.Timeout():
  925. err = ErrRequestTimeout
  926. case errors.As(err, &netErr):
  927. err = ErrBadGateway
  928. case errors.Is(err, fasthttp.ErrBodyTooLarge):
  929. err = ErrRequestEntityTooLarge
  930. case errors.Is(err, fasthttp.ErrGetOnly):
  931. err = ErrMethodNotAllowed
  932. case strings.Contains(err.Error(), "timeout"):
  933. err = ErrRequestTimeout
  934. default:
  935. err = NewError(StatusBadRequest, err.Error())
  936. }
  937. if catch := app.ErrorHandler(c, err); catch != nil {
  938. log.Errorf("serverErrorHandler: failed to call ErrorHandler: %v", catch)
  939. _ = c.SendStatus(StatusInternalServerError) //nolint:errcheck // It is fine to ignore the error here
  940. return
  941. }
  942. }
  943. // startupProcess Is the method which executes all the necessary processes just before the start of the server.
  944. func (app *App) startupProcess() *App {
  945. app.mutex.Lock()
  946. defer app.mutex.Unlock()
  947. app.mountStartupProcess()
  948. // build route tree stack
  949. app.buildTree()
  950. return app
  951. }
  952. // Run onListen hooks. If they return an error, panic.
  953. func (app *App) runOnListenHooks(listenData ListenData) {
  954. if err := app.hooks.executeOnListenHooks(listenData); err != nil {
  955. panic(err)
  956. }
  957. }