app.go 31 KB

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