A demonstration of a Python REST API server with JWT Authentication and Memory Caching using FastAPI framework.
Database connection is handled by SQLAlchemy 2.0.x library.
JWT authentication is handled by python-jose library.
The caching is handled by cachetools library.
The payload size limiter is handled by content-size-limit-asgi middleware library.
- Python v3.10.x or newer.
- MySQL database v8.0 or newer with an empty database/schema prepared.
-
Initialize and activate virtual environment inside the project folder:
$ python3 -m venv venv $ . venv/bin/activate
-
Install the required libraries:
$ pip3 install -r requirements.txt
-
Modify the
DB_URL
environment variable in.env
file according to your database.
-
Activate the virtual environment if you haven't already:
$ . venv/bin/activate
-
Run the server:
$ uvicorn main:app --reload
The server will run at http://localhost:8000
The swagger API docs can be accessed at http://localhost:8000/docs
The tables in database will be created automatically if they don't exist yet when the server starts or reloaded.
-
POST /auth/signup
(Signup endpoint)- Accepts
email
,password
, andconfirm_password
values. - Returns a token (JWT) with 1 hour expiry time if successful.
- Accepts
-
POST /auth/login
(Login endpoint)- Accepts
email
andpassword
values. - Returns a token (JWT) with 1 hour expiry time if successful.
- Accepts
-
POST /posts
(Add New Post endpoint)- Endpoint is protected by JWT authentication (
Bearer <token>
header is required). - Accepts
text
value, and it will create a new Post that belongs to the authenticated User if successful. - Payload size is limited to 1 MB.
- Endpoint is protected by JWT authentication (
-
GET /posts
(Get Posts endpoint)- Endpoint is protected by JWT authentication (
Bearer <token>
header is required). - Returns all user's posts.
- Successful response is cached for 5 minutes for the authenticated user. Cache will be invalidated if user adds a new post or deletes a post.
- Endpoint is protected by JWT authentication (
-
DELETE /posts/:id
(Delete Post endpoint)- Endpoint is protected by JWT authentication (
Bearer <token>
header is required). - Deletes a Post by the id that belongs to the authenticated User.
- Endpoint is protected by JWT authentication (
The API endpoint's request parameter and response body details can be seen in the swagger API docs (http://localhost:8000/docs)