- Post Management: Create, update, delete, and view posts.
- User Managementn: Register users, retrieve user information, and manage user profiles.
- Authentication: Secure login system with JWT-based authentication.
- Voting System: Users can upvote posts, with the option for back-voting but no down-vote logic.
- The
main.pyfile initializes the FastAPI app and imports all necessary routes (Post, User, Auth, and Vote). - It also sets up the database connection through SQLAlchemy.
- The configuration file manages environment variables using Pydantic's
BaseSettings. - It loads database credentials, JWT secrets, and other configuration from a
.envfile.
- Defines the PostgreSQL connection using SQLAlchemy.
- Provides a session management system for database transactions.
- SQLAlchemy models for database tables: Post, User, and Vote.
- Implements relationships between the models to manage foreign keys.
- Handles JWT token creation and verification using the
joselibrary. - Secure token-based authentication for user login and session management.
- Pydantic models for request validation and response serialization.
- Includes models for Posts, Users, Tokens, and Votes.
- Utility functions for password hashing and verification using
bcrypt.
- Post Routes: CRUD operations for posts.
- User Routes: Create and retrieve user profiles.
- Auth Routes: Login and secure authentication.
- Vote Routes: Manage votes (upvote, back vote).
- Python 3.9+
- PostgreSQL
git clone https://github.com/0xfarben/fastapi.git
cd socialhubpip install -r requirements.txtpip install fastapi[all]- Install and configure PostgreSQL.
- Create a new database for the application.
- Create a
.envfile in the root directory and add the following environment variables:
DATABASE_HOSTNAME=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=your_db_user
DATABASE_PASSWORD=your_db_password
DATABASE_NAME=your_db_name
SECRET_KEY=your_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60uvicorn main:app --reload
- Swagger UI: https://swagger.io/tools/swagger-ui/
- FAST API: https://fastapi.tiangolo.com/
POST /users
{
"email": "user@example.com",
"password": "strongpassword"
}POST /login
{
"email": "user@example.com",
"password": "strongpassword"
}POST /posts
{
"title": "My First Post",
"content": "This is the content of my first post!"
}POST /vote
{
"post_id": 1,
"dir": 1
}- FastAPI for building the web framework.
- SQLAlchemy for ORM support.
- Uvicorn for ASGI server implementation.
