Getting Started β Project Overview
This project is a minimal REST API tutorial that demonstrates how to build a library service in Python 3.8 using Flask, Flask-RESTful, Flask-JWT, and Flask-SQLAlchemy on top of a SQLite database. It exposes CRUD endpoints for books and stores and includes JWT-based authentication and user registration. The main application entry point is PYTHON/app.py, which wires together the Flask app, API resources, authentication, and database models.
βοΈ Tech Stack
Below is an overview of the core technologies and libraries used:
| Technology | Purpose |
|---|---|
| Python 3.8 | Programming language |
| Flask | Micro web framework |
| Flask-RESTful | Simplifies building REST APIs |
| Flask-JWT | Provides JWT-based authentication |
| Flask-SQLAlchemy | ORM integration for Flask |
| SQLite | Lightweight file-based relational database |
π¦ Project Structure
The repositoryβs key components are organized as follows:
- PYTHON/app.py
Main entry point. Configures Flask, JWT, database, and registers API endpoints.
- PYTHON/db.py
Initializes the shared SQLAlchemy instance.
- PYTHON/security.py
Defines authenticate and identity functions for JWT.
- PYTHON/models/
BookModel,StoreModel,UserModelclasses map to database tables.- PYTHON/resources/
Book,BookList,Store,StoreList,UserRegisterRESTful resources handle HTTP requests and responses.- PYTHON/postman-collections/
- Postman collection for quick endpoint testing.
- PYTHON/readme.md
- Setup instructions and example requests/responses.
How the Application Boots
Below is a simplified flow of what happens when you start the app:
flowchart TD
A[Start: python app.py] --> B[Initialize Flask App]
B --> C[Configure SQLAlchemy]
C --> D[Register before_first_request create_tables]
D --> E[Setup JWT /auth with authenticate & identity]
E --> F[Register RESTful resources]
F --> G[Run server on port 5000]
Key Initialization Snippet
The following excerpt from PYTHON/app.py shows how the application is configured and started:
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['PROPAGATE_EXCEPTIONS'] = True
app.secret_key = 'SapanCrackle'
api = Api(app)
@app.before_first_request
def create_tables():
db.create_all()
jwt = JWT(app, authenticate, identity) # /auth
api.add_resource(Store, '/store/<string:name>')
api.add_resource(StoreList, '/stores')
api.add_resource(Book, '/book/<string:title>')
api.add_resource(BookList, '/books')
api.add_resource(UserRegister, '/register')
if __name__ == '__main__':
from db import db
db.init_app(app)
app.run(port=5000, debug=True)
- Flask configuration sets up database URI and secret key.
create_tables()ensures tables exist before any request.JWT(app, authenticate, identity)registers the/authendpoint.api.add_resource(...)binds resources to URLs.
Component Relationships
The following Class Diagram illustrates core model relationships:
classDiagram
BookModel <|-- Resource : Book, BookList
StoreModel <|-- Resource : Store, StoreList
UserModel <|-- Resource : UserRegister
BookModel : id
BookModel : title
BookModel : author
BookModel : isbn
BookModel : release_date
BookModel : price
BookModel : store_id
StoreModel : id
StoreModel : name
UserModel : id
UserModel : username
UserModel : password
StoreModel "1" o-- "many" BookModel : contains
BookModelandStoreModelare linked by a foreign key (store_id).UserModelis used for JWT-based authentication.- Each Resource class interacts with its corresponding Model to handle CRUD operations.
This overview gives you the context needed to dive into individual components, explore the code, and extend the library service with new features.