62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"flag"
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed all:static
|
|
var static embed.FS
|
|
|
|
func main() {
|
|
port := flag.Int("port", 8080, "puerto del servidor")
|
|
flag.Parse()
|
|
|
|
staticFS, err := fs.Sub(static, "static")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Proxy a EasyAppointments API
|
|
// El frontend llama a /ea-api/v1/... y aquí se reescribe y se añade auth
|
|
eaTarget := "http://easyappointments:80"
|
|
if t := os.Getenv("EA_TARGET"); t != "" {
|
|
eaTarget = t
|
|
}
|
|
eaURL, err := url.Parse(eaTarget)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
eaProxy := httputil.NewSingleHostReverseProxy(eaURL)
|
|
|
|
http.HandleFunc("/ea-api/", func(w http.ResponseWriter, r *http.Request) {
|
|
// /ea-api/v1/services → /index.php/api/v1/services
|
|
r.URL.Path = strings.Replace(r.URL.Path, "/ea-api", "/index.php/api", 1)
|
|
|
|
// Auth (server-side, nunca se expone al navegador)
|
|
if apiKey := os.Getenv("EA_API_KEY"); apiKey != "" {
|
|
r.Header.Set("Authorization", "Bearer " + apiKey)
|
|
} else if user := os.Getenv("EA_API_USERNAME"); user != "" {
|
|
if pass := os.Getenv("EA_API_PASSWORD"); pass != "" {
|
|
r.SetBasicAuth(user, pass)
|
|
}
|
|
}
|
|
|
|
eaProxy.ServeHTTP(w, r)
|
|
})
|
|
|
|
http.Handle("/", http.FileServer(http.FS(staticFS)))
|
|
|
|
addr := fmt.Sprintf(":%d", *port)
|
|
log.Printf("Servidor iniciado en http://localhost%s", addr)
|
|
log.Fatal(http.ListenAndServe(addr, nil))
|
|
}
|