From 4001c81e4ca946e3b8079e13b1c5b0da0c67e1a5 Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Wed, 7 Feb 2024 11:10:18 +0100 Subject: [PATCH] Added base documentation --- .../{hello-world.md => 01-hello-world.md} | 2 + documentation/docs/tutorials/crud.md | 105 ++++++++++++++++++ documentation/docs/tutorials/hot-reload.md | 20 ++++ 3 files changed, 127 insertions(+) rename documentation/docs/tutorials/{hello-world.md => 01-hello-world.md} (93%) diff --git a/documentation/docs/tutorials/hello-world.md b/documentation/docs/tutorials/01-hello-world.md similarity index 93% rename from documentation/docs/tutorials/hello-world.md rename to documentation/docs/tutorials/01-hello-world.md index b253bc40..625ef7ef 100644 --- a/documentation/docs/tutorials/hello-world.md +++ b/documentation/docs/tutorials/01-hello-world.md @@ -1,5 +1,7 @@ --- title: Hello World +position: 1 +order: 1 --- # Hello world diff --git a/documentation/docs/tutorials/crud.md b/documentation/docs/tutorials/crud.md index fc2ae75b..4f8ac7ff 100644 --- a/documentation/docs/tutorials/crud.md +++ b/documentation/docs/tutorials/crud.md @@ -2,6 +2,111 @@ How to write simple CRUD operations. +:::tip + +Fuego comes with a **generator** that can generates CRUD routes and controllers for you! + +::: + ## Generation +```bash +go install github.com/go-fuego/fuego/cmd/fuego@latest +fuego controller books + +# or in one line: +go run github.com/go-fuego/fuego/cmd/fuego@latest controller books +``` + ## The routes + +The generator will create the following routes: + +- `GET /books`: list all books +- `POST /books`: create a new book +- `GET /books/:id`: get a book by id +- `PUT /books/:id`: update a book by id +- `PATCH /books/:id`: update a book by id +- `DELETE /books/:id`: delete a book by id + +If you want to create the routes manually, you can use the following code: + +```go title="main.go" +package main + +import ( + "github.com/go-fuego/fuego" +) + +type Book struct { + ID string `json:"id"` + Title string `json:"title"` +} + +func main() { + s := fuego.NewServer() + + // List all books + fuego.Get(s, "/books", getBooks) + + // Create a new book + fuego.Post(s, "/books", createBook) + + // Get a book by id + fuego.Get(s, "/books/:id", getBook) + + // Update a book by id + fuego.Put(s, "/books/:id", updateBook) + + // Update a book by id + fuego.Patch(s, "/books/:id", updateBook) + + // Delete a book by id + fuego.Delete(s, "/books/:id", deleteBook) + + s.Run() +} +``` + +```go title="controllers/books.go" +package controllers + +import ( + "github.com/go-fuego/fuego" +) + +type Book struct { + ID string `json:"id"` + Title string `json:"title"` +} + +type BookToCreate struct { + Title string `json:"title"` +} + +func getBooks(c fuego.ContextNoBody) ([]Book, error) { + // Your code here + return nil, nil +} + +func createBook(c fuego.ContextWithBody[BookToCreate]) (Book, error) { + // Your code here + return Book{}, nil +} + +func getBook(c fuego.ContextNoBody) (Book, error) { + // Your code here + return Book{}, nil +} + +func updateBook(c fuego.ContextWithBody[Book]) (Book, error) { + // Your code here + return Book{}, nil +} + +func deleteBook(c fuego.ContextNoBody) error { + // Your code here + return nil +} + +``` diff --git a/documentation/docs/tutorials/hot-reload.md b/documentation/docs/tutorials/hot-reload.md index da1a06b8..2a229d27 100644 --- a/documentation/docs/tutorials/hot-reload.md +++ b/documentation/docs/tutorials/hot-reload.md @@ -1 +1,21 @@ # Hot reload + +Hot reload is a feature that allows you to update your code and see the changes in real-time without restarting the server. This is very useful for development, as it allows you to see the changes you make to your code immediately. + +To enable hot reload, you need to install the `air` command-line tool: + +```sh +go install github.com/cosmtrek/air@latest +``` + +Then, create a `.air.toml` file in the root of your project with the following content: + +```sh +air init +``` + +Finally, simply the following command to start the server with hot reload: + +```sh +air +```