Skip to content

Instantly share code, notes, and snippets.

@anatollupacescu
Last active February 22, 2018 10:12
Show Gist options
  • Save anatollupacescu/6d82dc029a6d1960639997da3143c251 to your computer and use it in GitHub Desktop.
Save anatollupacescu/6d82dc029a6d1960639997da3143c251 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
const url = "http://192.168.1.130:8081/data"
func copyHeaders(w http.ResponseWriter, req *http.Request) {
for header, value := range req.Header {
for _, v := range value {
w.Header().Set(header, v)
}
}
}
func fetchData() []byte {
client := &http.Client{}
// uri := req.URL.Path
// log.Printf("Fetching from %s", uri)
req, _ := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
if err != nil {
log.Fatal("Got error", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}
func handler(w http.ResponseWriter, req *http.Request) {
copyHeaders(w, req)
data := fetchData()
w.Write(data)
// fmt.Fprint(w, data)
}
var (
url1 = "http://range"
url2 = "http://pim"
pimPrefix = "/bop"
pimPanda = "/v1"
)
func handleTwoEndpoints(old_url string, new_url string) func(w http.ResponseWriter, req *http.Request) {
return func(resp, req) {
if strings.HasPrefix(req, pimPrefix) {
log.Println(strings.Replace(req, pimPrefix, pimPanda, 1))
} else {
log.Println("nope")
}
}
}
func main() {
old_url := os.Getenv("OLD")
new_url := os.Getenv("NEW")
req := "/bop/current"
if old_url == new_url || len(new_url) == 0 {
http.HandleFunc("/", handlerSingleEndoint(old_url))
} else {
http.HandleFunc("/", handlerTowEndpoints(old_url, new_url))
}
os.Exit(1)
// http.HandleFunc("/", handler)
// log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment