-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (49 loc) · 1.4 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
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
)
// LoadBalancer struct holds information about the backend servers
type LoadBalancer struct {
servers []string
current int
}
// NewLoadBalancer creates a new instance of LoadBalancer
func NewLoadBalancer(servers []string) *LoadBalancer {
return &LoadBalancer{
servers: servers,
current: 0,
}
}
// NextServer selects the next server in a round-robin fashion
func (lb *LoadBalancer) NextServer() string {
server := lb.servers[lb.current]
lb.current = (lb.current + 1) % len(lb.servers)
return server
}
// Handler function to handle incoming requests
func (lb *LoadBalancer) Handler(w http.ResponseWriter, r *http.Request) {
selectedServer := lb.NextServer()
fmt.Fprintf(w, "Request routed to server: %s\n", selectedServer)
}
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
// Read servers from environment variable
serversString := os.Getenv("SERVERS")
servers := strings.Split(serversString, ",")
// Create a new instance of the load balancer
loadBalancer := NewLoadBalancer(servers)
// Set up the HTTP server to handle incoming requests
http.HandleFunc("/", loadBalancer.Handler)
fmt.Println("Load balancer running on port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println(err)
}
}