Skip to content

Instantly share code, notes, and snippets.

@icedream
Created August 20, 2017 17:44
Show Gist options
  • Save icedream/28107b47dc33c1575d8ee6facc04c2ce to your computer and use it in GitHub Desktop.
Save icedream/28107b47dc33c1575d8ee6facc04c2ce to your computer and use it in GitHub Desktop.
icy thing
package main
import (
"fmt"
"io"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
client := http.DefaultClient
r := mux.NewRouter()
r.HandleFunc("/{protocol}/{hostname}/{mountpoint}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hostname := vars["hostname"]
mountpoint := vars["mountpoint"]
protocol := vars["protocol"]
defer r.Body.Close()
req, err := http.NewRequest("SOURCE", fmt.Sprintf("%s://%s/%s", protocol, hostname, mountpoint), r.Body)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
req.Header = r.Header
resp, err := client.Do(req)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
for key, values := range resp.Header {
for i, value := range values {
if i == 0 {
w.Header().Set(key, value)
} else {
w.Header().Add(key, value)
}
}
}
if resp.StatusCode != 200 {
w.WriteHeader(resp.StatusCode)
}
io.Copy(w, resp.Body)
})
log.Fatal(http.ListenAndServe(":8000", r))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment