This codebase was created to demonstrate a backend API built with Sanic including CRUD operations, authentication, routing, pagination, and more.
We've gone to great lengths to adhere to the Sanic community styleguides & best practices.
For more information on how to this works with other frontends/backends, head over to the RealWorld repo.
All contributions are welcome!
This is then general folder structure of the application
├── helpers (Offloaded repetitive function calls)
│ ├── __init__.py
│ ├── article_and_comment_fetch_helper.py
│ ├── jwt_token_helper.py
│ └── serializer_helper.py
├── middleware (Decorator style functions for middleware such as validation and token checks)
│ ├── __init__.py
│ ├── request_content_validator.py
│ └── request_header_and_body_validator.py
├── models (Database models - used to create and query)
│ ├── __init__.py
│ ├── article.py
│ ├── articletag.py
│ ├── base.py
│ ├── comment.py
│ ├── follower.py
│ ├── tag.py
│ ├── userfavorite.py
│ └── user.py
├── schemas (Used to validate incoming requests and serialize outgoing ones)
│ ├── __init__.py
│ ├── article_comment_schema.py
│ ├── profile_schema.py
│ └── user_schema.py
└── services (Core route blueprints, responsible for overall request response cycle)
├── __init__.py
├── article_comment_service.py
├── auth_service.py
├── profile_service.py
└── tag_service.py
├── server.py (Core server file, has setup and configuration)
The application has been tested with version Python 3.10.12, we recommend you install at least 3.10 to best reproduce the results
- Clone the repo
- Create a virtual environment using venv, virtualenv inside the repo directory
python -m venv .venv
-
Activate the virtual enviroment in
- Windows - using
./.venv/Scripts/activate
- Linux - using
source .venv/bin/activate
- Windows - using
-
Install the required dependencies using
pip install -r requirements.txt
-
Launch the app locally using
sanic realworld.server:create_app --dev
(Dev mode) orsanic realworld.server:create_app
(Production mode)- To run unit tests using the API test script
- To run the tests using pytest run
pytest tests/
- For the verbose option use
pytest --verbose tests/
- To see the code coverage use
pytest --cov=realworld/ tests/
(You can combine these two options)
- For the verbose option use
Prequisites : Docker pre-installed.
- Clone the repo
- Run
[sudo] docker compose build
(Default mode in development, prod docker file exists but reference is not meant for production use)- To run the docker file with sanic in prod mode, copy the contents of Dockerfile.prod to Dockerfile and run command in step 2.
- To run the docker file with sanic in dev mode, copy the contents of Dockerfile.dev to Dockerfile and run command in step 2. ( Dev mode creates an in memory database rather than an SQlite db so better to run the API test script.)
- Run
[sudo] docker compose up -d
to run the application - Run
[sudo] docker compose down
to stop the application
- Auth
- Registration
- Authentication
- Get current user
- Update User
- Profile
- Get Profile
- Follow User
- Unfollow User
- Articles
- List Articles
- Get Articles
- Get Feed
- Fitler Articles
- Get Article
- Create Article
- Update Article
- Delete Article
- Comments
- Get Comments
- Add Comments
- Delete Comments
- Tags
- Get Tags
- Centralized logging using Sanic logger
- Custom exception handler and logging using Sanic logger and Sanic exception
- Segregation of services using Sanic Blueprints
- Unit testing (Currently at 80%, contributions are welcome!)
- Containerization using docker (Simple docker file, for local runs)
- Sanic - asynchronous Python web framework
- Peewee - easy to use ORM
- Pydantic - Fast and extensible validation library, most used validation library for Python
- Isort and Black for import orders, and linting.
- Bcrpyt for password hashing and PyJWT for authentication.
- Pytest for testing and pytest-cov for code coverage.