In this workshop we will learn how to use the main functions of the Gin package in Go and how to create a REST API.
💡 A quick documentation about REST API.
All the required information to install the workshop's dependencies are given in the SETUP.md
We designed a boilerplate for this project, it will be useful to start this project, but also to summarize all the required parts of an API We will have :
controllers
: this is where you will design your routes endpoint. it will often be a wrapper that calls other API or the database itselfmiddlewares
: those are functions that can be called to print logs ,check access rights before entering a controller or define a default endpointroutes
: the core of the router, setting up all the routes handler, with their middlewares and controllers
There are plenty of other important package you may need in a real API, but these are the main one you can be sure you'll need one time or another. Now let's code.
Add a route on the endpoint /
:
- It uses the
GET
method - It responds
"hello: world"
in a json format
You can test it with
http://localhost:8080/
in a browser or withpostman
You are now fine with the creation of endpoint.
Next step is to create different request method.
There is many request method, you already experimented the GET
request.
For this step, we will use some others' method:
- a
GET
request/item/template
: to get the template of the item as follows.
{
"name": "name",
"description": "description",
"price": 0
}
- a
POST
request/item/display
: to display the item passed in the body as follows.
also try to implement a default endpoint for the URL's that doesn't match any routes. (404 notfound error)
This tutorial may help you
You should create a structure item with the following fields:
- name
- description
- price
and bind the request body to the structure to retrieve it (do it in a structure folder).
See how to request with postman 📡
Enter your URL and the method you which to use in the title bar and click Send
.
Then the result (if there is any) will be printed out at the bottom.
For the following we won't use a real database, but we will use a simple json file as the database. (The configuration would have been too long to explain and to do here) You can see the database.json in the database folder.
- Create a
GET
request/item
: to retrieve all the items in the database. - Create a
POST
request/item
: to create a new item in the database.
You must fill the
database/database.go
functions you need (GetDatabase
&AddItemInDB
) and call.
Look at the documentation to interact with the file
To go further you can add some others routes :
- a
DELETE
request/item
: to delete an item in the database. - a
PUT
request/item
: to update an item in the database. - a
POST
request/items
: to create multiple items in the database.
If you still want to toy around with Go and all its usages, you can take a look at other go features like:
go routines
interfaces
You can also use a real database with this ORM packages:
Ismaël FALL |
---|
🚀 Don't hesitate to follow us on our different networks, and put a star 🌟 on
PoC's
repositories.