This repository demonstrates a load balancer implemented in Rust using the Actix web framework. The load balancer supports different algorithms and can be integrated into JavaScript and Go applications.
- Rust (Cargo) installed. Install Rust
- Node.js installed for JavaScript example. Install Node.js
- Go installed for Go example. Install Go
- PostgreSQL installed for database operations. Install PostgreSQL
-
Clone this repository:
git clone https://github.com/your-username/rust-load-balancer.git
-
Navigate to the repository directory:
cd rust-load-balancer
-
Install dependencies:
cargo build
- Run the Rust load balancer:
cargo run
-
Navigate to the
js-example
directory:cd js-example
-
Install dependencies:
npm install
-
Run the JavaScript example:
node index.js
Example JavaScript code (js-example/index.js
):
const axios = require('axios');
async function requestToLoadBalancer() {
const response = await axios.get('http://localhost:8080');
console.log(response.data);
}
requestToLoadBalancer();
The JavaScript example sends requests to the Rust load balancer.
-
Navigate to the
go-example
directory:cd go-example
-
Run the Go example:
go run main.go
Example Go code (go-example/main.go
):
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func requestToLoadBalancer() {
response, err := http.Get("http://localhost:8080")
if err != nil {
fmt.Println("Error:", err)
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(string(body))
}
func main() {
requestToLoadBalancer()
}
The Go example interacts with the Rust load balancer.
The load balancer supports various algorithms and integrates with a Prisma-based database for caching. You can modify the load balancer's behavior, algorithms, or integrations according to your needs. Contributions are welcome!
The load balancer supports caching using a Prisma-based PostgreSQL database. The schema is defined in the Prisma schema file (schema.prisma
). To set up the database and generate the Prisma client, follow these steps:
-
Install Prisma CLI globally:
npm install -g prisma
-
Set up the PostgreSQL database and set the
DATABASE_URL
environment variable. -
Generate the Prisma client:
npx prisma generate
-
Railway: Railway is a platform to deploy and manage databases. You can easily host a PostgreSQL database on Railway and obtain the connection URL.
-
Supabase: Supabase is an open-source alternative to Firebase with a built-in PostgreSQL database. You can create a project on Supabase and obtain the connection URL.
This project is licensed under the MIT License - see the LICENSE file for details.