This is a lightweight twitter back end project with mysql as the database and Django as the REST-API framework. This project follows a typical MVC template and then introduces Redis and Memcached to accelerate the caching and retrieving process.
This project mainly serves as a sample of implementing a MVC oriented coding style and certain backend solutions.
You can always follow the official guide of any backend frameworks to setup your backend service, just gather the following materials:
- environment setup for program language and your framework(full suite Django, light weight flask)
- seup database (if it is a stateful service)
- setting up router and database connection if necessary
- read the guide of how to start make a simple API sample and test it
- Write shell script and requirement.txt to make the above setup process automatic and repeatable
- Write a Dockerfile if for deployment
The folders named with plurals like accounts, comments, tweets are all models. They represent specific objects in the database.
Their structures are quite the same and most API frameworks provide samples to create them. They are usually composed by the following things
- model definition in the database. Some frameworks can define objects in Mysql through code, please keep an eye on official guides on how to maintain them.
- view/API
- serializer/deserializer
with those basic ingridients, you should be able to have at least one running API and retreiveable data
those are the things always good to have if you are considering certain optimizations, but remember they somtimes require trade offs
- pagination, always good to have if you have a long list of data to retrieve but only want a small portion visible
- cache, cahes are good to have but you need to have a cache intermediate and have a good coding template to put initilization and cache clean up in your models
- hepler functions/services, it is a good habbit to have a script for each model to store specific bussiness logics
- tests, always good to have tests for each model and each MVC layers are working
- listeners, you may need to have listeners to do extra work before construction and after destruction of a data, like manipulating caches
For this repository it is twitter.
Usually a full suite of web backend has its own manager folder to store things like settings and meta info. Just look for official documents about how to setup.
TODO: It is reasonable to have secrets in development branches, but if the project should be visible to multiple users and needs deployment, better to use infra frameworks like K8s
Always only stores helper functions that can be used globally here and your client of intermediate components might get here. Such as a Redis client
For this project is it a comprehensive tool box your can refer to if you want certain funcitons in your project.
You can always try to find useful decorators online, lots of good samples on github
For this project it is a decorator to check if parameters/data from request have the same keys as defined. Having this decrator can save some time writing same logic in serializer
The json decoder in Django used to lose some percision at that time so this class overwrites it. You can always consider overwrite the original class in python
The common listener used in this project is to clear caches when object got changed or deleted
An simple cache helper quite useful if you only have a key:value cache needed
always good to havea pagination helper class. You can also check for other paginations samples from other SDK or projects. I recommend Supervisely if you are looking for one for HTTP based client
Django can set are customized ratelimit function globally. You can check for official documents about how to set them
Redis to good to cache list or numeric data
You can find some sample code online. In this project a redis toolbox is combines by the follwing parts
- client: the code the setup and stop client is needed. Usually you just need a global client class for the whole program
- helper: You still need to write your own functions or wrap them if you want to load, clean list or manipulate numbers
- serializer: finally if you are working with database, you will need to serialize/deserialize them with models