Skip to content

Latest commit

History

History
350 lines (275 loc) 路 10.3 KB

application.md

File metadata and controls

350 lines (275 loc) 路 10.3 KB
description
The app instance conventionally denotes the Fiber application.

馃殌 Application

New

This method creates a new App named instance.
You can pass optional settings when creating a new instance

{% code title="Signature" %}

fiber.New(settings ...Settings) *App

{% endcode %}

{% code title="Example" %}

package main

import "github.com/gofiber/fiber/v2"

func main() {
    app := fiber.New()

    // ...

    app.Listen(":3000")
}

{% endcode %}

Settings

You can pass application settings when calling New.

{% code title="Example" %}

func main() {
    // Pass Settings creating a new instance
    app := fiber.New(fiber.Settings{
        Prefork:       true,
        CaseSensitive: true,
        StrictRouting: true,
        ServerHeader:  "Fiber",
    })

    // ...

    log.Fatal(app.Listen(":3000"))
}

{% endcode %}

Or change the settings after initializing an app.

{% code title="Example" %}

func main() {
    app := fiber.New()

    // Or change Settings after creating an instance
    app.Settings.Prefork = true
    app.Settings.CaseSensitive = true
    app.Settings.StrictRouting = true
    app.Settings.ServerHeader = "Fiber"

    // ...

    log.Fatal(app.Listen(":3000"))
}

{% endcode %}

Settings fields

Property Type Description Default
Prefork bool Enables use of theSO_REUSEPORTsocket option. This will spawn multiple Go processes listening on the same port. learn more about socket sharding. false
ServerHeader string Enables the Server HTTP header with the given value. ""
StrictRouting bool When enabled, the router treats /foo and /foo/ as different. Otherwise, the router treats /foo and /foo/ as the same. false
CaseSensitive bool When enabled, /Foo and /foo are different routes. When disabled, /Fooand /foo are treated the same. false
Immutable bool When enabled, all values returned by context methods are immutable. By default they are valid until you return from the handler, see issue #185. false
BodyLimit int Sets the maximum allowed size for a request body, if the size exceeds the configured limit, it sends 413 - Request Entity Too Large response. 4 * 1024 * 1024
Concurrency int Maximum number of concurrent connections. 256 * 1024
DisableKeepalive bool Disable keep-alive connections, the server will close incoming connections after sending the first response to client false
DisableDefaultDate bool When set to true causes the default date header to be excluded from the response. false
DisableDefaultContentType bool When set to true, causes the default Content-Type header to be excluded from the Response. false
TemplateEngine func(raw string, bind interface{}) (string, error) You can specify a custom template function to render different template languages. See our Template Middleware _**_for presets. nil
TemplateFolder string A directory for the application's views. If a directory is set, this will be the prefix for all template paths. c.Render("home", data) -> ./views/home.pug ""
TemplateExtension string If you preset the template file extension, you do not need to provide the full filename in the Render function: c.Render("home", data) -> home.pug "html"
ReadTimeout time.Duration The amount of time allowed to read the full request including body. Default timeout is unlimited. nil
WriteTimeout time.Duration The maximum duration before timing out writes of the response. Default timeout is unlimited. nil
IdleTimeout time.Duration The maximum amount of time to wait for the next request when keep-alive is enabled. If IdleTimeout is zero, the value of ReadTimeout is used. nil

Static

Use the Static method to serve static files such as images, CSS and JavaScript.

{% hint style="info" %} By default, Static will serveindex.html files in response to a request on a directory. {% endhint %}

{% code title="Signature" %}

app.Static(prefix, root string, config ...Static) // => with prefix

{% endcode %}

Use the following code to serve files in a directory named ./public

{% code title="Example" %}

app.Static("/", "./public")

// => http://localhost:3000/hello.html
// => http://localhost:3000/js/jquery.js
// => http://localhost:3000/css/style.css

{% endcode %}

To serve from multiple directories, you can use Static multiple times.

{% code title="Example" %}

// Serve files from "./public" directory:
app.Static("/", "./public")

// Serve files from "./files" directory:
app.Static("/", "./files")

{% endcode %}

{% hint style="info" %} Use a reverse proxy cache like NGINX to improve performance of serving static assets. {% endhint %}

You can use any virtual path prefix (where the path does not actually exist in the file system) for files that are served by the Static method, specify a prefix path for the static directory, as shown below:

{% code title="Example" %}

app.Static("/static", "./public")

// => http://localhost:3000/static/hello.html
// => http://localhost:3000/static/js/jquery.js
// => http://localhost:3000/static/css/style.css

{% endcode %}

If you want to have a little bit more control regarding the settings for serving static files. You could use the fiber.Static struct to enable specific settings.

{% code title="fiber.Static{}" %}

// Static represents settings for serving static files
type Static struct {
    // Transparently compresses responses if set to true
    // This works differently than the github.com/gofiber/compression middleware
    // The server tries minimizing CPU usage by caching compressed files.
    // It adds ".fiber.gz" suffix to the original file name.
    // Optional. Default value false
    Compress bool
    // Enables byte range requests if set to true.
    // Optional. Default value false
    ByteRange bool
    // Enable directory browsing.
    // Optional. Default value false.
    Browse bool
    // Index file for serving a directory.
    // Optional. Default value "index.html".
    Index string
}

{% endcode %}

{% code title="Example" %}

app.Static("/", "./public", fiber.Static{
  Compress:   true,
  ByteRange:  true,
  Browse:     true,
  Index:      "john.html"
})

{% endcode %}

HTTP Methods

Routes an HTTP request, where METHOD is the HTTP method of the request.

{% code title="Signatures" %}

// HTTP methods support :param, :optional? and *wildcards
// You are required to pass a path to each method
app.All(path string, handlers ...func(*Ctx)) *Fiber
app.Get
app.Put
app.Post
app.Head
app.Patch
app.Trace
app.Delete
app.Connect
app.Options

// Use() will only match the beggining of each path
// i.e. "/john" will match "/john/doe", "/johnnnn"
// Use() does not support :param & :optional? in path
app.Use(handlers ...func(*Ctx))
app.Use(prefix string, handlers ...func(*Ctx)) *Fiber

{% endcode %}

{% code title="Example" %}

app.Use("/api", func(c *fiber.Ctx) {
  c.Set("X-Custom-Header", random.String(32))
  c.Next()
})
app.Get("/api/list", func(c *fiber.Ctx) {
  c.Send("I'm a GET request!")
})
app.Post("/api/register", func(c *fiber.Ctx) {
  c.Send("I'm a POST request!")
})

{% endcode %}

Group

You can group routes by creating a *Group struct.

Signature

app.Group(prefix string, handlers ...func(*Ctx)) *Group

Example

func main() {
  app := fiber.New()

  api := app.Group("/api", cors())  // /api

  v1 := api.Group("/v1", mysql())   // /api/v1
  v1.Get("/list", handler)          // /api/v1/list
  v1.Get("/user", handler)          // /api/v1/user

  v2 := api.Group("/v2", mongodb()) // /api/v2
  v2.Get("/list", handler)          // /api/v2/list
  v2.Get("/user", handler)          // /api/v2/user

  log.Fatal(app.Listen(":3000"))
}

Listen

Binds and listens for connections on the specified address.

{% code title="Signature" %}

app.Listen(address string, tls ...*tls.Config) error

{% endcode %}

{% code title="Examples" %}

app.Listen(":8080")
app.Listen("8080")
app.Listen(":8080")
app.Listen("127.0.0.1:8080")

{% endcode %}

To enable TLS/HTTPS you can append a TLS config.

{% code title="Example" %}

cer, err := tls.LoadX509KeyPair("server.crt", "server.key")
if err != nil {
    log.Fatal(err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}

app.Listen(":443", config)

{% endcode %}

Serve

You can pass your own net.Listener using the Serve method.

{% code title="Signature" %}

app.Serve(ln net.Listener, tls ...*tls.Config) error

{% endcode %}

{% hint style="warning" %} Serve does not support the ****Prefork feature. {% endhint %}

{% code title="Example" %}

if ln, err = net.Listen("tcp4", ":8080"); err != nil {
    log.Fatal(err)
}

app.Serve(ln)

{% endcode %}

Test

Testing your application is done with the Test method. Use this method for creating _test.go files or when you need to debug your routing logic. The default timeout is 200ms if you want to disable a timeout completely, pass -1 as a second argument.

{% code title="Signature" %}

app.Test(req *http.Request, msTimeout ...int) (*http.Response, error)

{% endcode %}

{% code title="Example" %}

// Create route with GET method for test:
app.Get("/", func(c *Ctx) {
  fmt.Println(c.BaseURL())              // => http://google.com
  fmt.Println(c.Get("X-Custom-Header")) // => hi

  c.Send("hello, World!")
})

// http.Request
req, _ := http.NewRequest("GET", "http://google.com", nil)
req.Header.Set("X-Custom-Header", "hi")

// http.Response
resp, _ := app.Test(req)

// Do something with results:
if resp.StatusCode == fiber.StatusOK {
  body, _ := ioutil.ReadAll(resp.Body)
  fmt.Println(string(body)) // => Hello, World!
}

{% endcode %}