Skip to content

Commit e73da2d

Browse files
1 parent f5f2623 commit e73da2d

File tree

21 files changed

+2576
-0
lines changed

21 files changed

+2576
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: 👋 Welcome
3+
sidebar_position: 1
4+
---
5+
6+
<div align="center">
7+
<img height="125" alt="Fiber" src="https://raw.githubusercontent.com/gofiber/contrib/master/.github/logo-dark.svg#gh-dark-mode-only" />
8+
<img height="125" alt="Fiber" src="https://raw.githubusercontent.com/gofiber/contrib/master/.github/logo.svg#gh-light-mode-only" />
9+
<br />
10+
11+
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord)
12+
![Test](https://github.com/gofiber/contrib/workflows/Tests/badge.svg)
13+
![Security](https://github.com/gofiber/contrib/workflows/Security/badge.svg)
14+
![Linter](https://github.com/gofiber/contrib/workflows/Linter/badge.svg)
15+
16+
Repository for third party middlewares with dependencies.
17+
18+
</div>
19+
20+
## 📑 Middleware Implementations
21+
22+
* [Casbin](./casbin/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Casbin%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-casbin.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
23+
* [Fgprof](./fgprof/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Fgprof%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fgprof.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
24+
* [Fiberi18n](./fiberi18n/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fiberi18n%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fiberi18n.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
25+
* [Fibersentry](./fibersentry/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fibersentry%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fibersentry.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
26+
* [Fiberzap](./fiberzap/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fiberzap%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fiberzap.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
27+
* [Fiberzerolog](./fiberzerolog/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fiberzerolog%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fiberzerolog.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
28+
* [HCaptcha](./hcaptcha/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+hcaptcha%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-hcaptcha.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
29+
* [JWT](./jwt/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+jwt%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-jwt.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
30+
* [Loadshed](./loadshed/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+loadshed%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-loadshed.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
31+
* [NewRelic](./fibernewrelic/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+fibernewrelic%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-fibernewrelic.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
32+
* [Monitor](./monitor/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+Monitor%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-monitor.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
33+
* [Open Policy Agent](./opafiber/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+opafiber%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-opafiber.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
34+
* [Otelfiber (OpenTelemetry)](./otelfiber/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+otelfiber%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-otelfiber.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
35+
* [Paseto](./paseto/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+paseto%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-paseto.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
36+
* [Socket.io](./socketio/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+socketio%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-socketio.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
37+
* [Swagger](./swagger/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+swagger%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-swagger.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
38+
* [Websocket](./websocket/README.md) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Test+websocket%22"> <img src="https://img.shields.io/github/actions/workflow/status/gofiber/contrib/test-websocket.yml?branch=main&label=%F0%9F%A7%AA%20&style=flat&color=75C46B" /> </a>
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
id: casbin
3+
---
4+
5+
# Casbin
6+
7+
![Release](https://img.shields.io/github/v/tag/gofiber/contrib?filter=casbin*)
8+
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord)
9+
![Test](https://github.com/gofiber/contrib/workflows/Tests/badge.svg)
10+
![Security](https://github.com/gofiber/contrib/workflows/Security/badge.svg)
11+
![Linter](https://github.com/gofiber/contrib/workflows/Linter/badge.svg)
12+
13+
Casbin middleware for Fiber.
14+
15+
**Note: Requires Go 1.18 and above**
16+
17+
## Install
18+
```
19+
go get -u github.com/gofiber/fiber/v2
20+
go get -u github.com/gofiber/contrib/casbin
21+
```
22+
choose an adapter from [here](https://casbin.org/docs/adapters)
23+
```
24+
go get -u github.com/casbin/xorm-adapter
25+
```
26+
27+
## Signature
28+
```go
29+
casbin.New(config ...casbin.Config) *casbin.Middleware
30+
```
31+
32+
## Config
33+
34+
| Property | Type | Description | Default |
35+
|:--------------|:--------------------------|:-----------------------------------------|:--------------------------------------------------------------------|
36+
| ModelFilePath | `string` | Model file path | `"./model.conf"` |
37+
| PolicyAdapter | `persist.Adapter` | Database adapter for policies | `./policy.csv` |
38+
| Enforcer | `*casbin.Enforcer` | Custom casbin enforcer | `Middleware generated enforcer using ModelFilePath & PolicyAdapter` |
39+
| Lookup | `func(*fiber.Ctx) string` | Look up for current subject | `""` |
40+
| Unauthorized | `func(*fiber.Ctx) error` | Response body for unauthorized responses | `Unauthorized` |
41+
| Forbidden | `func(*fiber.Ctx) error` | Response body for forbidden responses | `Forbidden` |
42+
43+
### Examples
44+
- [Gorm Adapter](https://github.com/svcg/-fiber_casbin_demo)
45+
- [File Adapter](https://github.com/gofiber/contrib/casbin/tree/master/example)
46+
47+
## CustomPermission
48+
49+
```go
50+
package main
51+
52+
import (
53+
"github.com/gofiber/fiber/v2"
54+
"github.com/gofiber/contrib/casbin"
55+
_ "github.com/go-sql-driver/mysql"
56+
"github.com/casbin/xorm-adapter/v2"
57+
)
58+
59+
func main() {
60+
app := fiber.New()
61+
62+
authz := casbin.New(casbin.Config{
63+
ModelFilePath: "path/to/rbac_model.conf",
64+
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
65+
Lookup: func(c *fiber.Ctx) string {
66+
// fetch authenticated user subject
67+
},
68+
})
69+
70+
app.Post("/blog",
71+
authz.RequiresPermissions([]string{"blog:create"}, casbin.WithValidationRule(casbin.MatchAllRule)),
72+
func(c *fiber.Ctx) error {
73+
// your handler
74+
},
75+
)
76+
77+
app.Delete("/blog/:id",
78+
authz.RequiresPermissions([]string{"blog:create", "blog:delete"}, casbin.WithValidationRule(casbin.AtLeastOneRule)),
79+
func(c *fiber.Ctx) error {
80+
// your handler
81+
},
82+
)
83+
84+
app.Listen(":8080")
85+
}
86+
```
87+
88+
## RoutePermission
89+
90+
```go
91+
package main
92+
93+
import (
94+
"github.com/gofiber/fiber/v2"
95+
"github.com/gofiber/contrib/casbin"
96+
_ "github.com/go-sql-driver/mysql"
97+
"github.com/casbin/xorm-adapter/v2"
98+
)
99+
100+
func main() {
101+
app := fiber.New()
102+
103+
authz := casbin.New(casbin.Config{
104+
ModelFilePath: "path/to/rbac_model.conf",
105+
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
106+
Lookup: func(c *fiber.Ctx) string {
107+
// fetch authenticated user subject
108+
},
109+
})
110+
111+
// check permission with Method and Path
112+
app.Post("/blog",
113+
authz.RoutePermission(),
114+
func(c *fiber.Ctx) error {
115+
// your handler
116+
},
117+
)
118+
119+
app.Listen(":8080")
120+
}
121+
```
122+
123+
## RoleAuthorization
124+
125+
```go
126+
package main
127+
128+
import (
129+
"github.com/gofiber/fiber/v2"
130+
"github.com/gofiber/contrib/casbin"
131+
_ "github.com/go-sql-driver/mysql"
132+
"github.com/casbin/xorm-adapter/v2"
133+
)
134+
135+
func main() {
136+
app := fiber.New()
137+
138+
authz := casbin.New(casbin.Config{
139+
ModelFilePath: "path/to/rbac_model.conf",
140+
PolicyAdapter: xormadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/"),
141+
Lookup: func(c *fiber.Ctx) string {
142+
// fetch authenticated user subject
143+
},
144+
})
145+
146+
app.Put("/blog/:id",
147+
authz.RequiresRoles([]string{"admin"}),
148+
func(c *fiber.Ctx) error {
149+
// your handler
150+
},
151+
)
152+
153+
app.Listen(":8080")
154+
}
155+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: fgprof
3+
---
4+
5+
# Fgprof
6+
7+
![Release](https://img.shields.io/github/v/tag/gofiber/contrib?filter=fgprof*)
8+
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord)
9+
![Test](https://github.com/gofiber/contrib/workflows/Tests/badge.svg)
10+
![Security](https://github.com/gofiber/contrib/workflows/Security/badge.svg)
11+
![Linter](https://github.com/gofiber/contrib/workflows/Linter/badge.svg)
12+
13+
[fgprof](https://github.com/felixge/fgprof) support for Fiber.
14+
15+
**Note: Requires Go 1.19 and above**
16+
17+
## Install
18+
19+
This middleware supports Fiber v2.
20+
21+
Using fgprof to profiling your Fiber app.
22+
23+
```
24+
go get -u github.com/gofiber/fiber/v2
25+
go get -u github.com/gofiber/contrib/fgprof
26+
```
27+
28+
## Config
29+
30+
| Property | Type | Description | Default |
31+
|----------|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|---------|
32+
| Next | `func(c *fiber.Ctx) bool` | A function to skip this middleware when returned `true`. | `nil` |
33+
| Prefix | `string`. | Prefix defines a URL prefix added before "/debug/fgprof". Note that it should start with (but not end with) a slash. Example: "/federated-fiber" | `""` |
34+
35+
## Example
36+
37+
```go
38+
package main
39+
40+
import (
41+
"log"
42+
43+
"github.com/gofiber/contrib/fgprof"
44+
"github.com/gofiber/fiber/v2"
45+
)
46+
47+
func main() {
48+
app := fiber.New()
49+
app.Use(fgprof.New())
50+
app.Get("/", func(c *fiber.Ctx) error {
51+
return c.SendString("OK")
52+
})
53+
log.Fatal(app.Listen(":3000"))
54+
}
55+
```
56+
57+
```bash
58+
go tool pprof -http=:8080 http://localhost:3000/debug/fgprof
59+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
id: fiberi18n
3+
---
4+
5+
# Fiberi18n
6+
7+
![Release](https://img.shields.io/github/v/tag/gofiber/contrib?filter=fiberi18n*)
8+
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord)
9+
![Test](https://github.com/gofiber/contrib/workflows/Tests/badge.svg)
10+
![Security](https://github.com/gofiber/contrib/workflows/Security/badge.svg)
11+
![Linter](https://github.com/gofiber/contrib/workflows/Linter/badge.svg)
12+
13+
[go-i18n](https://github.com/nicksnyder/go-i18n) support for Fiber.
14+
15+
**Note: Requires Go 1.18 and above**
16+
17+
## Install
18+
19+
This middleware supports Fiber v2.
20+
21+
```
22+
go get -u github.com/gofiber/fiber/v2
23+
go get -u github.com/gofiber/contrib/fiberi18n/v2
24+
```
25+
26+
## Signature
27+
28+
| Name | Signature | Description |
29+
|--------------|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
30+
| New | `New(config ...*fiberi18n.Config) fiber.Handler` | Create a new fiberi18n middleware handler |
31+
| Localize | `Localize(ctx *fiber.Ctx, params interface{}) (string, error)` | Localize returns a localized message. param is one of these type: messageID, *i18n.LocalizeConfig |
32+
| MustLocalize | `MustLocalize(ctx *fiber.Ctx, params interface{}) string` | MustLocalize is similar to Localize, except it panics if an error happens. param is one of these type: messageID, *i18n.LocalizeConfig |
33+
34+
## Config
35+
36+
| Property | Type | Description | Default |
37+
|------------------|---------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------|
38+
| Next | `func(c *fiber.Ctx) bool` | A function to skip this middleware when returned `true`. | `nil` |
39+
| RootPath | `string` | The i18n template folder path. | `"./example/localize"` |
40+
| AcceptLanguages | `[]language.Tag` | A collection of languages that can be processed. | `[]language.Tag{language.Chinese, language.English}` |
41+
| FormatBundleFile | `string` | The type of the template file. | `"yaml"` |
42+
| DefaultLanguage | `language.Tag` | The default returned language type. | `language.English` |
43+
| Loader | `Loader` | The implementation of the Loader interface, which defines how to read the file. We provide both os.ReadFile and embed.FS.ReadFile. | `LoaderFunc(os.ReadFile)` |
44+
| UnmarshalFunc | `i18n.UnmarshalFunc` | The function used for decoding template files. | `yaml.Unmarshal` |
45+
| LangHandler | `func(ctx *fiber.Ctx, defaultLang string) string` | Used to get the kind of language handled by *fiber.Ctx and defaultLang. | Retrieved from the request header `Accept-Language` or query parameter `lang`. |
46+
47+
## Example
48+
49+
```go
50+
package main
51+
52+
import (
53+
"log"
54+
55+
"github.com/gofiber/contrib/fiberi18n/v2"
56+
"github.com/gofiber/fiber/v2"
57+
"github.com/nicksnyder/go-i18n/v2/i18n"
58+
"golang.org/x/text/language"
59+
)
60+
61+
func main() {
62+
app := fiber.New()
63+
app.Use(
64+
fiberi18n.New(&fiberi18n.Config{
65+
RootPath: "./example/localize",
66+
AcceptLanguages: []language.Tag{language.Chinese, language.English},
67+
DefaultLanguage: language.Chinese,
68+
}),
69+
)
70+
app.Get("/", func(c *fiber.Ctx) error {
71+
localize, err := fiberi18n.Localize(c, "welcome")
72+
if err != nil {
73+
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
74+
}
75+
return c.SendString(localize)
76+
})
77+
app.Get("/:name", func(ctx *fiber.Ctx) error {
78+
return ctx.SendString(fiberi18n.MustLocalize(ctx, &i18n.LocalizeConfig{
79+
MessageID: "welcomeWithName",
80+
TemplateData: map[string]string{
81+
"name": ctx.Params("name"),
82+
},
83+
}))
84+
})
85+
log.Fatal(app.Listen(":3000"))
86+
}
87+
```
88+

0 commit comments

Comments
 (0)