From 8c00d9cebddfe918aca9072c49899c8953746be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serhat=20=C5=9Eevki=20Din=C3=A7er?= Date: Sat, 7 May 2022 11:56:12 +0300 Subject: [PATCH 1/3] :link: update links to point to go.dev --- README.md | 8 ++++---- api/app.md | 2 +- api/ctx.md | 2 +- extra/faq.md | 2 +- guide/error-handling.md | 2 +- original/README.md | 6 +++--- original/application.md | 4 ++-- original/context.md | 2 +- original/middleware.md | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 635562b4606..33c9c1e5f29 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ description: >- # 👋 Welcome -**Fiber** is an [Express](https://github.com/expressjs/express) inspired **web framework** built on top of [Fasthttp](https://github.com/valyala/fasthttp), the **fastest** HTTP engine for [Go](https://golang.org/doc/). Designed to **ease** things up for **fast** development with **zero memory allocation** and **performance** in mind. +**Fiber** is an [Express](https://github.com/expressjs/express) inspired **web framework** built on top of [Fasthttp](https://github.com/valyala/fasthttp), the **fastest** HTTP engine for [Go](https://go.dev/doc/). 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**. @@ -14,9 +14,9 @@ These docs are for **Fiber v2**, which was released on **September 15th, 2020**. ## Installation -First of all, [download](https://golang.org/dl/) and install Go. `1.14` or higher is required. +First of all, [download](https://go.dev/dl/) and install Go. `1.14` or higher is required. -Installation is done using the [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: +Installation is done using the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: ```bash go get github.com/gofiber/fiber/v2 @@ -39,7 +39,7 @@ func handler(c *fiber.Ctx) error { } ``` -If you need to persist such values outside the handler, make copies of their **underlying buffer** using the [copy](https://golang.org/pkg/builtin/#copy) builtin. Here is an example for persisting a string: +If you need to persist such values outside the handler, make copies of their **underlying buffer** using the [copy](https://pkg.go.dev/builtin/#copy) builtin. Here is an example for persisting a string: ```go func handler(c *fiber.Ctx) error { diff --git a/api/app.md b/api/app.md index 356652dd3ea..47b1e4c3a46 100644 --- a/api/app.md +++ b/api/app.md @@ -603,7 +603,7 @@ Using `ListenMutualTLS` defaults to the following config \( use `Listener` to pr ## Listener -You can pass your own [`net.Listener`](https://golang.org/pkg/net/#Listener) using the `Listener` method. This method can be used to enable **TLS/HTTPS** with a custom tls.Config. +You can pass your own [`net.Listener`](https://pkg.go.dev/net/#Listener) using the `Listener` method. This method can be used to enable **TLS/HTTPS** with a custom tls.Config. {% code title="Signature" %} ```go diff --git a/api/ctx.md b/api/ctx.md index ba2468f6c48..d9d7b707e19 100644 --- a/api/ctx.md +++ b/api/ctx.md @@ -1375,7 +1375,7 @@ app.Get("/back", func(c *fiber.Ctx) error { ## Render -Renders a view with data and sends a `text/html` response. By default `Render` uses the default [**Go Template engine**](https://golang.org/pkg/html/template/). If you want to use another View engine, please take a look at our [**Template middleware**](https://github.com/gofiber/template). +Renders a view with data and sends a `text/html` response. By default `Render` uses the default [**Go Template engine**](https://pkg.go.dev/html/template/). If you want to use another View engine, please take a look at our [**Template middleware**](https://github.com/gofiber/template). {% code title="Signature" %} ```go diff --git a/extra/faq.md b/extra/faq.md index 1ab86727266..36a14497402 100644 --- a/extra/faq.md +++ b/extra/faq.md @@ -55,7 +55,7 @@ Fiber currently supports 8 template engines in our [gofiber/template](https://gi * [Amber](https://github.com/eknkc/amber) * [Django](https://github.com/flosch/pongo2) * [Handlebars](https://github.com/aymerick/raymond) -* [HTML](https://golang.org/pkg/html/template/) +* [HTML](https://pkg.go.dev/html/template/) * [Jet](https://github.com/CloudyKit/jet) * [Mustache](https://github.com/cbroglie/mustache) * [Pug](https://github.com/Joker/jade) diff --git a/guide/error-handling.md b/guide/error-handling.md index 5ec89ba7169..3577fb5319c 100644 --- a/guide/error-handling.md +++ b/guide/error-handling.md @@ -22,7 +22,7 @@ app.Get("/", func(c *fiber.Ctx) error { {% endtab %} {% endtabs %} -Fiber does not handle [panics](https://blog.golang.org/defer-panic-and-recover) by default. To recover from a panic thrown by any handler in the stack, you need to include the `Recover` middleware below: +Fiber does not handle [panics](https://go.dev/blog/defer-panic-and-recover) by default. To recover from a panic thrown by any handler in the stack, you need to include the `Recover` middleware below: {% code title="Example" %} ```go diff --git a/original/README.md b/original/README.md index 61cafc30060..03627261283 100644 --- a/original/README.md +++ b/original/README.md @@ -6,13 +6,13 @@ description: An API documentation so you can start building web apps with Fiber. [![](https://img.shields.io/github/release/gofiber/fiber?style=flat-square)](https://github.com/gofiber/fiber/releases) [![](https://img.shields.io/badge/go.dev-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/gofiber/fiber?tab=doc) [![](https://goreportcard.com/badge/github.com/gofiber/fiber?style=flat-square)](https://goreportcard.com/report/github.com/gofiber/fiber) [![](https://img.shields.io/badge/coverage-91%25-brightgreen?style=flat-square)](https://gocover.io/github.com/gofiber/fiber) [![](https://img.shields.io/github/workflow/status/gofiber/fiber/Test?label=tests&style=flat-square)](https://github.com/gofiber/fiber/actions?query=workflow%3ATest) [![](https://img.shields.io/github/workflow/status/gofiber/fiber/Gosec?label=gosec&style=flat-square)](https://github.com/gofiber/fiber/actions?query=workflow%3AGosec) -**Fiber** is an [Express](https://github.com/expressjs/express) inspired **web framework** build on top of [Fasthttp](https://github.com/valyala/fasthttp), the **fastest** HTTP engine for [Go](https://golang.org/doc/). Designed to **ease** things up for **fast** development with **zero memory allocation** and **performance** in mind. +**Fiber** is an [Express](https://github.com/expressjs/express) inspired **web framework** build on top of [Fasthttp](https://github.com/valyala/fasthttp), the **fastest** HTTP engine for [Go](https://go.dev/doc/). Designed to **ease** things up for **fast** development with **zero memory allocation** and **performance** in mind. ## Installation -First of all, [download](https://golang.org/dl/) and install Go. `1.11` or higher is required. +First of all, [download](https://go.dev/dl/) and install Go. `1.11` or higher is required. -Installation is done using the [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: +Installation is done using the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: ```bash go get -u github.com/gofiber/fiber diff --git a/original/application.md b/original/application.md index 30fbb035659..5034833f405 100644 --- a/original/application.md +++ b/original/application.md @@ -275,7 +275,7 @@ app.Listen("127.0.0.1:8080") ``` {% endcode %} -To enable **TLS/HTTPS** you can append a [**TLS config**](https://golang.org/pkg/crypto/tls/#Config). +To enable **TLS/HTTPS** you can append a [**TLS config**](https://pkg.go.dev/crypto/tls/#Config). {% code title="Example" %} ```go @@ -291,7 +291,7 @@ app.Listen(":443", config) ## Serve -You can pass your own [`net.Listener`](https://golang.org/pkg/net/#Listener) using the `Serve` method. +You can pass your own [`net.Listener`](https://pkg.go.dev/net/#Listener) using the `Serve` method. {% code title="Signature" %} ```go diff --git a/original/context.md b/original/context.md index 4772345735b..a6773374939 100644 --- a/original/context.md +++ b/original/context.md @@ -956,7 +956,7 @@ app.Get("/", func(c *fiber.Ctx) { ## Render -Renders a template with data and sends a `text/html` response. By default `Render` uses the default [**Go Template engine**](https://golang.org/pkg/html/template/). If you want to use another engine, please take a look at our [**Template middleware**](middleware.md#template). +Renders a template with data and sends a `text/html` response. By default `Render` uses the default [**Go Template engine**](https://pkg.go.dev/html/template/). If you want to use another engine, please take a look at our [**Template middleware**](middleware.md#template). {% code title="Signature" %} ```go diff --git a/original/middleware.md b/original/middleware.md index fa905904522..ad0d8cd21c5 100644 --- a/original/middleware.md +++ b/original/middleware.md @@ -315,7 +315,7 @@ func main() { ## Template -By default Fiber comes with the [**default HTML template**](https://golang.org/pkg/html/template/) engine, but this middleware contains third party rendering engines. +By default Fiber comes with the [**default HTML template**](https://pkg.go.dev/html/template/) engine, but this middleware contains third party rendering engines. **Installation** From 892cea2eb8e6e4144a72a5456ed14e7382795bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serhat=20=C5=9Eevki=20Din=C3=A7er?= Date: Sat, 7 May 2022 11:56:55 +0300 Subject: [PATCH 2/3] :bar_chart: update monitor mw docs --- api/middleware/monitor.md | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/api/middleware/monitor.md b/api/middleware/monitor.md index dcd4b24dc56..16e6a1a0fc2 100644 --- a/api/middleware/monitor.md +++ b/api/middleware/monitor.md @@ -33,23 +33,40 @@ import ( func main() { app := fiber.New() - - app.Get("/dashboard", monitor.New()) - + + app.Get("/metrics", monitor.New(monitor.Config{Title: "MyService Metrics Page"})) + log.Fatal(app.Listen(":3000")) } ``` +You can also access the API endpoint with +`curl -X GET -H "Accept: application/json" http://localhost:3000/metrics` which returns: +```json +{"pid":{ "cpu":0.4568381746582226, "ram":20516864, "conns":3 }, + "os": { "cpu":8.759124087593099, "ram":3997155328, "conns":44, + "total_ram":8245489664, "load_avg":0.51 }} +``` ## Config ```go // Config defines the config for middleware. type Config struct { + // Metrics page title + // + // Optional. Default: "Fiber Monitor" + Title string + + // Refresh period + // + // Optional. Default: 3 seconds + Refresh time.Duration + // To disable serving HTML, you can make true this option. // // Optional. Default: false APIOnly bool - + // Next defines a function to skip this middleware when returned true. // // Optional. Default: nil @@ -61,7 +78,9 @@ type Config struct { ```go var ConfigDefault = Config{ + Title: "Fiber Monitor", + Refresh: 3 * time.Second, APIOnly: false, - Next: nil, + Next: nil, } -``` \ No newline at end of file +``` From 037b3210ac43720a324f24a6bbb2ab0f40f16712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serhat=20=C5=9Eevki=20Din=C3=A7er?= Date: Sat, 7 May 2022 12:10:08 +0300 Subject: [PATCH 3/3] :scroll: overhaul README --- README.md | 90 ++++++++++++++++++++++++------------------------------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 33c9c1e5f29..b140dfbda3a 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,22 @@ ---- -description: >- - An online API documentation with examples so you can start building web apps - with Fiber right away! ---- - -# 👋 Welcome +## 👋 Welcome to Fiber Docs +An online API documentation with examples so you can start building web apps with Fiber right away! **Fiber** is an [Express](https://github.com/expressjs/express) inspired **web framework** built on top of [Fasthttp](https://github.com/valyala/fasthttp), the **fastest** HTTP engine for [Go](https://go.dev/doc/). 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 +### Installation First of all, [download](https://go.dev/dl/) and install Go. `1.14` or higher is required. Installation is done using the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: -```bash +``` go get github.com/gofiber/fiber/v2 ``` -## Zero Allocation - -{% hint style="warning" %} -Some values returned from \***fiber.Ctx** are **not** immutable by default -{% endhint %} +### Zero Allocation +Some values returned from \***fiber.Ctx** are **not** immutable by default. 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: @@ -56,7 +46,7 @@ func handler(c *fiber.Ctx) error { } ``` -We created a custom `ImmutableString` a function that does the above and is available in the [gofiber/utils](https://github.com/gofiber/utils) package. +We created a custom `ImmutableString` function that does the above and is available in the [gofiber/utils](https://github.com/gofiber/utils) package. ```go app.Get("/:foo", func(c *fiber.Ctx) error { @@ -67,13 +57,13 @@ app.Get("/:foo", func(c *fiber.Ctx) error { }) ``` -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. +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**](https://github.com/gofiber/fiber/issues/426) **and** [**\#185**](https://github.com/gofiber/fiber/issues/185). +For more information, please check [**\#426**](https://github.com/gofiber/fiber/issues/426) and [**\#185**](https://github.com/gofiber/fiber/issues/185). -## Hello, World! +### Hello, World! -Embedded below is essentially the most straightforward **Fiber** app, which you can create. +Embedded below is essentially the most straightforward **Fiber** app you can create: ```go package main @@ -81,13 +71,13 @@ package main import "github.com/gofiber/fiber/v2" func main() { - app := fiber.New() + app := fiber.New() - app.Get("/", func(c *fiber.Ctx) error { - return c.SendString("Hello, World!") - }) + app.Get("/", func(c *fiber.Ctx) error { + return c.SendString("Hello, World!") + }) - app.Listen(":3000") + app.Listen(":3000") } ``` @@ -95,15 +85,13 @@ func main() { go run server.go ``` -Browse to `http://localhost:3000,` and you should see `Hello, World!` on the page. +Browse to `http://localhost:3000` and you should see `Hello, World!` on the page. -## Basic routing +### 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\). +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`, etc.). -{% hint style="info" %} -Each route can have **multiple handler functions**, that is executed when the route is matched. -{% endhint %} +Each route can have **multiple handler functions** that are executed when the route is matched. Route definition takes the following structures: @@ -112,17 +100,17 @@ Route definition takes the following structures: app.Method(path string, ...func(*fiber.Ctx) error) ``` -* `app` is an instance of **Fiber**. -* `Method` is an [HTTP request method](https://docs.gofiber.io/api/app#route-handlers), 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](https://docs.gofiber.io/api/ctx) executed when the route is matched. +- `app` is an instance of **Fiber** +- `Method` is an [HTTP request method](https://docs.gofiber.io/api/app#route-handlers): `GET`, `PUT`, `POST`, etc. +- `path` is a virtual path on the server +- `func(*fiber.Ctx) error` is a callback function containing the [Context](https://docs.gofiber.io/api/ctx) executed when the route is matched **Simple route** ```go // Respond with "Hello, World!" on root path, "/" app.Get("/", func(c *fiber.Ctx) error { - return c.SendString("Hello, World!") + return c.SendString("Hello, World!") }) ``` @@ -132,8 +120,8 @@ app.Get("/", func(c *fiber.Ctx) error { // 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 + return c.SendString("value: " + c.Params("value")) + // => Get request with value: hello world }) ``` @@ -143,11 +131,11 @@ app.Get("/:value", func(c *fiber.Ctx) error { // 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?") + if c.Params("name") != "" { + return c.SendString("Hello " + c.Params("name")) + // => Hello john + } + return c.SendString("Where is john?") }) ``` @@ -157,19 +145,19 @@ app.Get("/:name?", func(c *fiber.Ctx) error { // 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 + return c.SendString("API path: " + c.Params("*")) + // => API path: user/john }) ``` -## Static files +### 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: ```go -app.Static(prefix, root string) +app.Static(prefix, root string, config ...Static) ``` Use the following code to serve files in a directory named `./public`: @@ -190,7 +178,7 @@ 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](https://blog.logrocket.com/express-style-api-go-fiber/) +### 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](https://blog.logrocket.com/express-style-api-go-fiber/).