All Projects → gofiber → Docs

gofiber / Docs

Licence: apache-2.0
📚 Documentation for 🚀 Fiber

Projects that are alternatives of or similar to Docs

Recipes
📁 Examples for 🚀 Fiber
Stars: ✭ 691 (+471.07%)
Mutual labels:  hacktoberfest, fiber
Settingsguide
More extensive explanations of Cura slicing settings.
Stars: ✭ 55 (-54.55%)
Mutual labels:  hacktoberfest, documentation
Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+586.78%)
Mutual labels:  hacktoberfest, documentation
Docusaurus
Easy to maintain open source documentation websites.
Stars: ✭ 29,053 (+23910.74%)
Mutual labels:  hacktoberfest, documentation
Go Compression.github.io
The Hitchhiker's Guide to Compression
Stars: ✭ 106 (-12.4%)
Mutual labels:  hacktoberfest, documentation
Pdoc
🐍 ➡️ 📜 Auto-generate API documentation for Python projects
Stars: ✭ 604 (+399.17%)
Mutual labels:  hacktoberfest, documentation
Bonita Doc
This repository contains the sources of the Bonita documentation site. It uses Markdown to create the documentation content.
Stars: ✭ 31 (-74.38%)
Mutual labels:  hacktoberfest, documentation
Documentation Html Template
A Sample Documentation Template for Themes, Templates and Plugins
Stars: ✭ 322 (+166.12%)
Mutual labels:  hacktoberfest, documentation
Docs
The source for https://www.gobuffalo.io
Stars: ✭ 105 (-13.22%)
Mutual labels:  hacktoberfest, documentation
Swagger Combine
Combines multiple Swagger schemas into one dereferenced schema.
Stars: ✭ 102 (-15.7%)
Mutual labels:  hacktoberfest, documentation
Circleci Docs
Documentation for CircleCI.
Stars: ✭ 501 (+314.05%)
Mutual labels:  hacktoberfest, documentation
Component Docs
📝 Simple documentation for your React components
Stars: ✭ 116 (-4.13%)
Mutual labels:  hacktoberfest, documentation
Ava Docs
Localized docs for AVA
Stars: ✭ 455 (+276.03%)
Mutual labels:  hacktoberfest, documentation
Docs
CakePHP CookBook
Stars: ✭ 653 (+439.67%)
Mutual labels:  hacktoberfest, documentation
Openshift Docs
OpenShift 3 and 4 product and community documentation
Stars: ✭ 452 (+273.55%)
Mutual labels:  hacktoberfest, documentation
Tldr
📚 Collaborative cheatsheets for console commands
Stars: ✭ 36,408 (+29989.26%)
Mutual labels:  hacktoberfest, documentation
Docs
Parse Platform docs
Stars: ✭ 296 (+144.63%)
Mutual labels:  hacktoberfest, documentation
Cordova Docs
Apache Cordova Documentation
Stars: ✭ 315 (+160.33%)
Mutual labels:  hacktoberfest, documentation
Docs
Documentation for Tasmota (https://github.com/arendst/Tasmota)
Stars: ✭ 55 (-54.55%)
Mutual labels:  hacktoberfest, documentation
React Styleguidist
Isolated React component development environment with a living style guide
Stars: ✭ 10,172 (+8306.61%)
Mutual labels:  hacktoberfest, documentation

description: >- An online API documentation with examples so you can start building web apps with Fiber right away!

👋 Welcome

Fiber is an Express inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind.

{% hint style="warning" %} These docs are for Fiber v2, which was released on September 15th, 2020. {% endhint %}

Installation

First of all, download and install Go. 1.14 or higher is required.

Installation is done using the go get command:

go get github.com/gofiber/fiber/v2

Zero Allocation

{% hint style="warning" %} Some values returned from *fiber.Ctx are not immutable by default {% endhint %}

Because fiber is optimized for high-performance, values returned from fiber.Ctx are not immutable by default and will be re-used across requests. As a rule of thumb, you must only use context values within the handler, and you must not keep any references. As soon as you return from the handler, any values you have obtained from the context will be re-used in future requests and will change below your feet. Here is an example:

func handler(c *fiber.Ctx) error {
    // Variable is only valid within this handler
    result := c.Params("foo") 

    // ...
}

If you need to persist such values outside the handler, make copies of their underlying buffer using the copy builtin. Here is an example for persisting a string:

func handler(c *fiber.Ctx) error {
    // Variable is only valid within this handler
    result := c.Params("foo")

    // Make a copy
    buffer := make([]byte, len(result))
    copy(buffer, result)
    resultCopy := string(buffer) 
    // Variable is now valid forever

    // ...
}

We created a custom ImmutableString a function that does the above and is available in the gofiber/utils package.

app.Get("/:foo", func(c *fiber.Ctx) error {
    // Variable is now immutable
    result := utils.ImmutableString(c.Params("foo")) 

    // ...
})

Alternatively, you can also use the Immutable setting. It will make all values returned from the context immutable, allowing you to persist them anywhere. Of course, this comes at the cost of performance.

For more information, please check #426 and #185.

Hello, World!

Embedded below is essentially the most straightforward Fiber app, which you can create.

package main

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

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

  app.Get("/", func(c *fiber.Ctx) error {
    return c.SendString("Hello, World!")
  })

  app.Listen(":3000")
}
go run server.go

Browse to http://localhost:3000, and you should see Hello, World! on the page.

Basic routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, PUT, POST and so on).

{% hint style="info" %} Each route can have multiple handler functions, that is executed when the route is matched. {% endhint %}

Route definition takes the following structures:

// Function signature
app.Method(path string, ...func(*fiber.Ctx) error)
  • app is an instance of Fiber.
  • Method is an HTTP request method, in capitalization: Get, Put, Post, etc.
  • path is a virtual path on the server.
  • func(*fiber.Ctx) error is a callback function containing the Context executed when the route is matched.

Simple route

// Respond with "Hello, World!" on root path, "/"
app.Get("/", func(c *fiber.Ctx) error {
  return c.SendString("Hello, World!")
})

Parameters

// GET http://localhost:8080/hello%20world

app.Get("/:value", func(c *fiber.Ctx) error {
  return c.SendString("value: " + c.Params("value"))
  // => Get request with value: hello world
})

Optional parameter

// GET http://localhost:3000/john

app.Get("/:name?", func(c *fiber.Ctx) error {
  if c.Params("name") != "" {
    return c.SendString("Hello " + c.Params("name"))
    // => Hello john
  }
  return c.SendString("Where is john?")
})

Wildcards

// GET http://localhost:3000/api/user/john

app.Get("/api/*", func(c *fiber.Ctx) error {
  return c.SendString("API path: " + c.Params("*"))
  // => API path: user/john
})

Static files

To serve static files such as images, CSS, and JavaScript files, replace your function handler with a file or directory string.

Function signature:

app.Static(prefix, root string)

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

app := fiber.New()

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

app.Listen(":3000")

Now, you can load the files that are in the ./public directory:

http://localhost:8080/hello.html
http://localhost:8080/js/jquery.js
http://localhost:8080/css/style.css

Note

For more information on how to build APIs in Go with Fiber, please check out this excellent article on building an express-style API in Go with Fiber

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].