Rust Load Balancer

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.

Prerequisites

Getting Started

  1. Clone this repository:

    git clone https://github.com/your-username/rust-load-balancer.git
  2. Navigate to the repository directory:

    cd rust-load-balancer
  3. Install dependencies:

    cargo build

Running the Load Balancer

  1. Run the Rust load balancer:
    cargo run

Using the Load Balancer from JavaScript

  1. Navigate to the js-example directory:

    cd js-example
  2. Install dependencies:

    npm install
  3. 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.

Using the Load Balancer from Go

  1. Navigate to the go-example directory:

    cd go-example
  2. 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.

Customizing and Extending

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!

Database Integration

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:

  1. Install Prisma CLI globally:

    npm install -g prisma
  2. Set up the PostgreSQL database and set the DATABASE_URL environment variable.

  3. Generate the Prisma client:

    npx prisma generate

Alternative PostgreSQL Hosting

  • 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.

License

This project is licensed under the MIT License - see the LICENSE file for details.