@jajoosam/

simple load-balancer

Go

simple round-robin load-balancer

fork
loading
Files
  • main.go
  • go.mod

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4797377445644515","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImxvZyIKCSJuZXQvaHR0cCIKCSJuZXQvaHR0cC9odHRwdXRpbCIKCSJuZXQvdXJsIgopCgpmdW5jIG1haW4oKSB7CgliYWNrZW5kcyA6PSBbXXN0cmluZ3sKCQkiaHR0cHM6Ly9seXJpY3MtcmlwLTEuamFqb29zYW0ucmVwbC5jbyIsCiAgICAiaHR0cHM6Ly9seXJpY3MtcmlwLTIuamFqb29zYW0ucmVwbC5jbyIsCiAgICAiaHR0cHM6Ly9seXJpY3MtcmlwLTMuamFqb29zYW0ucmVwbC5jbyIsCiAgICAiaHR0cHM6Ly9seXJpY3MtcmlwLTQuamFqb29zYW0ucmVwbC5jbyIsCiAgICAiaHR0cHM6Ly9seXJpY3MtcmlwLTUuamFqb29zYW0ucmVwbC5jbyIsCgl9CgoJaSA6PSAtMQoKCWRpcmVjdG9yIDo9IGZ1bmMocmVxICpodHRwLlJlcXVlc3QpIHsKCQlpID0gKGkgKyAxKSAlIGxlbihiYWNrZW5kcykKCQlvcmlnaW4sIF8gOj0gdXJsLlBhcnNlKGJhY2tlbmRzW2ldKQoJCWxvZy5QcmludGxuKCJnb2luZyB0byIsIG9yaWdpbi5Ib3N0KQoKCQlyZXEuVVJMLlNjaGVtZSA9ICJodHRwcyIKCQlyZXEuVVJMLkhvc3QgPSBvcmlnaW4uSG9zdAoJCXJlcS5Ib3N0ID0gb3JpZ2luLkhvc3QKCX0KCglwcm94eSA6PSAmaHR0cHV0aWwuUmV2ZXJzZVByb3h5e0RpcmVjdG9yOiBkaXJlY3Rvcn0KCglodHRwLkhhbmRsZUZ1bmMoIi8iLCBmdW5jKHcgaHR0cC5SZXNwb25zZVdyaXRlciwgciAqaHR0cC5SZXF1ZXN0KSB7CgkJcHJveHkuU2VydmVIVFRQKHcsIHIpCgl9KQoKCWxvZy5GYXRhbChodHRwLkxpc3RlbkFuZFNlcnZlKCI6OTAwMSIsIG5pbCkpCn0K"},"asBuffer":null},"loaded":true}}
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
package main

import (
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
)

func main() {
	backends := []string{
		"https://lyrics-rip-1.jajoosam.repl.co",
    "https://lyrics-rip-2.jajoosam.repl.co",
    "https://lyrics-rip-3.jajoosam.repl.co",
    "https://lyrics-rip-4.jajoosam.repl.co",
    "https://lyrics-rip-5.jajoosam.repl.co",
	}

	i := -1

	director := func(req *http.Request) {
		i = (i + 1) % len(backends)
		origin, _ := url.Parse(backends[i])
		log.Println("going to", origin.Host)

		req.URL.Scheme = "https"
		req.URL.Host = origin.Host
		req.Host = origin.Host
	}

	proxy := &httputil.ReverseProxy{Director: director}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		proxy.ServeHTTP(w, r)
	})

	log.Fatal(http.ListenAndServe(":9001", nil))
}