@jajoosam/

simple load-balancer

Go

simple round-robin load-balancer

fork
loading
Files
  • main.go
  • go.mod
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
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))
}