diff --git a/documentation/docs/index.md b/documentation/docs/index.md index e81fa8c9..2f895994 100644 --- a/documentation/docs/index.md +++ b/documentation/docs/index.md @@ -10,7 +10,7 @@ sidebar_position: 1 Let's discover **Fuego in less than 5 minutes**. -## Getting Started +## Quick peek without installing Try our [Hello World](./tutorials/01-hello-world.md)! @@ -18,9 +18,39 @@ Try our [Hello World](./tutorials/01-hello-world.md)! go run github.com/go-fuego/fuego/examples/hello-world@latest ``` -Or **try Fuego immediately** by cloning **[one of our examples](https://github.com/go-fuego/fuego/tree/main/examples)**. +This runs the code for a simple hello world server. Look at all it generates from a simple code! You'll get a URL to see the result in your browser. + +```go showLineNumbers +package main + +import ( + "github.com/go-fuego/fuego" +) + +func main() { + s := fuego.NewServer() + + fuego.Get(s, "/", helloWorld) + + s.Run() +} + +func helloWorld(c fuego.ContextNoBody) (string, error) { + return "Hello, World!", nil +} +``` + +## Try example from real Fuego source code in 3 sec + +Try Fuego immediately by cloning [the repo](https://github.com/go-fuego/fuego) and running one of our examples. + +```bash +git clone git@github.com:go-fuego/fuego.git +cd fuego/examples/petstore +go run . +``` ### What you'll need - [Golang v1.22](https://golang.org/doc/go1.22) or above -_(Fuego relies on a new feature of the net/http package only available after 1.22)_. + _(Fuego relies on a new feature of the net/http package only available after 1.22)_. diff --git a/documentation/docs/tutorials/02-crud.md b/documentation/docs/tutorials/02-crud.md index 755e8771..b1c1a8ee 100644 --- a/documentation/docs/tutorials/02-crud.md +++ b/documentation/docs/tutorials/02-crud.md @@ -14,10 +14,13 @@ fuego controller books go run github.com/go-fuego/fuego/cmd/fuego@latest controller books ``` -After executing the above code, -you need to slightly modify the generated controllers/books.go and main.go files. +This generates a controller and a service for the `books` resource. -```go title="controllers/books.go" {8-9,28-39} +You then have to implement the service interface in the controller to be able to play with data. It's a form of **dependency injection** that we chose to use for the code generator of Fuego, but you can implement it in any way you want. + +To implement the service, you need to slightly modify the generated controllers/books.go and main.go files. + +```go title="controllers/books.go" {8-9,28-39} showLineNumbers package controller import ( @@ -25,7 +28,7 @@ import ( ) type BooksResources struct { - // CHANGE NEXT LINE (BooksService -> RealBooksService) + // Use a concrete struct that implements the service (BooksService -> RealBooksService) BooksService RealBooksService } @@ -45,9 +48,9 @@ type BooksService interface { } -// ADD THIS CODE BLOCK +// Implement the BooksService interface type RealBooksService struct { - BooksService + BooksService // Embed the interface to satisfy it -this pattern is just there to make the code compile but you should implement all methods } func (s RealBooksService) GetBooks(id string) (Books, error) { @@ -62,7 +65,9 @@ func (s RealBooksService) GetBooks(id string) (Books, error) { // END OF CODE BLOCK ``` -```go title="main.go" {4-5,13-14} +Then we'll inject this controller into the server. + +```go title="main.go" {6-7,14-21} package main import ( @@ -76,8 +81,15 @@ func main() { s := fuego.NewServer() // .... - // ADD NEXT LINE - controllers.BooksResources{}.Routes(s) + // Declare the ressource + booksResources := controllers.BooksResources{ + BooksService: controllers.RealBooksService{}, + // Other services & dependencies, like a DB etc. + } + + // Plug the controllers into the server + booksResources.Routes(s) + s.Run() } ```