package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"

	"git.milar.in/milarin/adverr"
	"git.milar.in/milarin/ezhttp"
)

var BaseURL = adverr.Must(url.Parse("http://192.168.1.4:30124"))

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)
}