Skip to content

Commit

Permalink
Add docs from gofiber/fiber@726c499
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Apr 17, 2024
1 parent 30a69c2 commit 3d7b270
Show file tree
Hide file tree
Showing 9 changed files with 551 additions and 377 deletions.
233 changes: 145 additions & 88 deletions docs/core/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ description: The app instance conventionally denotes the Fiber application.
sidebar_position: 2
---

## Routing

import RoutingHandler from './../partials/routing/handler.md';

### Static
## Static

Use the **Static** method to serve static files such as **images**, **CSS,** and **JavaScript**.

Expand Down Expand Up @@ -111,11 +109,11 @@ app.Static("/", "./public", fiber.Static{
})
```
### Route Handlers
## Route Handlers
<RoutingHandler />
### Mount
## Mount
You can Mount Fiber instance by creating a `*Mount`
Expand All @@ -137,7 +135,7 @@ func main() {
}
```
### MountPath
## MountPath
The `MountPath` property contains one or more path patterns on which a sub-app was mounted.
Expand Down Expand Up @@ -167,7 +165,7 @@ func main() {
Mounting order is important for MountPath. If you want to get mount paths properly, you should start mounting from the deepest app.
:::
### Group
## Group
You can group routes by creating a `*Group` struct.
Expand All @@ -193,7 +191,7 @@ func main() {
}
```
### Route
## Route
You can define routes with a common prefix inside the common function.
Expand All @@ -214,15 +212,47 @@ func main() {
}
```
### HandlersCount
## Server
Server returns the underlying [fasthttp server](https://godoc.org/github.com/valyala/fasthttp#Server)
```go title="Signature"
func (app *App) Server() *fasthttp.Server
```
```go title="Examples"
func main() {
app := fiber.New()
app.Server().MaxConnsPerIP = 1
// ...
}
```
## Server Shutdown
Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners and then waits indefinitely for all connections to return to idle before shutting down.
ShutdownWithTimeout will forcefully close any active connections after the timeout expires.
ShutdownWithContext shuts down the server including by force if the context's deadline is exceeded.
```go
func (app *App) Shutdown() error
func (app *App) ShutdownWithTimeout(timeout time.Duration) error
func (app *App) ShutdownWithContext(ctx context.Context) error
```
## HandlersCount
This method returns the amount of registered handlers.
```go title="Signature"
func (app *App) HandlersCount() uint32
```
### Stack
## Stack
This method returns the original router stack
Expand Down Expand Up @@ -276,7 +306,7 @@ func main() {
]
```
### Name
## Name
This method assigns the name of latest created route.
Expand Down Expand Up @@ -378,7 +408,7 @@ func main() {
]
```
### GetRoute
## GetRoute
This method gets the route by name.
Expand All @@ -399,6 +429,7 @@ func main() {
app.Listen(":3000")
}
```
Expand All @@ -411,7 +442,7 @@ func main() {
}
```
### GetRoutes
## GetRoutes
This method gets all routes.
Expand Down Expand Up @@ -458,118 +489,144 @@ Handler returns the server handler that can be used to serve custom \*fasthttp.R
func (app *App) Handler() fasthttp.RequestHandler
```
## ErrorHandler
## Listen
Errorhandler executes the process which was defined for the application in case of errors, this is used in some cases in middlewares.
Listen serves HTTP requests from the given address.
```go title="Signature"
func (app *App) ErrorHandler(ctx Ctx, err error) error
func (app *App) Listen(addr string) error
```
```go title="Examples"
// Listen on port :8080
app.Listen(":8080")
// Custom host
app.Listen("127.0.0.1:8080")
```
## NewCtxFunc
## ListenTLS
NewCtxFunc allows to customize the ctx struct as we want.
ListenTLS serves HTTPs requests from the given address using certFile and keyFile paths to as TLS certificate and key file.
```go title="Signature"
func (app *App) NewCtxFunc(function func(app *App) CustomCtx)
func (app *App) ListenTLS(addr, certFile, keyFile string) error
```
```go title="Examples"
type CustomCtx struct {
DefaultCtx
}
app.ListenTLS(":443", "./cert.pem", "./cert.key");
```
Using `ListenTLS` defaults to the following config \( use `Listener` to provide your own config \)
// Custom method
func (c *CustomCtx) Params(key string, defaultValue ...string) string {
return "prefix_" + c.DefaultCtx.Params(key)
```go title="Default \*tls.Config"
&tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{
cert,
},
}
```
app := New()
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx {
return &CustomCtx{
DefaultCtx: *NewDefaultCtx(app),
}
})
// curl http://localhost:3000/123
app.Get("/:id", func(c Ctx) error {
// use custom method - output: prefix_123
return c.SendString(c.Params("id"))
})
## ListenTLSWithCertificate
```go title="Signature"
func (app *App) ListenTLS(addr string, cert tls.Certificate) error
```
## RegisterCustomBinder
```go title="Examples"
app.ListenTLSWithCertificate(":443", cert);
```
You can register custom binders to use as Bind().Custom("name").
They should be compatible with CustomBinder interface.
Using `ListenTLSWithCertificate` defaults to the following config \( use `Listener` to provide your own config \)
```go title="Default \*tls.Config"
&tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{
cert,
},
}
```
## ListenMutualTLS
ListenMutualTLS serves HTTPs requests from the given address using certFile, keyFile and clientCertFile are the paths to TLS certificate and key file
```go title="Signature"
func (app *App) RegisterCustomBinder(binder CustomBinder)
func (app *App) ListenMutualTLS(addr, certFile, keyFile, clientCertFile string) error
```
```go title="Examples"
app := fiber.New()
app.ListenMutualTLS(":443", "./cert.pem", "./cert.key", "./ca-chain-cert.pem");
```
// My custom binder
customBinder := &customBinder{}
// Name of custom binder, which will be used as Bind().Custom("name")
func (*customBinder) Name() string {
return "custom"
}
// Is used in the Body Bind method to check if the binder should be used for custom mime types
func (*customBinder) MIMETypes() []string {
return []string{"application/yaml"}
Using `ListenMutualTLS` defaults to the following config \( use `Listener` to provide your own config \)
```go title="Default \*tls.Config"
&tls.Config{
MinVersion: tls.VersionTLS12,
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: clientCertPool,
Certificates: []tls.Certificate{
cert,
},
}
// Parse the body and bind it to the out interface
func (*customBinder) Parse(c Ctx, out any) error {
// parse yaml body
return yaml.Unmarshal(c.Body(), out)
```
## ListenMutualTLSWithCertificate
ListenMutualTLSWithCertificate serves HTTPs requests from the given address using certFile, keyFile and clientCertFile are the paths to TLS certificate and key file
```go title="Signature"
func (app *App) ListenMutualTLSWithCertificate(addr string, cert tls.Certificate, clientCertPool *x509.CertPool) error
```
```go title="Examples"
app.ListenMutualTLSWithCertificate(":443", cert, clientCertPool);
```
Using `ListenMutualTLSWithCertificate` defaults to the following config \( use `Listener` to provide your own config \)
```go title="Default \*tls.Config"
&tls.Config{
MinVersion: tls.VersionTLS12,
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: clientCertPool,
Certificates: []tls.Certificate{
cert,
},
}
// Register custom binder
app.RegisterCustomBinder(customBinder)
// curl -X POST http://localhost:3000/custom -H "Content-Type: application/yaml" -d "name: John"
app.Post("/custom", func(c Ctx) error {
var user User
// output: {Name:John}
// Custom binder is used by the name
if err := c.Bind().Custom("custom", &user); err != nil {
return err
}
// ...
return c.JSON(user)
})
// curl -X POST http://localhost:3000/normal -H "Content-Type: application/yaml" -d "name: Doe"
app.Post("/normal", func(c Ctx) error {
var user User
// output: {Name:Doe}
// Custom binder is used by the mime type
if err := c.Bind().Body(&user); err != nil {
return err
}
// ...
return c.JSON(user)
})
```
## RegisterCustomConstraint
## Listener
RegisterCustomConstraint allows to register custom constraint.
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.
```go title="Signature"
func (app *App) RegisterCustomConstraint(constraint CustomConstraint)
func (app *App) Listener(ln net.Listener) error
```
See [Custom Constraint](../guide/routing.md#custom-constraint) section for more information.
```go title="Examples"
ln, _ := net.Listen("tcp", ":3000")
cer, _:= tls.LoadX509KeyPair("server.crt", "server.key")
## SetTLSHandler
ln = tls.NewListener(ln, &tls.Config{Certificates: []tls.Certificate{cer}})
Use SetTLSHandler to set [ClientHelloInfo](https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.2) when using TLS with Listener.
app.Listener(ln)
```
## RegisterCustomConstraint
RegisterCustomConstraint allows to register custom constraint.
```go title="Signature"
func (app *App) SetTLSHandler(tlsHandler *TLSHandler)
func (app *App) RegisterCustomConstraint(constraint CustomConstraint)
```
See [Custom Constraint](../guide/routing.md#custom-constraint) section for more information.
## Test
Testing your application is done with the **Test** method. Use this method for creating `_test.go` files or when you need to debug your routing logic. The default timeout is `1s` if you want to disable a timeout altogether, pass `-1` as a second argument.
Expand Down Expand Up @@ -603,8 +660,8 @@ if resp.StatusCode == fiber.StatusOK {

## Hooks

Hooks is a method to return [hooks](./hooks.md) property.
Hooks is a method to return [hooks](../guide/hooks.md) property.

```go title="Signature"
func (app *App) Hooks() *Hooks
```
```
4 changes: 2 additions & 2 deletions 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: 8
sidebar_position: 4
---

HTTP methods were copied from net/http.
Expand Down Expand Up @@ -288,4 +288,4 @@ const (
HeaderXRobotsTag = "X-Robots-Tag"
HeaderXUACompatible = "X-UA-Compatible"
)
```
```
Loading

0 comments on commit 3d7b270

Please sign in to comment.