Implementing Session caching in Go with Gin Sessions, Redis and Docker

Go’s web framework Gin-Gonic provides developers with a wide range of approaches to keep track of users’ sessions. In this article I will discuss the one involving Redis.

Uxío García Andrade
4 min readDec 15, 2019

While trying to implement gin’s sessions with Redis in Go, I went through some issues that could have been avoided hadn’t there been a lack of examples that helped me understand how to implement it. So, once I figured it out, I thought it would be a good idea to make a post out of it.

https://unsplash.com/photos/WiONHd_zYI4

This article will cover how to implement a basic API with user authentication and session cache in Go. For that purpose, we will use the Gin-gonic framework and Redis, running both services in separate docker containers.

First of all, let’s set up our environment. In your working directory, create a file name go.mod and add the following two lines to the file:

module github.com/username/repositorynamego 1.13

Now, create a file called main.go, and paste the following code (gin’s official documentation first example).

Basic gin-gonic server

As we said before, we plan to dockerize all our services, so now let’s create a Dockerfile.dev file to build our gin container.

gin-gonic hot reload dockerfile

Note that we don’t copy all our files before running go mod, as that would imply that we wouldn’t be able to use docker’s cache when running the go mod download. On the other hand, in order to enable hot reload, we will run our server with fresh.

Once we have our Dockerfile ready, we can test if it is working properly. First, let’s build the container.

docker build -t uxioandrade/gin-sessions -f Dockerfile.dev .

After the -t tag you can name the image in any way you want, but remember to use the same name in the docker run command:

docker run -p 8080:8080 uxioandrade/gin-sessions

Once the container is up, you can test that everything is running as it should with the following command:

curl localhost:8080/ping

If you receive a {“message”:”pong”} as a response, everything is okay.

Now that we have our Go container up and running, let’s see how we can create the Redis one and establish a connection between both of them. For that purpose, we will just create a docker-compose.yml file as follows:

docker-compose.yml

After doing that, we may start both services by running the following command:

docker-compose up 

Once you check that both services are running, let’s go back to our main.go file. First of all, we must initialize our redis store, and indicate our gin router to use it for all our routes. Then, we will set up routes for logging in and out, and then we will create another group which will use a middleware function which checks if the user is logged in. If that’s not the case, the middleware function will return a JSON error and prevent the server from getting to the handler function.

Authentication and routing

Now, let’s define the userController and the two controller functions. Note that all the code related to controllers should be in a different package, but for the purpose of simplicity, I just added all the code to the main.go file. Moreover, in a real world application, we would implement the login feature by connecting to a database and validating the request’s credentials, but here I skipped that part.

User Controller

In the end, the only remaining step is to test that everything is working. We will use the curl command to send requests to our server. However, we need some extra file to keep track of the cookies, so that our server can recognize the user’s session. One simple solution would be to create a cookies.txt file, and run the following commands (remember that you need to be running the containers with docker-compose up):

Testing our implementation

As we can see, the first time we tried to send a get request to the /auth/ping endpoint, we got an error stating that we were not authed. Then, once we log in, this error goes away and we can access the ping function. After that, we log out and get the error back.

Gin’s sessions library is such a powerful library which allows developers to keep track of user authentication in Redis with a few lines of code. In case you have had any issues with this tutorial, you can find all the code in the this repository.

--

--