-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
454 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//coverage:ignore | ||
package controller | ||
|
||
import ( | ||
"github.com/go-fuego/fuego" | ||
) | ||
|
||
type PetsRessources struct { | ||
PetsService PetsService | ||
} | ||
|
||
type Pets struct { | ||
ID string `json:"id"` | ||
Name string `json:"name" example:"Napoleon"` | ||
Age int `json:"age" example:"18"` | ||
} | ||
|
||
type PetsCreate struct { | ||
Name string `json:"name" validate:"required,min=1,max=100" example:"Napoleon"` | ||
Age int `json:"age" validate:"max=100" example:"18"` | ||
} | ||
|
||
type PetsUpdate struct { | ||
Name string `json:"name" validate:"min=1,max=100" example:"Napoleon"` | ||
Age int `json:"age" validate:"max=100" example:"18"` | ||
} | ||
|
||
func (rs PetsRessources) Routes(s *fuego.Server) { | ||
petsGroup := fuego.Group(s, "/pets") | ||
|
||
fuego.Get(petsGroup, "/", rs.getAllPets) | ||
fuego.Post(petsGroup, "/", rs.postPets) | ||
|
||
fuego.Get(petsGroup, "/{id}", rs.getPets) | ||
fuego.Put(petsGroup, "/{id}", rs.putPets) | ||
fuego.Delete(petsGroup, "/{id}", rs.deletePets) | ||
} | ||
|
||
func (rs PetsRessources) getAllPets(c fuego.ContextNoBody) ([]Pets, error) { | ||
return rs.PetsService.GetAllPets() | ||
} | ||
|
||
func (rs PetsRessources) postPets(c *fuego.ContextWithBody[PetsCreate]) (Pets, error) { | ||
body, err := c.Body() | ||
if err != nil { | ||
return Pets{}, err | ||
} | ||
|
||
new, err := rs.PetsService.CreatePets(body) | ||
if err != nil { | ||
return Pets{}, err | ||
} | ||
|
||
return new, nil | ||
} | ||
|
||
func (rs PetsRessources) getPets(c fuego.ContextNoBody) (Pets, error) { | ||
id := c.PathParam("id") | ||
|
||
return rs.PetsService.GetPets(id) | ||
} | ||
|
||
func (rs PetsRessources) putPets(c *fuego.ContextWithBody[PetsUpdate]) (Pets, error) { | ||
id := c.PathParam("id") | ||
|
||
body, err := c.Body() | ||
if err != nil { | ||
return Pets{}, err | ||
} | ||
|
||
new, err := rs.PetsService.UpdatePets(id, body) | ||
if err != nil { | ||
return Pets{}, err | ||
} | ||
|
||
return new, nil | ||
} | ||
|
||
func (rs PetsRessources) deletePets(c *fuego.ContextNoBody) (any, error) { | ||
return rs.PetsService.DeletePets(c.PathParam("id")) | ||
} | ||
|
||
type PetsService interface { | ||
GetPets(id string) (Pets, error) | ||
CreatePets(PetsCreate) (Pets, error) | ||
GetAllPets() ([]Pets, error) | ||
UpdatePets(id string, input PetsUpdate) (Pets, error) | ||
DeletePets(id string) (any, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/go-fuego/fuego" | ||
controller "github.com/go-fuego/fuego/examples/petstore/controllers" | ||
) | ||
|
||
func newPetStoreServer(options ...func(*fuego.Server)) *fuego.Server { | ||
s := fuego.NewServer(options...) | ||
|
||
petsRessources := controller.PetsRessources{ | ||
PetsService: nil, // Dependency injection: we can pass a service here (for example a database service) | ||
} | ||
petsRessources.Routes(s) | ||
|
||
return s | ||
} | ||
|
||
func main() { | ||
newPetStoreServer().Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-fuego/fuego" | ||
"github.com/stretchr/testify/require" | ||
"gotest.tools/v3/golden" | ||
) | ||
|
||
func TestPetstoreOpenAPIGeneration(t *testing.T) { | ||
server := newPetStoreServer( | ||
fuego.WithoutStartupMessages(), | ||
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{ | ||
JsonFilePath: "testdata/doc/openapi.json", | ||
PrettyFormatJson: true, | ||
}), | ||
) | ||
|
||
server.OutputOpenAPISpec() | ||
err := server.OpenApiSpec.Validate(context.Background()) | ||
require.NoError(t, err) | ||
|
||
generatedSpec, err := os.ReadFile("testdata/doc/openapi.json") | ||
require.NoError(t, err) | ||
|
||
golden.Assert(t, string(generatedSpec), "doc/openapi.golden.json") | ||
} |
Oops, something went wrong.