Fiber is a Express Inspired Web framework, based on the use of Go The fastest HTTP engine written in Fasthttp Build. It aims to make rapid development easier through zero memory allocation and high-performance services.
⚡ , quick start
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") }
🤖 Benchmarking
These tests are performed by TechEmpower and Go Web Done. If you want to see all the results, please visit our Wiki .
⚙️ install
Make sure it is installed( download )1.14 or later Go.
By creating a folder and running go mod init GitHub. In the folder com/your/repo ( Learn more )To initialize the project, and then use go get Command to install Fiber:
go get -u github.com/gofiber/fiber/v2
🎯 characteristic
- Powerful route
- Static file service
- limit performance
- Low memory usage
- API interface
- middleware and Next support
- fast Server side programming
- template engine
- WebSocket support
- Frequency limit
- 15 languages
- More please Explore documents
💡 Philosophy
from Node.js Switch to Go Your new gopher needs to Go through a learning process before you start building Web applications or microservices. As a Web framework, Fiber is created according to the minimalist idea and UNIX way, so the new gopher can quickly enter the Go world in a warm and reliable welcome.
Fiber is inspired by Express, the most popular Web framework on the Internet. We combine the ease of use of Express with the raw performance of Go. If you have ever been in node JS (using Express or similar tools), many methods and principles should be very easy for you to understand.
We focus on the whole Internet users in issues And Discord channel In order to create a fast, flexible and friendly Go web framework that meets any task, deadline and developer skills. Just like Express in the JavaScript world.
limit
- Because fiber uses the unsafe feature, it may be incompatible with the latest Go version. Fiber 2.18.0 has been verified on Go 1.14 to 1.17.
- Fiber is incompatible with the net/http interface. This means that you cannot use gqlen, go swagger, or any other project that belongs to the net/http ecosystem.
👀 Example
Some common examples are listed below. If you want to see more code examples, please visit our Recipes Code base or API documentation .
📖 Basic routing
func main() { app := fiber.New() // GET /api/register app.Get("/api/*", func(c *fiber.Ctx) error { msg := fmt.Sprintf("✋ %s", c.Params("*")) return c.SendString(msg) // => ✋ register }) // GET /flights/LAX-SFO app.Get("/flights/:from-:to", func(c *fiber.Ctx) error { msg := fmt.Sprintf("💸 From: %s, To: %s", c.Params("from"), c.Params("to")) return c.SendString(msg) // => 💸 From: LAX, To: SFO }) // GET /dictionary.txt app.Get("/:file.:ext", func(c *fiber.Ctx) error { msg := fmt.Sprintf("📃 %s.%s", c.Params("file"), c.Params("ext")) return c.SendString(msg) // => 📃 dictionary.txt }) // GET /john/75 app.Get("/:name/:age/:gender?", func(c *fiber.Ctx) error { msg := fmt.Sprintf("👴 %s is %s years old", c.Params("name"), c.Params("age")) return c.SendString(msg) // => 👴 john is 75 years old }) // GET /john app.Get("/:name", func(c *fiber.Ctx) error { msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name")) return c.SendString(msg) // => Hello john 👋! }) log.Fatal(app.Listen(":3000")) }
📖 Static file service
func main() { app := fiber.New() app.Static("/", "./public") // => http://localhost:3000/js/script.js // => http://localhost:3000/css/style.css app.Static("/prefix", "./public") // => http://localhost:3000/prefix/js/script.js // => http://localhost:3000/prefix/css/style.css app.Static("*", "./public/index.html") // => http://localhost:3000/any/path/shows/index/html log.Fatal(app.Listen(":3000")) }
📖 middleware and Next
func main() { app := fiber.New() // Match any route app.Use(func(c *fiber.Ctx) error { fmt.Println("🥇 First handler") return c.Next() }) // Match all routes starting with /api app.Use("/api", func(c *fiber.Ctx) error { fmt.Println("🥈 Second handler") return c.Next() }) // GET /api/register app.Get("/api/list", func(c *fiber.Ctx) error { fmt.Println("🥉 Last handler") return c.SendString("Hello, World 👋!") }) log.Fatal(app.Listen(":3000")) }
📚 Show more code examples
🧬 Internal Middleware
The following is a list of middleware included in the Fiber framework
middleware | describe |
---|---|
basicauth | The basic authentication middleware provides HTTP basic authentication. It calls the next handler for valid credentials and 401 Unauthorized. For missing or invalid credentials |
compress | Fiber's compression middleware supports' deflate ',' gzip 'and' brotli 'by default |
cache | Intercept and cache responses |
cors | Use various options to enable cross - source resource sharing (CORS) |
csrf | Protect vulnerabilities from CSRF |
filesystem | Optical file system middleware, special thanks to Alireza Salary |
favicon | If a file path is provided, ignore the icon in the log or service from memory |
limiter | Speed limiting middleware for optical fiber. Used to limit duplicate requests to public APIs and / or endpoints, such as password reset |
logger | HTTP request / response log |
pprof | Special thanks to Matthew Lee (@mthli) |
proxy | Allows you to proxy requests to multiple servers |
requestid | Add a requesttid for each request |
recover | Recovery middleware recovers from panic anywhere in the stack chain and processes control to centralized ErrorHandler. |
timeout | The maximum time to add a request. If it exceeds, it will be sent to ErrorHandler |
🧬 External Middleware
The following is a list of externally hosted middleware, which is provided by Fiber team maintain.
middleware | describe |
---|---|
adaptor | net/http handler and Fiber request handler, thank you very much @ arsmn! |
helmet | Help protect your application by setting various HTTP headers. |
jwt | JWT returns a JSON Web Token(JWT) authentication middleware. |
keyauth | Key auth middleware provides key based authentication. |
rewrite | Rewrite middleware rewrites the URL path according to the provided rules. It helps to be backward compatible or create clearer and more descriptive links. |
session | This session middleware is based on @savsgio MIT's fasthttp/session. Special thanks to @ Thomas vvugt for helping this middleware! |
template | The software package contains 8 template engines, which can be used with fiber v1 10. X go 1.13 or later. |
websocket | Based on fastttp websocket for fiber and Locals support! |
🌱 Third party Middleware
This is the list of middleware created by Fiber community. If you want to see your own middleware, please create PR.
- arsmn/fiber-casbin
- arsmn/fiber-introspect
- arsmn/fiber-swagger
- arsmn/gqlgen
- codemicro/fiber-cache
- sujit-baniya/fiber-boilerplate
- juandiii/go-jwk-security
- kiyonlin/fiber_limiter
- shareed2k/fiber_limiter
- shareed2k/fiber_tracing
- thomasvvugt/fiber-boilerplate
- ansrivas/fiberprometheus
- LdDl/fiber-long-poll
- K0enM/fiber_vhost
- theArtechnology/fiber-inertia
- aschenmaker/fiber-health-check
- elastic/apmfiber
👍 contribution
If you want to say thank you or support the positive development of Fiber:
- For Fiber GitHub Star Order one ⭐ stars.
- stay Twitter Publish information about the project on Tweet.
- stay Medium,Dev.to Or write comments or tutorials on your personal blog.
- By donation A cup of coffee To support this project.
☕ supporter
Fiber is an open source project that relies on donations to pay bills, such as our domain name, gitbook, netlife and serverless hosting. If you want to support fiber, you can ☕ Buy a cup of coffee here
This article was first published in LearnKu.com On the website.