Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 218 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,227 @@
# Chess App

## Overview
This project is a web-based chess application with a JavaScript frontend and a Rust backend. The frontend displays the chessboard and handles the game logic, while the backend serves the JavaScript files via static HTML and stores all player games and moves in a PostgreSQL database.
This project is a web-based chess application with a **JavaScript frontend** and a **Rust backend**. The frontend displays the chessboard and handles game logic, while the backend serves JavaScript files via static HTML and stores all player games and moves in a **PostgreSQL** database.

## Frontend
---
## 📌 Clone the Repository
```bash
git clone git@github.com:timwhite06/chess-rust-and-javascript.git
cd chess-rust-and-javascript
```

---
## 🔧 Setup Instructions

### ✅ Prerequisites

#### 1️⃣ **Docker Desktop**
Install [Docker Desktop](https://www.docker.com/products/docker-desktop) and ensure it is running in **Linux container mode** (right-click the Docker icon in the system tray and select *"Switch to Linux containers..."* if needed).

#### 2️⃣ **Node.js & npm**
Install [Node.js](https://nodejs.org/) (which includes npm) for managing frontend dependencies.

#### 3️⃣ **Rust Toolchain**
Install Rust via [rustup](https://rustup.rs/).

#### 4️⃣ **SQLx CLI**
To run database migrations using SQLx, install the CLI:
```bash
cargo install sqlx-cli --no-default-features --features postgres
```

---

## Setting Up DBeaver for PostgreSQL

This guide will walk you through the process of setting up DBeaver to connect to your PostgreSQL database for the project.

### 1. Download and Install DBeaver

- **Download:**
Visit the [DBeaver Community Edition website](https://dbeaver.io/download/) and download the installer for your operating system.

- **Install:**
Follow the installation instructions specific to your OS.

### 2. Create a New PostgreSQL Connection

1. **Open DBeaver.**

2. **Start a New Connection:**
- Click on **Database** in the menu and select **New Database Connection**.
- Alternatively, click the **New Connection** button in the toolbar.

3. **Select Database Type:**
- In the "Connect to a database" dialog, scroll through the list and select **PostgreSQL**.
- Click **Next**.

### 3. Configure Connection Settings

You need to provide the correct JDBC URL and credentials to connect to your database.

#### Option A: Use the Connection Fields

- **Host:** `localhost`
- **Port:** `5432`
- **Database:** `chess-rust-javascript`
- **Username:** `admin`
- **Password:** `admin`

DBeaver will automatically construct the JDBC URL for you when these fields are filled out.

#### Option B: Use a Custom JDBC URL

If you prefer to use a JDBC URL, use the following format:
```
jdbc:postgresql://localhost:5432/chess-rust-javascript?user=admin&password=admin
```
- Enter this URL into the **URL** field.
- You can leave the username and password fields blank if they are specified in the URL, or fill them in as above.

### 4. Test the Connection

- Click the **Test Connection** button.
- If prompted for a driver download, allow DBeaver to download the PostgreSQL JDBC driver.
- You should see a message indicating the connection was successful.

### 5. Finish and Save the Connection

- Once the connection test is successful, click **Finish**.
- Your new connection will appear in the **Database Navigator** panel on the left.

### 6. Refresh the Schema and Verify Tables

- **Refresh:**
Right-click on your connection or the `public` schema and choose **Refresh** or **Reload**.

- **Verify Tables:**
Open an SQL Editor in DBeaver and run:
```sql
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';

---
## 🚀 Running the Project

### 1️⃣ Start the Database
Ensure Docker is running, then execute:
<!-- ```bash
npm run docker:up
``` -->

Verify database connection:

```bash
docker exec -it $(docker ps -q -f name=postgres) psql -U admin -d chess-rust-javascript -c "\dt"
```


**Expected Output:**

List of relations
```
Schema | Name | Type | Owner
-------+--------+-------+-------
public | games | table | admin
public | moves | table | admin
(2 rows)
```
If tables are missing, proceed with migrations.

---
### 2️⃣ Run Database Migrations
Ensure `DATABASE_URL` is set:
```bash
export DATABASE_URL="postgres://admin:admin@localhost:5432/chess-rust-javascript"
```
For Windows PowerShell:
```powershell
$env:DATABASE_URL="postgres://admin:admin@localhost:5432/chess-rust-javascript"
```
Run migrations:
```bash
sqlx migrate run
```
Verify migrations:
```bash
sqlx migrate info
```

---
### 3️⃣ Build the Backend
```bash
npm run build:backend
```

### 4️⃣ Run the Project
```bash
npm run serve
```

---
## 🏗️ Project Structure

### 🎨 Frontend
- **Language:** JavaScript
- **Description:** The frontend is responsible for rendering the chessboard, handling user interactions, and implementing the game logic. It follows an object-oriented design.
- **Files:**
- `index.html`: The main HTML file that includes the chessboard container.
- `css/styles.css`: The stylesheet for the application.
- `scripts/main.js`: The main JavaScript file that initializes the game.
- `scripts/board.js`: Handles the chessboard rendering.
- `scripts/game.js`: Manages the game state.
- `scripts/moveLogic.js`: Contains the logic for validating and executing moves.
- `scripts/piece.js`: Defines the `Piece` class.

## Backend
- **Description:** Handles rendering the chessboard, user interactions, and game logic.
- **Key Files:**
- `index.html` – Main HTML file with the chessboard container.
- `css/styles.css` – Stylesheet for UI.
- `scripts/main.js` – Initializes the game.
- `scripts/board.js` – Manages chessboard rendering.
- `scripts/game.js` – Controls game state.
- `scripts/moveLogic.js` – Implements move validation and execution.
- `scripts/piece.js`Defines chess pieces.

### 🔧 Backend
- **Language:** Rust
- **Description:** The backend is a web server that hosts the frontend files and interacts with a PostgreSQL database to store game data.
- **Database:** PostgreSQL
- **Functionality:**
- Serves static HTML, CSS, and JavaScript files.
- Supports WebSocket connections for real-time communication.
- Stores player games and moves in the database.

## Libraries Used
The backend uses the following Rust libraries to provide functionality:

1. **Axum**
- **Purpose:** A modern, ergonomic web framework for building APIs and web servers.
- **Usage in Project:** Used to handle HTTP routes and serve static files.
- **Why Axum:** Its simplicity and compatibility with async programming make it ideal for building performant and scalable applications.

2. **Tower HTTP**
- **Purpose:** Provides middleware and utilities for web servers.
- **Usage in Project:** Used to serve static files (HTML, CSS, JS) from a directory on the server.
- **Why Tower HTTP:** It integrates seamlessly with Axum for static file serving and other HTTP-specific tasks.

3. **Tokio**
- **Purpose:** An asynchronous runtime for Rust.
- **Usage in Project:** Powers asynchronous operations, such as handling multiple HTTP requests and WebSocket connections concurrently.
- **Why Tokio:** It’s fast, reliable, and widely used in the Rust ecosystem for async programming.

4. **SQLx**
- **Purpose:** A Rust library for interacting with databases.
- **Usage in Project:** Used to connect to the PostgreSQL database and execute SQL queries for storing and retrieving game data.
- **Why SQLx:** It supports async queries, has compile-time query validation, and integrates well with Rust.

5. **Tracing**
- **Purpose:** A structured logging and diagnostics library for Rust.
- **Usage in Project:** Provides detailed logs for debugging and monitoring application behavior.
- **Why Tracing:** It enables rich, structured logs that help identify issues during development and production.

6. **Tracing Subscriber**
- **Purpose:** A library that processes and outputs tracing data.
- **Usage in Project:** Configures how logs are displayed in the application.
- **Why Tracing Subscriber:** It works in tandem with tracing to provide a flexible logging setup.

## Why Async is Important
Asynchronous programming is crucial for this project because it allows the backend to handle multiple tasks concurrently without blocking the execution of other tasks. This is particularly important for:
- Handling multiple HTTP requests simultaneously, ensuring that the server remains responsive even under heavy load.
- Managing WebSocket connections for real-time communication between the frontend and backend.
- Performing database operations without blocking the main thread, which improves the overall performance and scalability of the application.

## Usage
- **Features:**
- Serves static files (HTML, CSS, JS).
- Supports WebSockets for real-time communication.
- Stores games and moves in the database.

---
## 📚 Rust Libraries Used

### 🚀 Actix Web
- **Purpose:** Fast web framework for handling HTTP requests.
- **Usage:** Serves frontend files and API routes.

### ⚡ Actix RT
- **Purpose:** Async runtime for handling multiple tasks.
- **Usage:** Runs non-blocking operations like WebSockets and database queries.

### 🔄 Actix Web Actors
- **Purpose:** Enables WebSocket connections.
- **Usage:** Handles real-time chess game updates.

### 📂 Actix Files
- **Purpose:** Middleware for serving static files.
- **Usage:** Serves frontend files efficiently.

### 🗄️ SQLx
- **Purpose:** Async SQL library for Rust.
- **Usage:** Executes database queries and migrations.

### 📊 Tracing & Tracing Subscriber
- **Purpose:** Structured logging and diagnostics.
- **Usage:** Logs application activity for debugging and monitoring.

---
## 🔄 Why Async is Important?
Asynchronous programming improves performance by allowing the backend to handle:
✅ Multiple HTTP requests concurrently.
✅ WebSocket connections for real-time communication.
✅ Database operations without blocking execution.

---
## 🎮 Usage
- Open the application in a web browser.
- Play chess by interacting with the chessboard.
- The backend will store all game data in the PostgreSQL database.
- The backend stores all game data in PostgreSQL.

## License
This project is licensed under the MIT License.
---
## 📜 License
This project is licensed under the **MIT License**.
1 change: 1 addition & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL="postgres://admin:admin@localhost:5432/chess-rust-javascript"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading