From 69f1bc8b4ba19da1087a631759fb0c3fe5f7db4a Mon Sep 17 00:00:00 2001 From: Onur Oduncu <160393966+0nur0duncu@users.noreply.github.com> Date: Tue, 21 Jan 2025 14:42:27 +0300 Subject: [PATCH 1/2] Update main.go --- golangService/main.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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) From 3dab80bce97c5ad86543f2dce2c3b4237ab9a567 Mon Sep 17 00:00:00 2001 From: Onur Oduncu <160393966+0nur0duncu@users.noreply.github.com> Date: Tue, 21 Jan 2025 14:43:32 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) 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