Skip to content

Commit

Permalink
Add configuration via endpoint and cross compiling scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
rb3ckers committed Feb 15, 2019
1 parent 4e8886e commit c01d8bd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 24 deletions.
2 changes: 2 additions & 0 deletions build-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/bash
env GOOS=linux GOARCH=arm go build
2 changes: 2 additions & 0 deletions build-mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/bash
env GOOS=darwin GOARCH=386 go build
94 changes: 70 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,51 @@ import (
"time"
)

var netClient = &http.Client{
Timeout: time.Second * 10,
}

var mirrors = make(map[string]bool)

func main() {
listenPort := flag.String("port", "7071", "Port to listen on")
proxyTarget := flag.String("main", "http://localhost:7072", "Main proxy target, its responses will be returned")
mirrorsEndpoint := flag.String("mirrors", "mirrors", "Path on which additional mirrors can be added/deleted/listed")

help := flag.Bool("help", false, "Print help")

flag.Parse()

if *help {
fmt.Printf("HTTP proxy that:")
fmt.Printf("* sends requests to a main endpoint from which the response is returned")
fmt.Printf("* can mirror the requests to any additional number of endpoints")
fmt.Printf("")
fmt.Printf("Additional endpoints are configured via POST/DELETE on the `/mirrors?url=<endpoint>`.")

flag.PrintDefaults()
return
}

url, _ := url.Parse(*proxyTarget)

proxyTo := httputil.NewSingleHostReverseProxy(url)

http.HandleFunc("/"+*mirrorsEndpoint, mirrorsHandler)

http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
body := bufferRequest(req)

proxyTo.ServeHTTP(res, req)
go sendToMirrors(req, body)
})

// start server
if err := http.ListenAndServe(":"+*listenPort, nil); err != nil {
panic(err)
}
}

func bufferRequest(req *http.Request) []byte {
// Read body to buffer
body, err := ioutil.ReadAll(req.Body)
Expand All @@ -25,8 +70,10 @@ func bufferRequest(req *http.Request) []byte {
return body
}

var netClient = &http.Client{
Timeout: time.Second * 10,
func sendToMirrors(req *http.Request, body []byte) {
for mirrorURL := range mirrors {
go mirrorTo(mirrorURL, req, body)
}
}

func mirrorTo(targetURL string, req *http.Request, body []byte) {
Expand All @@ -43,33 +90,32 @@ func mirrorTo(targetURL string, req *http.Request, body []byte) {
defer response.Body.Close()
}

func main() {
listenPort := flag.String("port", "7071", "Port to listen on")
proxyTarget := flag.String("main", "http://localhost:7072", "Main proxy target, its responses will be returned")
help := flag.Bool("help", false, "Print help")

flag.Parse()

if *help {
flag.PrintDefaults()
func mirrorsHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodGet {
for url := range mirrors {
fmt.Fprintln(res, url)
}
return
}

url, _ := url.Parse(*proxyTarget)

mirrorURL := "http://localhost:7073"

proxyTo := httputil.NewSingleHostReverseProxy(url)
req.ParseForm()

http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
body := bufferRequest(req)
mirrorURLs, inForm := req.Form["url"]

proxyTo.ServeHTTP(res, req)
go mirrorTo(mirrorURL, req, body)
})
if !inForm {
http.Error(res, "Missing required field: 'url'.", http.StatusBadRequest)
return
}

// start server
if err := http.ListenAndServe(":"+*listenPort, nil); err != nil {
panic(err)
if req.Method == http.MethodPost {
log.Printf("Adding '%s' to mirror list.", mirrorURLs)
for _, url := range mirrorURLs {
mirrors[url] = true
}
} else if req.Method == http.MethodDelete {
log.Printf("Removing '%s' from mirror list.", mirrorURLs)
for _, url := range mirrorURLs {
delete(mirrors, url)
}
}
}

0 comments on commit c01d8bd

Please sign in to comment.