This project will guide you through creating an API to manage scores for the XOXO game using FastAPI. The project consists of two phases:
- Connecting database(SQLITE) and API
- Creating a React UI
In the XOXO game, we need an API connection to keep track of scores in a table. There are two players in each match. The score submission follows this format:
{
"name": "JOHN",
"status": "WIN"
}
{
"name": "REZA",
"status": "LOSE"
}
- If the status is "WIN", find the player's score in the table and increase it by one.
- If the status is "LOSE", find the player's score in the table and decrease it by one.
For each match, two records will be posted. For example:
- JOHN | 2pt
- REZA | 0pt
Phase 1: Connecting Database and API In this phase, you will create routes to:
- Get the top 10 players by rank.
- Post score and player name.
Phase 2: React and UI To be covered later.
To create a virtual environment for your project, follow these steps:
-
Open your terminal (command prompt, PowerShell, or any terminal you prefer).
-
Create a virtual environment named .venv:
python3 -m venv .venv
-
Activating the Virtual Environment Activate the virtual environment to ensure that any packages you install are local to this project.
- On macOS/Linux:
source .venv/bin/activate
- On Windows:
.venv\Scripts\activate
- On macOS/Linux:
-
You should now see your terminal prompt prefixed with (.venv), indicating that the virtual environment is active.
-
Installing FastAPI and Uvicorn With the virtual environment activated, install FastAPI and Uvicorn using pip:
pip install fastapi uvicorn
-
Running the FastAPI Application Create a file named main.py in your project directory and add the following code: JUST FOR TEST : from fastapi import FastAPI
app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"}
-
To run the FastAPI application, use the following command:
uvicorn main:app --reload
-
visit http://127.0.0.1:8000/docs for Swagger UI