yt-remote-control/api.go

35 lines
699 B
Go
Raw Permalink Normal View History

2024-09-15 14:16:45 +02:00
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"git.milar.in/milarin/adverr"
"git.milar.in/milarin/ezhttp"
)
2024-10-10 15:23:31 +02:00
var BaseURL = adverr.Must(url.Parse("http://192.168.1.4:30124"))
2024-09-15 14:16:45 +02:00
func ProxyRequest(w http.ResponseWriter, r *http.Request) {
req := ezhttp.Request(
ezhttp.Method(r.Method),
ezhttp.URL(BaseURL.JoinPath(r.URL.Path).String()),
ezhttp.Body(ezhttp.Reader(r.Body)),
ezhttp.ContentType("application/json"),
)
resp, err := ezhttp.Do(req)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer resp.Body.Close()
w.WriteHeader(resp.StatusCode)
w.Header().Add("Content-Type", "application/json")
io.Copy(w, resp.Body)
}