Skip to content

SherlockH0/Blog-API

Repository files navigation

Blog API

GitHub Actions Workflow Status

This is a simple blog API with AI moderation features, written in python with Django and Django Ninja.

Table of Contents

Demo

Watch the video

Tech Stack

Usage

Clone the repo, and cd into it:

git clone https://github.com/SherlockH0/Blog-API.git
cd Blog-API

Then, you can either run the application in testing mode using docker compose, or run the application in development mode.

Launching the application in testing mode

Make sure you have Docker installed on your system.

Create local settings files:

mkdir -p local
cp blogapi/project/settings/templates/settings.prod.py ./local/settings.prod.py

And set a google AI API key in it (you can omit this step, but the AI features won't work):

# local/settings.prod.py
- GOOGLE_AI_API_KEY = NotImplemented
+ GOOGLE_AI_API_KEY = "djjnJIJIJhkh"

Run the application:

docker compose up

Application is now available on localhost:8000 (for more routes see API Docs)

Development and running tests

Make sure you have Docker and Python ^3.12 installed on your system. I'm also using Poetry as a dependency manager, so if you have it installed on your system, it would be a plus.

Create local settings files:

mkdir -p local
cp blogapi/project/settings/templates/settings.dev.py ./local/settings.dev.py
cp blogapi/project/settings/templates/settings.unittest.py ./local/settings.unittest.py

And add set a google AI API key in both of them (.dev file is used for development, and .unittest is used for running tests) (you can omit this step, but the AI features won't work):

# /local/settings.dev.py or /local/settings.unittest.py
- GOOGLE_AI_API_KEY = NotImplemented
+ GOOGLE_AI_API_KEY = "djjnJIJIJhkh"

Create and activate virtual environment:

python -m venv venv
# Linux, MacOS, Windows (WSL)
source venv/bin/activate
# Windows
venv\Scripts\activate.bat

In a separate terminal window start PostgreSQL and Redis with docker:

docker-compose -f docker-compose.dev.yml up --force-recreate

Then, continue depending on your system:

With Makefile and Poetry (Linux, MacOS, Windows (WSL))

Install the project using poetry, migrate the database, and create superuser (optional):

make install
make migrate
make superuser

Run local server:

make runserver

In a different terminal window, run rq worker and scheduler:

make rq
make rqscheduler

To make migrations after changes in the models, run:

make migrations

To migrate, run:

make migrate

To run tests, run:

make test
# With coverage
make test-cov
# With html coverage
make test-cov-html
With Poetry (All systems with Poetry installed)

Install the project using poetry, migrate the database, and create superuser (optional):

poetry install
poetry run python -m blogapi.manage migrate
poetry run python -m blogapi.manage createsuperuser

Run local server:

poetry run python -m blogapi.manage runserver

In a different terminal window, run rq worker and scheduler:

poetry run python -m blogapi.manage rqworker default
poetry run python -m blogapi.manage rqscheduler

To make migrations after changes in the models, run:

poetry run python -m blogapi.manage makemigrations

To migrate, run:

poetry run python -m blogapi.manage migrate

To run tests, run:

poetry run pytest -v -rs
# With coverage
poetry run pytest -v -rs --cov
# With html coverage
poetry run pytest -v -rs --cov --cov-report html
With pip (All systems)

Install the project using pip, migrate the database, and create superuser (optional):

pip install .
python -m blogapi.manage migrate
python -m blogapi.manage createsuperuser

Run local server:

python -m blogapi.manage runserver

In a different terminal window, run rq worker and scheduler:

python -m blogapi.manage rqworker default
python -m blogapi.manage rqscheduler

To run tests, run:

pytest -v -rs
# With coverage
pytest -v -rs --cov
# With html coverage
pytest -v -rs --cov --cov-report html

API Docs

Django-Ninja comes with an easy to use interactive API documentation. If you have launched the application, you can check it out on localhost:8000/api/docs.

Django also comes with a featureful admin panel which you can use by visiting localhost:8000/admin (you have to create superuser to use it)

Authentication

Blog-API implements a simple JWT authentication.

To create a user, make a POST request to the /api/users route.

To log user in, obtain access and refresh tokens by making a POST request to the api/token/pair route.

To access protected routes, add a Authorization header into your request:

Authorization: Bearer <token>

About

✒️ A simple blog API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published