Skip to content

Commit

Permalink
Merge pull request #435 from gofiber/recipes_docs
Browse files Browse the repository at this point in the history
docusaurus preparations for the recipes
  • Loading branch information
ReneWerner87 authored Nov 26, 2024
2 parents 8c6a7b2 + 1a8705c commit e04fbcc
Show file tree
Hide file tree
Showing 101 changed files with 7,113 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: npm

- name: Install dependencies
Expand Down
6 changes: 3 additions & 3 deletions sidebars.js → default_sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// @ts-check

/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
const sidebars = {
tutorialSidebar: [
const default_sidebars = {
left_sidebar: [
{type: 'autogenerated', dirName: '.'}
],
};

module.exports = sidebars;
module.exports = default_sidebars;
80 changes: 80 additions & 0 deletions docs/recipes/404-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: 404 Handler
keywords: [404, not found, handler, errorhandler, custom]
---

# Custom 404 Not Found Handler Example

[![Github](https://img.shields.io/static/v1?label=&message=Github&color=2ea44f&style=for-the-badge&logo=github)](https://github.com/gofiber/recipes/tree/master/404-handler) [![StackBlitz](https://img.shields.io/static/v1?label=&message=StackBlitz&color=2ea44f&style=for-the-badge&logo=StackBlitz)](https://stackblitz.com/github/gofiber/recipes/tree/master/404-handler)

This example demonstrates how to implement a custom 404 Not Found handler using the [Fiber](https://gofiber.io) web framework in Go. The purpose of this example is to show how to handle requests to undefined routes gracefully by returning a 404 status code.

## Description

In web applications, it's common to encounter requests to routes that do not exist. Handling these requests properly is important to provide a good user experience and to inform the user that the requested resource is not available. This example sets up a simple Fiber application with a custom 404 handler to manage such cases.

## Requirements

- [Go](https://golang.org/dl/) 1.18 or higher
- [Git](https://git-scm.com/downloads)

## Running the Example

To run the example, use the following command:
```bash
go run main.go
```

The server will start and listen on `localhost:3000`.

## Example Routes

- **GET /hello**: Returns a simple greeting message.
- **Undefined Routes**: Any request to a route not defined will trigger the custom 404 handler.

## Custom 404 Handler

The custom 404 handler is defined to catch all undefined routes and return a 404 status code with a "Not Found" message.

## Code Overview

### `main.go`

```go
package main

import (
"log"
"github.com/gofiber/fiber/v2"
)

func main() {
// Fiber instance
app := fiber.New()

// Routes
app.Get("/hello", hello)

// 404 Handler
app.Use(func(c *fiber.Ctx) error {
return c.SendStatus(404) // => 404 "Not Found"
})

// Start server
log.Fatal(app.Listen(":3000"))
}

// Handler
func hello(c *fiber.Ctx) error {
return c.SendString("I made a ☕ for you!")
}
```

## Conclusion

This example provides a basic setup for handling 404 Not Found errors in a Fiber application. It can be extended and customized further to fit the needs of more complex applications.

## References

- [Fiber Documentation](https://docs.gofiber.io)
- [GitHub Repository](https://github.com/gofiber/fiber)
100 changes: 100 additions & 0 deletions docs/recipes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
id: welcome
title: 👋 Overview
sidebar_position: 1
---

# 🍳 Examples for [Fiber](https://github.com/gofiber/fiber)

**Welcome to the official Fiber cookbook**!

Here you can find the most **delicious** recipes to cook delicious meals using our web framework.

## 🌽 Table of contents

<!-- AUTO-GENERATED-CONTENT:START -->
- [404 Handler](./404-handler/README.md)
- [Air Live Reloading](./air/README.md)
- [Auth + Docker + Postgres + JWT](./auth-docker-postgres-jwt/README.md)
- [Auth + JWT](./auth-jwt/README.md)
- [Autocert](./autocert/README.md)
- [AWS Elastic Beanstalk](./aws-eb/README.md)
- [AWS SAM](./aws-sam/README.md)
- [AWS SAM Container](./aws-sam-container/README.md)
- [Bootstrap](./bootstrap/README.md)
- [Clean Architecture](./clean-architecture/README.md)
- [Cloud Run](./cloud-run/README.md)
- [Colly Gorm](./colly-gorm/README.md)
- [CSRF](./csrf/README.md)
- [CSRF + Session](./csrf-with-session/README.md)
- [Docker + MariaDB](./docker-mariadb-clean-arch/README.md)
- [Docker + Nginx](./docker-nginx-loadbalancer/README.md)
- [Dummy JSON Proxy](./dummyjson/README.md)
- [Entgo ORM (MySQL)](./ent-mysql/README.md)
- [Entgo Sveltekit](./entgo-sveltekit/README.md)
- [Envoy External Authorization](./envoy-extauthz/README.md)
- [File Server](./file-server/README.md)
- [Firebase Authentication](./firebase-auth/README.md)
- [Firebase Functions](./firebase-functions/README.md)
- [Firebase GCloud](./gcloud/README.md)
- [Google Cloud Firebase](./gcloud-firebase/README.md)
- [GeoIP](./geoip/README.md)
- [GeoIP + MaxMind](./geoip-maxmind/README.md)
- [GORM](./gorm/README.md)
- [GORM MySQL](./gorm-mysql/README.md)
- [GORM + PostgreSQL](./gorm-postgres/README.md)
- [Graceful shutdown](./graceful-shutdown/README.md)
- [GraphQL](./graphql/README.md)
- [gRPC](./grpc/README.md)
- [Hello World](./hello-world/README.md)
- [Heroku](./heroku/README.md)
- [Hexagonal Architecture](./hexagonal/README.md)
- [HTTPS with PKCS12 TLS](./https-pkcs12-tls/README.md)
- [HTTPS with TLS](./https-tls/README.md)
- [I18n](./i18n/README.md)
- [JWT](./jwt/README.md)
- [Kubernetes](./k8s/README.md)
- [Memgraph](./memgraph/README.md)
- [MinIO](./minio/README.md)
- [MongoDB Example](./mongodb/README.md)
- [Multiple Ports](./multiple-ports/README.md)
- [MySQL](./mysql/README.md)
- [Neo4j](./neo4j/README.md)
- [OAuth2](./oauth2/README.md)
- [Google OAuth2](./oauth2-google/README.md)
- [Optional Parameter Example](./optional-parameter/README.md)
- [Parsley](./parsley/README.md)
- [PostgreSQL](./postgresql/README.md)
- [Prefork Example](./prefork/README.md)
- [RabbitMQ](./rabbitmq/README.md)
- [React](./react-router/README.md)
- [Recover Middleware Example](./recover/README.md)
- [RSS Feed](./rss-feed/README.md)
- [Server Timing](./server-timing/README.md)
- [Sessions + SQLite3](./sessions-sqlite3/README.md)
- [Socketio](./socketio/README.md)
- [Single Page Application (SPA)](./spa/README.md)
- [Sqlboiler](./sqlboiler/README.md)
- [Sqlc](./sqlc/README.md)
- [Server-Sent Events](./sse/README.md)
- [Stream Request Body](./stream-request-body/README.md)
- [Svelte Netlify](./svelte-netlify/README.md)
- [Sveltekit Embed](./sveltekit-embed/README.md)
- [Swagger](./swagger/README.md)
- [Tableflip Example](./tableflip/README.md)
- [Template](./template/README.md)
- [Template Asset Bundling](./template-asset-bundling/README.md)
- [Todo App + Auth + GORM](./todo-app-with-auth-gorm/README.md)
- [Unit Testing](./unit-test/README.md)
- [File Upload](./upload-file/README.md)
- [URL Shortener](./url-shortener-api/README.md)
- [Validation](./validation/README.md)
- [Vercel](./vercel/README.md)
- [WebSocket](./websocket/README.md)
- [WebSocket Chat](./websocket-chat/README.md)
<!-- AUTO-GENERATED-CONTENT:END -->

## 👩‍🍳 Have a delicious recipe?

If you have found an amazing recipe for **Fiber** — share it with others!
We are ready to accept your [PR](https://github.com/gofiber/recipes/pulls) and add your recipe to the cookbook (both on [website](https://docs.gofiber.io) and this repository).
98 changes: 98 additions & 0 deletions docs/recipes/air/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: Air Live Reloading
keywords: [air, live reloading, development, air tool, hot reload, watch, changes]
---

# Live Reloading with Air Example

[![Github](https://img.shields.io/static/v1?label=&message=Github&color=2ea44f&style=for-the-badge&logo=github)](https://github.com/gofiber/recipes/tree/master/air) [![StackBlitz](https://img.shields.io/static/v1?label=&message=StackBlitz&color=2ea44f&style=for-the-badge&logo=StackBlitz)](https://stackblitz.com/github/gofiber/recipes/tree/master/air)

This example demonstrates how to set up live reloading for a Go application using the [Air](https://github.com/cosmtrek/air) tool. The purpose of this example is to show how to automatically reload your application during development whenever you make changes to the source code.

## Description

Live reloading is a useful feature during development as it saves time by automatically restarting the application whenever changes are detected. This example sets up a simple Fiber application and configures Air to watch for changes and reload the application.

## Requirements

- [Go](https://golang.org/dl/) 1.18 or higher
- [Git](https://git-scm.com/downloads)
- [Air](https://github.com/cosmtrek/air)

## Setup

1. Clone the repository:
```bash
git clone https://github.com/gofiber/recipes.git
cd recipes/air
```

2. Install the dependencies:
```bash
go mod download
```

3. Install Air:
```bash
go install github.com/cosmtrek/air@latest
```

## Configuration

Air is configured using the `air/.air.conf` file. This file specifies the build command, binary name, and directories to watch for changes. The configuration files for different operating systems are provided:

- `air/.air.windows.conf` for Windows
- `air/.air.linux.conf` for Linux

## Running the Example

To run the example with live reloading, use the following command:
```bash
air -c .air.linux.conf
```
or for Windows:
```bash
air -c .air.windows.conf
```

The server will start and listen on `localhost:3000`. Any changes to the source code will automatically trigger a rebuild and restart of the application.

## Example Routes

- **GET /**: Returns a simple greeting message.

## Code Overview

### `main.go`

```go
package main
import (
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
// Create new Fiber instance
app := fiber.New()
// Create new GET route on path "/"
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
// Start server on http://localhost:3000
log.Fatal(app.Listen(":3000"))
}
```

## Conclusion

This example provides a basic setup for live reloading a Go application using Air. It can be extended and customized further to fit the needs of more complex applications.

## References

- [Air Documentation](https://github.com/cosmtrek/air)
- [Fiber Documentation](https://docs.gofiber.io)
- [GitHub Repository](https://github.com/gofiber/fiber)
94 changes: 94 additions & 0 deletions docs/recipes/auth-docker-postgres-jwt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: Auth + Docker + Postgres + JWT
keywords: [auth, docker, postgres, jwt]
---

# Auth Docker Postgres JWT Example

[![Github](https://img.shields.io/static/v1?label=&message=Github&color=2ea44f&style=for-the-badge&logo=github)](https://github.com/gofiber/recipes/tree/master/auth-docker-postgres-jwt) [![StackBlitz](https://img.shields.io/static/v1?label=&message=StackBlitz&color=2ea44f&style=for-the-badge&logo=StackBlitz)](https://stackblitz.com/github/gofiber/recipes/tree/master/auth-docker-postgres-jwt)

This example demonstrates a boilerplate setup for a Go Fiber application that uses Docker, PostgreSQL, and JWT for authentication.

## Description

This project provides a starting point for building a web application with user authentication using JWT. It leverages Docker for containerization and PostgreSQL as the database.

## Requirements

- [Docker](https://www.docker.com/get-started)
- [Docker Compose](https://docs.docker.com/compose/install/)
- [Go](https://golang.org/dl/) 1.18 or higher

## Setup

1. Clone the repository:
```bash
git clone https://github.com/gofiber/recipes.git
cd recipes/auth-docker-postgres-jwt
```

2. Set the environment variables in a `.env` file:
```env
DB_PORT=5432
DB_USER=example_user
DB_PASSWORD=example_password
DB_NAME=example_db
SECRET=example_secret
```

3. Build and start the Docker containers:
```bash
docker-compose build
docker-compose up
```

The API and the database should now be running.

## Database Management

You can manage the database via `psql` with the following command:
```bash
docker-compose exec db psql -U <DB_USER>
```

Replace `<DB_USER>` with the value from your `.env` file.

## API Endpoints

The following endpoints are available in the API:

- **POST /api/auth/register**: Register a new user.
- **POST /api/auth/login**: Authenticate a user and return a JWT.
- **GET /api/user/:id**: Get a user (requires a valid JWT).
- **PATCH /api/user/:id**: Update a user (requires a valid JWT).
- **DELETE /api/user/:id**: Delete a user (requires a valid JWT).

## Example Usage

1. Register a new user:
```bash
curl -X POST http://localhost:3000/api/auth/register -d '{"username":"testuser", "password":"testpassword"}' -H "Content-Type: application/json"
```

2. Login to get a JWT:
```bash
curl -X POST http://localhost:3000/api/auth/login -d '{"username":"testuser", "password":"testpassword"}' -H "Content-Type: application/json"
```

3. Access a protected route:
```bash
curl -H "Authorization: Bearer <JWT>" http://localhost:3000/api/user/1
```

Replace `<JWT>` with the token received from the login endpoint.

## Conclusion

This example provides a basic setup for a Go Fiber application with Docker, PostgreSQL, and JWT authentication. It can be extended and customized further to fit the needs of more complex applications.

## References

- [Fiber Documentation](https://docs.gofiber.io)
- [Docker Documentation](https://docs.docker.com)
- [PostgreSQL Documentation](https://www.postgresql.org/docs/)
- [JWT Documentation](https://jwt.io/introduction/)
Loading

0 comments on commit e04fbcc

Please sign in to comment.