Skip to content

Commit

Permalink
docusaurus preparations for the recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Nov 25, 2024
1 parent b938f08 commit d7e8036
Show file tree
Hide file tree
Showing 77 changed files with 4,159 additions and 74 deletions.
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;
73 changes: 73 additions & 0 deletions docs/recipes/404-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Custom 404 Not Found Handler Example

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)
85 changes: 85 additions & 0 deletions docs/recipes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
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

- [Amazon Web Services (AWS) Elastic Beanstalk](./aws-eb)
- [AWS SAM](./aws-sam)
- [Certificates from Let's Encrypt](./autocert)
- [Clean Architecture](./clean-architecture)
- [Cloud Run](./cloud-run)
- [Colly Scraping using Fiber and PostgreSQL](./fiber-colly-gorm)
- [CSRF-with-Session](./csrf-with-session)
- [CSRF](./csrf)
- [Custom 404 Not Found](./404-handler)
- [Dependency Injection (with Parsley)](./parsley)
- [Docker MariaDB Clean Architecture](./docker-mariadb-clean-arch)
- [Docker Nginx Loadbalancer](./docker-nginx-loadbalancer)
- [Docker Postgres-JWT](./auth-docker-postgres-jwt)
- [DummyJson](./dummyjson/)
- [Enable HTTPS/TLS using PKCS12 store](./https-pkcs12-tls)
- [Enable HTTPS/TLS](./https-tls)
- [Enable Preforking](./prefork)
- [Ent Mysql Example](./ent-mysql)
- [Entgo Sveltekit](./entgo-sveltekit)
- [Firebase Functions](./firebase-functions)
- [GeoIP (with MaxMind databases)](./geoip-maxmind)
- [GeoIP](./geoip)
- [GORM Mysql Example](./gorm-mysql)
- [GORM](./gorm)
- [Graceful shutdown](./graceful-shutdown)
- [GraphQL](./graphql)
- [Hello, World!](./hello-world)
- [Heroku App](./heroku)
- [Hexagonal Architecture](./hexagonal)
- [i18n](./i18n)
- [JWT](./jwt)
- [Kubernetes](./k8s)
- [Listen on Multiple Ports](./multiple-ports)
- [Live Reloading (Air)](./air)
- [Memgraph](./memgraph)
- [MinIO](./minio)
- [MongoDB](./mongodb)
- [MVC Application Bootstrap](./fiber-bootstrap)
- [Netlify Functions](fiber-svelte-netlify)
- [OAuth2 Google](./oauth2-google)
- [PostgreSQL](./postgresql)
- [RabbitMQ](rabbitmq)
- [React Router](./react-router)
- [Recover from panic](./recover)
- [RSS feed](./rss-feed)
- [Serve Static Files](./file-server)
- [Server Timing](./server-timing)
- [Server-Sent Events](./sse)
- [Sessions-SQLite3](./sessions-sqlite3)
- [Single Page Application Example](./spa)
- [Socket.io](./socketio)
- [Sqlboiler](./sqlboiler)
- [Sqlc](./sqlc)
- [Streaming of the Request Body](./stream-request-body)
- [Sveltekit Embed](./sveltekit-embed)
- [Tableflip (Graceful updates)](./tableflip)
- [Template Asset Bundling](./template-asset-bundling)
- [Unit Test Example](./unit-test)
- [Upload Multiple Files](./upload-file/multiple)
- [Upload Single File](./upload-file/single)
- [URL shortener API](./url-shortener-api)
- [User Auth with JWT](./auth-jwt)
- [Validation](./validation)
- [Vercel](./vercel)
- [WebSocket Chat Example](./websocket-chat)
- [WebSockets](./websocket)

## 👩‍🍳 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).
91 changes: 91 additions & 0 deletions docs/recipes/air/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Live Reloading with Air Example

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)
87 changes: 87 additions & 0 deletions docs/recipes/auth-docker-postgres-jwt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Auth Docker Postgres JWT Example

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 d7e8036

Please sign in to comment.