diff --git a/README.md b/README.md index d50c022..10f1f55 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ -# LoadBalancerDemo - ![]("githubimg.png") +# LoadBalancerDemo + +![Load Balancer Architecture](githubimg.png) + +## Overview +This project demonstrates a simple load balancing setup using Nginx as a reverse proxy to distribute traffic across multiple Go service instances. + +## Architecture +- **Nginx**: Acts as a load balancer, distributing incoming requests across three Go service instances +- **Go Services**: Three identical service instances that handle HTTP requests +- **Docker**: Everything is containerized using Docker and orchestrated with Docker Compose + +## Endpoints +- `/`: Welcome page showing server ID +- `/getResult`: Returns a message with the current server ID + +## Setup and Running +1. Make sure you have Docker and Docker Compose installed +2. Clone this repository +3. Run the following command: + ```bash + docker-compose up --build -d + ``` + +## Project Structure +- `nginx/`: Contains Nginx configuration and Dockerfile +- `golangService/`: Contains Go service code and Dockerfile +- `docker-compose.yml`: Defines the multi-container application setup diff --git a/golangService/main.go b/golangService/main.go index 2e22f1c..2d49b72 100644 --- a/golangService/main.go +++ b/golangService/main.go @@ -7,13 +7,23 @@ import ( "os" ) -func Handler(w http.ResponseWriter, r *http.Request) { +func getResultHandler(w http.ResponseWriter, r *http.Request) { ServerID := os.Getenv("SERVERID") io.WriteString(w, "msg from server"+ServerID) } +func rootHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + ServerID := os.Getenv("SERVERID") + io.WriteString(w, "Welcome to server "+ServerID) +} + func main() { - http.HandleFunc("/getResult", Handler) + http.HandleFunc("/", rootHandler) + http.HandleFunc("/getResult", getResultHandler) if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err)