FastHotelAPI is a project built using FastAPI to explore REST API development with Python. It is designed to manage hotels, users, and reviews efficiently while utilizing role-based access control and secure JWT-based authentication.
- Admin-only routes for managing users and sensitive data.
- Regular user routes for managing reviews and accessing hotel data.
- Implements JWT for authentication and authorization.
- Supports both admin and user tokens with role distinction.
- Create, retrieve, and list all hotels.
- Ensures scalability with proper repository and service separation.
- Admins can manage users, including retrieving, updating, and deleting accounts.
- User registration and login are supported with secure password hashing.
- Users can post, retrieve, and delete reviews for hotels.
- Reviews are tied to hotels and users, ensuring a relational structure.
The application is divided into layers:
- Routers: Define API endpoints and handle HTTP requests and responses.
- Services: Contain business logic, ensuring separation from data access logic.
- Repositories: Handle database interactions, ensuring the persistence layer is isolated from the business logic.
- Dependencies like repositories are injected using FastAPI's
Depends, allowing for easier testing and modularity.
- LoggingMiddleware: Logs all incoming requests, response times, and errors.
- AdminOnlyMiddleware: Restricts access to admin routes, ensuring enhanced security.
- PostgreSQL is used as the database backend.
- Repositories directly interact with the database, abstracting SQL queries and handling schema mappings.
- Secure password hashing using
bcrypt. - Expirable JWT tokens for session management, with separate keys for access and refresh tokens.
- Admin Token Issues: Admin tokens can access user endpoints, which sometimes results in errors. This happens because certain endpoints rely on
user_idas a foreign key. When using an admin token, the ID belongs to theadminstable instead of theuserstable, causing mismatches. - Logging: Logging could be improved. I'm not entirely sure if I'm doing it correctly or placing it in the right spots.
- Admin Dependencies: The file
util/admin_dependencies.pyfeels out of place. I’m not sure where it should go, but I’d prefer to restructure and move it elsewhere. - Refresh Tokens: I still need to implement functioning refresh tokens.
- Exception Handlers: The file
middleware/exception_handlers.pydoesn’t feel like typical middleware. Its current location makes some sense, but I’m not entirely satisfied with it. - Repository Exceptions: I might need to move exceptions to the repository classes instead of handling them entirely in the service layer. I like the current cleanliness of the code, but I’m not sure if this is the best approach.
- Fully containerized using Docker, ensuring consistent environments across development and production.
.envconfiguration allows easy setup and deployment flexibility.
Refer to the instructions below for launching the application.
-
Clone the Repository
git clone https://github.com/isErenG/FastHotelAPI.git
-
Configure Environment Variables
Fill in the
.envfile with your PostgreSQL credentials. An example file.envexampleis provided. Copy it to create your own.envfile:cp .envexample .env
Edit
.envto include the necessary credentials:POSTGRES_DATABASE=your_database_name POSTGRES_USER=your_username POSTGRES_PASSWORD=your_password POSTGRES_HOST=db POSTGRES_PORT=5432
-
Launch with Docker
cd deploy/ docker compose --env-file ../.env up -d --buildIf you wish to close the docker compose
docker compose --env-file ../.env down