This repository was archived by the owner on Jun 30, 2023. It is now read-only.
forked from cvhariharan/SLB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
150 lines (129 loc) · 3.1 KB
/
Copy pathmain.go
File metadata and controls
150 lines (129 loc) · 3.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"reflect"
"strings"
"sync"
"time"
"github.com/cvhariharan/SLB/cfg"
)
var config = cfg.Config{}
var count map[int]int
//Server key is -1
const serverMethod = -1
func proxy(target string, w http.ResponseWriter, r *http.Request) {
url, _ := url.Parse(target)
proxy := httputil.NewSingleHostReverseProxy(url)
r.URL.Host = url.Host
r.URL.Scheme = url.Scheme
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
r.Host = url.Host
proxy.ServeHTTP(w, r)
}
func handle(w http.ResponseWriter, r *http.Request) {
baseURL := r.URL.Path[1:]
baseURL = strings.Split(baseURL, "/")[0]
writeToLog("Basepath: /" + baseURL)
if len(config.Servers) > 0 {
server := chooseServer(config.Servers, serverMethod)
writeToLog("Server: " + server)
proxy(server, w, r)
} else if len(config.Routes) > 0 {
for m := range config.Routes {
route := config.Routes[m].Route
bURL := strings.Split(route, "/")[1]
if baseURL == bURL {
server := chooseServer(config.Routes[m].Endpoints, m)
writeToLog("Route: " + server)
proxy(server, w, r)
}
}
}
}
func chooseServer(servers []string, method int) string {
count[method] = (count[method] + 1) % len(servers)
writeToLog("Chose server: " + servers[count[method]])
return servers[count[method]]
}
func writeToLog(message string) {
logFile, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
logger := log.New(logFile, "", log.LstdFlags)
logger.Println(message)
logFile.Close()
}
//Could be improved but gets the job done
func reloadConfig(configFile string, config chan cfg.Config, wg *sync.WaitGroup) {
var s string
var oldConfig cfg.Config
var t cfg.Config
for {
t = cfg.Parse(configFile)
fmt.Println(reflect.DeepEqual(t, oldConfig))
if !reflect.DeepEqual(t, oldConfig) {
config <- t
fmt.Println("Reloaded")
oldConfig = t
}
fmt.Scanln(&s)
if s == "exit" {
fmt.Println("Closing configChannel")
close(config)
wg.Done()
return
}
}
}
func launch(server *http.Server, wg *sync.WaitGroup) {
writeToLog("Port: " + server.Addr)
writeToLog("Starting server...")
handler := http.HandlerFunc(handle)
server.Handler = handler
server.ListenAndServe()
wg.Done()
}
func main() {
var configFile = "./config.json"
var server *http.Server
var wg sync.WaitGroup
// Adding the reload and exit goroutines
wg.Add(2)
count = make(map[int]int)
configChannel := make(chan cfg.Config)
if len(os.Args) > 1 {
configFile = os.Args[1]
}
go reloadConfig(configFile, configChannel, &wg)
go func() {
for config = range configChannel {
fmt.Println(config)
port := ":" + config.Port
if port == ":" {
port = port + "8080"
}
fmt.Println(server)
if server != nil {
writeToLog("Server closing: " + server.Addr)
fmt.Println("Server closing...")
server.Close()
}
server = &http.Server{
Addr: port,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
wg.Add(1)
go launch(server, &wg)
}
fmt.Println("final")
wg.Done()
}()
wg.Wait()
}