Skip to content

Commit

Permalink
Add docs from gofiber/fiber@3ba90c0
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed May 13, 2024
1 parent baa5345 commit 9daeca1
Show file tree
Hide file tree
Showing 10 changed files with 2,911 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/core/api/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: constants
title: 📋 Constants
description: Some constants for Fiber.
sidebar_position: 9
sidebar_position: 8
---

### HTTP methods were copied from net/http.
Expand Down
2 changes: 1 addition & 1 deletion docs/core/api/hooks.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: hooks
title: 🎣 Hooks
sidebar_position: 8
sidebar_position: 7
---

import Tabs from '@theme/Tabs';
Expand Down
2 changes: 1 addition & 1 deletion docs/core/api/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: log
title: 📃 Log
description: Fiber's built-in log package
sidebar_position: 7
sidebar_position: 6
---

We can use logs to observe program behavior, diagnose problems, or configure corresponding alarms.
Expand Down
8 changes: 8 additions & 0 deletions docs/core/client/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "\uD83C\uDF0E Client",
"position": 5,
"link": {
"type": "generated-index",
"description": "HTTP client for Fiber."
}
}
250 changes: 250 additions & 0 deletions docs/core/client/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
---
id: examples
title: 🍳 Examples
description: >-
Client usage examples.
sidebar_position: 5
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

## Basic Auth

<Tabs>
<TabItem value="client" label="Client">

```go
package main

import (
"encoding/base64"
"fmt"

"github.com/gofiber/fiber/v3/client"
)

func main() {
cc := client.New()

out := base64.StdEncoding.EncodeToString([]byte("john:doe"))
resp, err := cc.Get("http://localhost:3000", client.Config{
Header: map[string]string{
"Authorization": "Basic " + out,
},
})
if err != nil {
panic(err)
}

fmt.Print(string(resp.Body()))
}
```
</TabItem>
<TabItem value="server" label="Server">

```go
package main

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/basicauth"
)

func main() {
app := fiber.New()
app.Use(
basicauth.New(basicauth.Config{
Users: map[string]string{
"john": "doe",
},
}),
)

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

app.Listen(":3000")
}
```
</TabItem>
</Tabs>

## TLS

<Tabs>
<TabItem value="client" label="Client">

```go
package main

import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"

"github.com/gofiber/fiber/v3/client"
)

func main() {
cc := client.New()

certPool, err := x509.SystemCertPool()
if err != nil {
panic(err)
}

cert, err := os.ReadFile("ssl.cert")
if err != nil {
panic(err)
}

certPool.AppendCertsFromPEM(cert)
cc.SetTLSConfig(&tls.Config{
RootCAs: certPool,
})

resp, err := cc.Get("https://localhost:3000")
if err != nil {
panic(err)
}

fmt.Print(string(resp.Body()))
}
```
</TabItem>
<TabItem value="server" label="Server">

```go
package main

import (
"github.com/gofiber/fiber/v3"
)

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

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

err := app.Listen(":3000", fiber.ListenConfig{
CertFile: "ssl.cert",
CertKeyFile: "ssl.key",
})
if err != nil {
panic(err)
}
}
```
</TabItem>
</Tabs>

## Cookiejar

### Request

```go
func main() {
jar := client.AcquireCookieJar()
defer client.ReleaseCookieJar(jar)

cc := client.New()
cc.SetCookieJar(jar)

jar.SetKeyValueBytes("httpbin.org", []byte("john"), []byte("doe"))

resp, err := cc.Get("https://httpbin.org/cookies")
if err != nil {
panic(err)
}

fmt.Println(string(resp.Body()))
}
```

<details>
<summary>Click here to see the result</summary>

```json
{
"cookies": {
"john": "doe"
}
}
```

</details>

### Response

```go
func main() {
jar := client.AcquireCookieJar()
defer client.ReleaseCookieJar(jar)

cc := client.New()
cc.SetCookieJar(jar)

_, err := cc.Get("https://httpbin.org/cookies/set/john/doe")
if err != nil {
panic(err)
}

uri := fasthttp.AcquireURI()
defer fasthttp.ReleaseURI(uri)

uri.SetHost("httpbin.org")
uri.SetPath("/cookies")
fmt.Println(jar.Get(uri))
}
```

<details>
<summary>Click here to see the result</summary>

```plaintext
[john=doe; path=/]
```

</details>

### Response 2

```go
func main() {
jar := client.AcquireCookieJar()
defer client.ReleaseCookieJar(jar)

cc := client.New()
cc.SetCookieJar(jar)

_, err := cc.Get("https://httpbin.org/cookies/set/john/doe")
if err != nil {
panic(err)
}

resp, err := cc.Get("https://httpbin.org/cookies")
if err != nil {
panic(err)
}

fmt.Println(resp.String())
}
```

<details>
<summary>Click here to see the result</summary>

```json
{
"cookies": {
"john": "doe"
}
}
```

</details>

0 comments on commit 9daeca1

Please sign in to comment.