This is the second part of the tutorial series, if you missed PART I, you might be able to catch up from here.
For the entirety of this part, we will be working on building the storage layer of our short link generator/forwarder, so the main steps we will be having are :
- Setup the Store Service
- Storage API design and Implementation
- Unit & Integration Testing
Recall, we installed Redis and loaded the needed Go depencies in the previous chapter, so for this one we will be putting them to use.
Note : In a real world workload, Redis storage alone can easily be overwhelmed, so a traditional RDBMS like Postgres or Mysql is used as a background storage for the least requested data ( cold values )
And Redis is used only to hold hot values that are often used/requested and have to maintain a super fast retrieval time.
Turn up your editor, buckle up and there we go ! 🚀
II. 1. Store Service Setup 🛠
First thing first, we are gonna have to create the our store folder in the project, So go inside the project directory, create a sub-directory named
store and go ahead and create 2 empty Go files :
store_service_test.go (Where we will be writing unit tests for the storage later )
└── store ├── store_service.go └── store_service_test.go
- We will start by setting up our wrappers around Redis, the wrappers will be used as interface for persisting and retrieving our application data mapping.
- After defining wrapper structs we can finally be able to initialize the store service, in this case our Redis client.
II. 2. Storage API design and Implementation ⚙️
Now that we have initialized successfully our data store service, it's time to think about what storage API to expose for our needs in particular for our shortener server.
- The next step is obviously implementing our storage API, and will all our setup done, it should be fairly straightforward.
II. 3. Unit & Integration Testing 🧪
For preserving the best practices and avoiding unintentional regressions in the future, we are going to have to think about unit and integration tests for our storage layer implementation
Now let's go and install the testing tools we will need to this part.
go get github.com/stretchr/testify
Recall we initially created
store_service_test.go file in the store directory, if you haven't done so yet, please go and do it.
Our project directory tree should look something similar to the tree below.
├── go.mod ├── go.sum ├── main.go └── store ├── store_service.go └── store_service_test.go
- We will start by setting up the the tests shell.
- Now let's go on and unit test the store service initialization.
- Finally we will go on and add tests for the storage APIs
The tests above should all pass if everything was setup well, in case you are having difficulties following up, don't hesitate do DM me on Twitter or email me, I should find some time helping out.
II. 4. Conclusion and Next Steps
In this part we were able to setup our storage service, implemented the APIs and finally added the unit tests that were needed. Hope you enjoyed this part !
As we now have our storage service setup, the next step will be about the short url generation algorithm, trust me that's gonna be too much fun 😄
Ciao ! 👋