A simple fullstack app - exercise to guide me throught the process of adding an authendication setup procedure with Keycloack.
- Create a new Python project directory and set up a virtual environment to isolate project dependencies.
- Inside the project folder
python -m venv venv- Activate the virual Enviroment
source venv/bin/activate- Install Flask and database library.
-
-
PostgreSQLis a robust, open-source relational database system known for its reliability, performance, and feature-richness. It's an excellent choice for our book manager application. -
Flask-SQLAlchemyis an extension for Flask that provides an ORM (Object-Relational Mapper) interface for interacting with databases. It simplifies database operations, making it easier to work with PostgreSQL in our Flask app. -
Why This Combination? PostgreSQL offers a solid foundation for our data storage needs, providing features like transactions, foreign keys, and indexing. Flask-SQLAlchemy streamlines database interactions, allowing us to define models as Python classes and perform CRUD operations without writing raw SQL.
-
By using this combination, we'll benefit from:
-
Improved productivity: Flask-SQLAlchemy simplifies database interactions.
-
Enhanced data integrity: PostgreSQL ensures data consistency and reliability.
-
Scalability: PostgreSQL can handle increasing data volumes and concurrent users.
-
-
- Define database models for books (title, author, etc.).
- Create Flask app with routes for book CRUD operations (create, read, update, delete).
- Implement database interactions using PostgreSQL and Flask-SQLAlchemy.
pip install Flask Flask-SQLAlchemy psycopg2This will install:
Flask: The web framework for our application. Flask-SQLAlchemy: For database interactions. psycopg2: The PostgreSQL adapter for Python.
- We need to :
- Import Flask and Flask-SQLAlchemy.
- Create a Flask application instance.
- Configure the database URI with your PostgreSQL credentials.
- Create a SQLAlchemy instance.
- Define the Book model with columns for id, title, and author.
- ! First create a table from the Postgresql command line and then use the API(app.py) !
- ? Do we need def to_dict(self): method ?
- (is obligatory ? )The repr method provides a string representation of a book instance for debugging purposes.
Added procedure: run the local database
sudo -u postgres psql -d bookstorerun the app.py
python3 app.pymake requests with curl
# create books
curl -X POST -H "Content-Type: application/json" -d '{
"title": "The book of my life", "author": "john Doe"}' http://127.0.0.1:5000/books
# get books or a spesific book
curl http://127.0.0.1:5000/books
curl http://127.0.0.1:5000/books/<id>
# update book
curl -X POST -H "Content-Type: application/json" -d '{
"title": "The book of my life", "author": "john Doe"}' http://127.0.0.1:5000/books
# delete a book
curl -X DELETE http://127.0.0.1:5000/books/<id>The database and flask deployment were done in Railway. The current sttus is working. The next steps are to make the frontend with Reactjs and also deploy that.
- Create a new React project using Vite.
- Design UI components for book listing, book form, and user interface (simple design for the meaning of the project).
- Implement state management for book data using React Context or Redux (not in the beginning!).
- Fetch book data from backend using API calls.
- Handle form submissions to create, update, and delete books.
- Connect frontend to backend using API calls.
- Thoroughly test both frontend and backend functionalities (...later).
- Implement error handling for API requests and database operations(...later).
- Integrate Keycloak into the backend.
- Implement token-based authentication for protected routes.
- Secure API endpoints with authentication checks.
- Handle user authorization based on roles or permissions (if needed).
- Create Dockerfiles for both frontend and backend.
- Define Docker Compose for multi-container setup (for database, frontend, and backend).
- Build and run Docker images to deploy the application.
mkdir: to create the directory of the projectcd [bookstore]: to move to the new directorytouch [README.md]: to create a new file, and also the same for gitignorerm -r [virtualenv]: to delete a wrongly setup of venv folder