Skip to content

Realworld Backend API implementation using Sanic Framework in Python

License

Notifications You must be signed in to change notification settings

ankur26/realworld-sanic

 
 

Repository files navigation

RealWorld Example App

Sanic codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.

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!

How it works

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)

Requirements

The application has been tested with version Python 3.10.12, we recommend you install at least 3.10 to best reproduce the results

Getting started

Using Python

  1. Clone the repo
  2. Create a virtual environment using venv, virtualenv inside the repo directory

python -m venv .venv

  1. Activate the virtual enviroment in

    • Windows - using ./.venv/Scripts/activate
    • Linux - using source .venv/bin/activate
  2. Install the required dependencies using pip install -r requirements.txt

  3. Launch the app locally using sanic realworld.server:create_app --dev(Dev mode) or sanic realworld.server:create_app(Production mode)

    1. To run unit tests using the API test script
      1. Install newman using npm (Requires NodeJS)
      2. In the project's root directory, run the test using APIURL=http://localhost:8000/api ./run-api-tests.sh
    2. To run the tests using pytest run pytest tests/
      1. For the verbose option use pytest --verbose tests/
      2. To see the code coverage use pytest --cov=realworld/ tests/ (You can combine these two options)

Using docker

Prequisites : Docker pre-installed.

  1. Clone the repo
  2. Run [sudo] docker compose build (Default mode in development, prod docker file exists but reference is not meant for production use)
    1. To run the docker file with sanic in prod mode, copy the contents of Dockerfile.prod to Dockerfile and run command in step 2.
    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.)
  3. Run [sudo] docker compose up -d to run the application
  4. Run [sudo] docker compose down to stop the application

Specs tracker

  • 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

Additional Features (more generalized things)

  • 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)

Libraries used

  1. Sanic - asynchronous Python web framework
  2. Peewee - easy to use ORM
  3. Pydantic - Fast and extensible validation library, most used validation library for Python
  4. Isort and Black for import orders, and linting.
  5. Bcrpyt for password hashing and PyJWT for authentication.
  6. Pytest for testing and pytest-cov for code coverage.

Note: This is not meant to be used in a production environment!

About

Realworld Backend API implementation using Sanic Framework in Python

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 97.1%
  • Dockerfile 2.2%
  • Shell 0.7%