-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (72 loc) · 2.1 KB
/
main.go
File metadata and controls
87 lines (72 loc) · 2.1 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
redisDB "Load_Balancer_Server/services"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
)
type LoadBalancer struct {
Port string
}
func (lb *LoadBalancer) serveProxy(rw http.ResponseWriter, r *http.Request) {
var domainKey string
if r.Header.Get("X-Domain-Name") != "" {
domainKey = r.Header.Get("X-Domain-Name") + "_Backend"
fmt.Printf("Domain : %s Backend", domainKey)
} else if r.Host != "" {
domainKey = r.Host + "_Frontend"
fmt.Printf("Domain : %s Frontend", domainKey)
} else {
http.Error(rw, "Domain not provided", http.StatusBadRequest)
return
}
// Fetch the optimal server
optimalServer, err := redisDB.GetOptimalServer(domainKey)
if err != nil {
http.Error(rw, "Failed to fetch optimal server", http.StatusInternalServerError)
return
}
fmt.Printf("Optimal server: %s\n", optimalServer)
// Ensure the URL includes the scheme
if !urlHasScheme(optimalServer) {
optimalServer = "http://" + optimalServer
}
// Parse the server URL
serverURL, err := url.Parse(optimalServer)
if err != nil {
http.Error(rw, "Invalid server URL", http.StatusInternalServerError)
return
}
// Create the reverse proxy
proxy := httputil.NewSingleHostReverseProxy(serverURL)
proxy.ServeHTTP(rw, r)
newVal, err := redisDB.UpdateActiveCount(domainKey, optimalServer, 1)
if err != nil {
http.Error(rw, "Failed to Update active count", http.StatusInternalServerError)
return
}
fmt.Printf("Host server : %s\nUpdated Value: %f", r.Host, newVal)
defer redisDB.UpdateActiveCount(domainKey, optimalServer, -1)
}
// Helper function to check if a URL includes a scheme
func urlHasScheme(urlStr string) bool {
return len(urlStr) > 0 && (urlStr[:7] == "http://" || urlStr[:8] == "https://")
}
var lb *LoadBalancer
func InitLoadBalancer() {
lb = &LoadBalancer{Port: "5000"}
}
func redirectRequest(rw http.ResponseWriter, r *http.Request) {
lb.serveProxy(rw, r)
}
func main() {
fmt.Println("Starting Load Balancer...")
redisDB.InitRedisClient()
InitLoadBalancer()
http.HandleFunc("/", redirectRequest)
err := http.ListenAndServe(":"+lb.Port, nil)
if err != nil {
panic(err)
}
}