Skip to content

Commit 4001c81

Browse files
committed
Added base documentation
1 parent dfc1b51 commit 4001c81

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

documentation/docs/tutorials/hello-world.md renamed to documentation/docs/tutorials/01-hello-world.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
22
title: Hello World
3+
position: 1
4+
order: 1
35
---
46

57
# Hello world

documentation/docs/tutorials/crud.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,111 @@
22

33
How to write simple CRUD operations.
44

5+
:::tip
6+
7+
Fuego comes with a **generator** that can generates CRUD routes and controllers for you!
8+
9+
:::
10+
511
## Generation
612

13+
```bash
14+
go install github.com/go-fuego/fuego/cmd/fuego@latest
15+
fuego controller books
16+
17+
# or in one line:
18+
go run github.com/go-fuego/fuego/cmd/fuego@latest controller books
19+
```
20+
721
## The routes
22+
23+
The generator will create the following routes:
24+
25+
- `GET /books`: list all books
26+
- `POST /books`: create a new book
27+
- `GET /books/:id`: get a book by id
28+
- `PUT /books/:id`: update a book by id
29+
- `PATCH /books/:id`: update a book by id
30+
- `DELETE /books/:id`: delete a book by id
31+
32+
If you want to create the routes manually, you can use the following code:
33+
34+
```go title="main.go"
35+
package main
36+
37+
import (
38+
"github.com/go-fuego/fuego"
39+
)
40+
41+
type Book struct {
42+
ID string `json:"id"`
43+
Title string `json:"title"`
44+
}
45+
46+
func main() {
47+
s := fuego.NewServer()
48+
49+
// List all books
50+
fuego.Get(s, "/books", getBooks)
51+
52+
// Create a new book
53+
fuego.Post(s, "/books", createBook)
54+
55+
// Get a book by id
56+
fuego.Get(s, "/books/:id", getBook)
57+
58+
// Update a book by id
59+
fuego.Put(s, "/books/:id", updateBook)
60+
61+
// Update a book by id
62+
fuego.Patch(s, "/books/:id", updateBook)
63+
64+
// Delete a book by id
65+
fuego.Delete(s, "/books/:id", deleteBook)
66+
67+
s.Run()
68+
}
69+
```
70+
71+
```go title="controllers/books.go"
72+
package controllers
73+
74+
import (
75+
"github.com/go-fuego/fuego"
76+
)
77+
78+
type Book struct {
79+
ID string `json:"id"`
80+
Title string `json:"title"`
81+
}
82+
83+
type BookToCreate struct {
84+
Title string `json:"title"`
85+
}
86+
87+
func getBooks(c fuego.ContextNoBody) ([]Book, error) {
88+
// Your code here
89+
return nil, nil
90+
}
91+
92+
func createBook(c fuego.ContextWithBody[BookToCreate]) (Book, error) {
93+
// Your code here
94+
return Book{}, nil
95+
}
96+
97+
func getBook(c fuego.ContextNoBody) (Book, error) {
98+
// Your code here
99+
return Book{}, nil
100+
}
101+
102+
func updateBook(c fuego.ContextWithBody[Book]) (Book, error) {
103+
// Your code here
104+
return Book{}, nil
105+
}
106+
107+
func deleteBook(c fuego.ContextNoBody) error {
108+
// Your code here
109+
return nil
110+
}
111+
112+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
# Hot reload
2+
3+
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.
4+
5+
To enable hot reload, you need to install the `air` command-line tool:
6+
7+
```sh
8+
go install github.com/cosmtrek/air@latest
9+
```
10+
11+
Then, create a `.air.toml` file in the root of your project with the following content:
12+
13+
```sh
14+
air init
15+
```
16+
17+
Finally, simply the following command to start the server with hot reload:
18+
19+
```sh
20+
air
21+
```

0 commit comments

Comments
 (0)