53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package routes
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
func Root (w http.ResponseWriter, r *http.Request){
|
|
|
|
if os.Getenv("ENV") == "dev" {
|
|
// proxy reverso al server de front
|
|
proxyReq, err := http.NewRequest(r.Method, "http://localhost:5173" + r.URL.Path, r.Body)
|
|
if err != nil {
|
|
http.Error(w, "Proxy error: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// copiar headers de request
|
|
for key, values := range r.Header {
|
|
for _, value := range values {
|
|
proxyReq.Header.Add(key, value)
|
|
}
|
|
}
|
|
|
|
// hacer request al front
|
|
client := &http.Client{}
|
|
resp, err := client.Do(proxyReq)
|
|
if err != nil {
|
|
http.Error(w, "Proxy error: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// copiar header de respuesta
|
|
for key, values := range resp.Header {
|
|
for _, value := range values {
|
|
w.Header().Add(key, value)
|
|
}
|
|
}
|
|
|
|
// Copiar codigo de estado y cuerpo
|
|
w.WriteHeader(resp.StatusCode)
|
|
_, err = io.Copy(w, resp.Body)
|
|
if err != nil {
|
|
http.Error(w, "Proxy error: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
} else {
|
|
http.ServeFile(w, r, "index.html")
|
|
}
|
|
}
|