-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 1.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"loadbalancer/algo"
"loadbalancer/server"
"log"
"net/http"
"net/url"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <algorithm>")
fmt.Println("Available algorithms: round-robin, least-connections, ip-hash")
return
}
algorithm := algo.Algorithm(os.Args[1])
switch algorithm {
case algo.RoundRobin, algo.LeastConnections, algo.IPHash:
default:
fmt.Println("Invalid algorithm. Available algorithms: round-robin, least-connections, ip-hash")
return
}
servers := []string{
"http://localhost:8081",
"http://localhost:8082",
"http://localhost:8083",
}
var serverPool algo.ServerPool
serverPool.Algorithm = algorithm
for _, server := range servers {
url, err := url.Parse(server)
if err != nil {
log.Fatalf("Error parsing server URL: %v", err)
}
serverPool.Servers = append(serverPool.Servers, &algo.Server{URL: url})
}
go server.RunServers()
http.HandleFunc("/", serverPool.LoadBalance)
fmt.Printf("Load Balancer started at :8080 using %s algorithm\n", algorithm)
log.Fatal(http.ListenAndServe(":8080", nil))
}