Create a simple RESTful API using FastAPI for a social networking application
-
There should be some form of authentication and registration (JWT, Oauth, Oauth 2.0, etc..)
-
As a user I need to be able to signup and login
-
As a user I need to be able to create, edit, delete and view posts
-
As a user I can like or dislike other users’ posts but not my own
-
The API needs a UI Documentation (Swagger/ReDoc)
- Use https://clearbit.com/platform/enrichment for getting additional data for the
user on signup
- [-] not completed
- Use emailhunter.co for verifying email existence on registration
- [-] not completed
- Use an in-memory DB for storing post likes and dislikes (As a cache, that gets
updated whenever new likes and dislikes get added)
- [+] completed with redis
- Fastapi
- fastapi_users - auth
- Pydantic
- SQLAlchemy
- Alembic
- PostgreSQL
- asyncpg - driver
- Redis
Source api folder. Contains all apps.
Configuration file for caching.
Configuration file for database.
Main script. collects all routers.
Config file for whole project.
App for auth from fastapi_users.
Config file for fastapi_users.
In this project JWT strategy (JWT auth) used with cookie transportation (JWT stored in a cookie).
User manager for User model from fastapi_users. Adds some features for User model e.g. additional handling after registration/lofin.
User - table with mixin from fastapi_users.
Mixin adds service attributes for correct operation of fastapi_users (is_active flag, is_superuser etc.).
Pydantic schemas for fastapi_users.
Some helper functions. Contains get_user_db for getting User object.
Cache manipulation functions. update_cache_reactions for updating/adding row into redis.
Dependencies for additional functional (validating of Post id, common params used in several functions).
Posts exceptions. They are all based on HTTPExceptions.
Stores posts models.
ReactionType - PostgreSQL enumerate (at this moment contains only two reactions - like and dislike).
Reaction - Secondary table for m2m (User can like many posts, Post can have many users like it).
user_id: combined pk "user_id-post_id", fk, UUIDpost_id: combined pk "user_id-post_id", fk, UUIDtype: ReactionType - PostgreSQL Enum
Post - Post table.
id- pk, UUID, default: uuid4owner_id: fk, UUIDowner: SQLAlchemy relationtitle: strdescription: textcreation_date: TIMESTAMP, default: Postgresql function now()last_update_date: TIMESTAMP, default: Postgresql function now(), updates when record is changeduser_reactions: SQLAlchemy relation, set of User's objects.
Contains all routes of posts app.
More detailed see in Swagger (tag "posts").
Pydantic schemas for posts app.
Contains:
CreatePost- used to receive user data to create new postsEditPost- user to receive user data to update existing post; have validator for removing leading and trailing spaces
This file contains app specific business logic. Mostly it is retrieve data from db (or add) and process it.
Alembic folder for storing migrations and migration conf.
Alembic config file.
Dockerfiles for services.
Docker-compose file.
Inital file for creating database in postgresql container.
- Clone project to the desired directory with this command:
git clone https://github.com/Daniil7575/Webtronics_task.git - Go to the root folder of project (
Webtronics_task) - Add
.envfile and fill it with data provided below (all fields were filled in in advance to save your time):DB_HOST=db DB_PORT=5432 DB_NAME=webtronics DB_USER=postgres DB_PASS=wejfhfYiug&687 JWT_SECRET=d92fa843055391a9abe9b4d9011dee549383ab890cc15482d8e4869a9297ef1f REDIS_URL=cache REDIS_PORT=6379 - Build and up containers with the following commands
sudo docker compose build sudo docker compose up - Wait until everything starts. When it starts below text will be displayed in console:
webtronics_task-api-1 | INFO [alembic.runtime.migration] Context impl PostgresqlImpl. webtronics_task-api-1 | INFO [alembic.runtime.migration] Will assume transactional DDL. webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Started server process [1] webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Started server process [1] webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Waiting for application startup. webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Waiting for application startup. webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Application startup complete. webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Application startup complete. webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) webtronics_task-api-1 | INFO: [2023-08-20 16:27:08] Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) - Go to
http://0.0.0.0:8000/docsorhttp://127.0.01:8000/docsand and you will be taken to Swagger.