First commit
This commit is contained in:
53
app/web/main.go
Normal file
53
app/web/main.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_ = godotenv.Load(".env") // Load .env file
|
||||
|
||||
valhallaURL := os.Getenv("VALHALLA_URL")
|
||||
if valhallaURL == "" {
|
||||
valhallaURL = "http://10.200.0.15:8002/route"
|
||||
}
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
|
||||
http.HandleFunc("/route", func(w http.ResponseWriter, r *http.Request) {
|
||||
proxyToValhalla(w, r, valhallaURL)
|
||||
})
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
http.ServeFile(w, r, "./static/index.html")
|
||||
return
|
||||
}
|
||||
http.ServeFile(w, r, "./static/"+r.URL.Path[1:])
|
||||
})
|
||||
log.Printf("Listening on :%s", port)
|
||||
http.ListenAndServe(":"+port, nil)
|
||||
}
|
||||
|
||||
func proxyToValhalla(w http.ResponseWriter, r *http.Request, valhallaURL string) {
|
||||
req, _ := http.NewRequest("POST", valhallaURL, r.Body)
|
||||
req.Header = r.Header
|
||||
|
||||
client := http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
|
||||
io.Copy(w, resp.Body)
|
||||
}
|
||||
Reference in New Issue
Block a user