In this workshop we will learn how to use the main functions of the http package in Go and how to create a 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 or check access rights before entering a controllerroutes: the core of the router, setting up all the routes handler, with their middlewares and controllersmodels: wraps all the database calls
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 /hello:
- It uses the
GETmethod - It responds
"hello world"
You can test it with
http://localhost:8080/helloin a browser or withpostman
You are now fine with the creation of endpoint. Next step is to add messages logs because our server doesn't print anything, yet we would like to know when a route is called to know if everything went well.
Create a middleware that logs the traffic of the API:
- Use the
lognative package - Apply this middleware to the
/helloroute
This tutorial may help you
You can make this middleware global to all route rather than calling it manually on each and every route you create
See how to request with postman 📡
Enter your URL and the method you which to use in the titlebar and click Send.
Then the result (if there is any) will be printed out at the bottom.
On a real API, there are parts of your routes you only want some people to use. To protect some routes from unknown users, we'll have to add another middleware.
- Create an middleware that checks if the request has a header called
Authorizationand if it contains theallowedstring - Create a route
/auth/hellowith both middlewares (logs and the one we've just created) to check if it works
In a real life scenario you will check if the field contains a token, and if this token is valid thanks to
JSON Web Tokenor any sessions
r.Headeris of typemap[string]string, you should give a look at how to check if a value exist within a map beforehand
See how to send a header 📡
Go into the 3rd panel, there you will be able to create the headers that you want to send, toggle the checkbox to send them or not.
Creating routes is nice, but we cannot receive variables from them... To do so, we will use "github.com/gorilla/mux" to have modular routes
Create an endpoint /whoami/{user}
- It uses the
GETmethod - It responds
I am $userwith$userbeing the text sent in the URL
Look at the
{``}syntax, these brackets indicate which part of the URL will contain vars
Check the mux doc for more information
Finally, we will use the API to manipulate data in a database. As we don't have the time to setup a real database, we've created functions to fake the behavior of a simple database.
You must import the
modelpackage, it contains 3 functions to manipulate users.
You will need to implement an endpoint for:
/add- It uses the
POSTmethod - It creates a user thanks to the information given in the
body
- It uses the
/get/{id}- It uses the
GETmethod - It gets a user based on its
id
- It uses the
/del/{id}- It uses the
DELETEmethod - It deletes a user based on its
id
- It uses the
The
bodywill contain a JSON that has the same structure that the one designed in the model package asmodel.Users
See how to send JSON in body 📡
Go into the 4rd panel and select the raw option, then you can write your JSON (you can also copy this one).
Theoretically, you should have all the basis needed to create your own API, if you still want to toy around with Go and all its usages, you can take a look at other go features like:
go routinesinterfaces
You can also use a real database with these ORM packages:



