diff --git a/main.go b/main.go index 66c7e0a..825b83e 100644 --- a/main.go +++ b/main.go @@ -14,28 +14,29 @@ var serverConfig = map[string]string{ "serverURL": os.Getenv("SERVER_URL"), } -var secret = os.Getenv("SECRET") - func main() { port := os.Getenv("PORT") if port == "" { port = "80" } - // Set up routes - http.HandleFunc("/", handleRoot) - http.HandleFunc("/config", handleConfig) - http.HandleFunc("/login", handleLogin) - http.HandleFunc("/search", handleSearch) - http.HandleFunc("/favicon.ico", handleFavicon) + // HTTP -> HTTPS Weiterleitung + go func() { + log.Fatal(http.ListenAndServe(":80", http.HandlerFunc(redirectToHTTPS))) + }() + + // Hauptserver für den Reverse Proxy + mux := http.NewServeMux() + mux.HandleFunc("/", handleRoot) + mux.HandleFunc("/config", handleConfig) + mux.HandleFunc("/search", handleSearch) + mux.HandleFunc("/favicon.ico", handleFavicon) - // Serve static files from 'public' directory fs := http.FileServer(http.Dir("./public")) - http.Handle("/public/", http.StripPrefix("/public/", fs)) + mux.Handle("/public/", http.StripPrefix("/public/", fs)) - // Start the server - log.Printf("Server is running at http://0.0.0.0:%s\n", port) - log.Fatal(http.ListenAndServe("0.0.0.0:"+port, nil)) + log.Printf("Server läuft auf Port %s (hinter Reverse Proxy)", port) + log.Fatal(http.ListenAndServe(":"+port, mux)) } func handleRoot(w http.ResponseWriter, r *http.Request) { @@ -50,30 +51,6 @@ func handleConfig(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, `{"serverIP":"%s","serverURL":"%s"}`, serverConfig["serverIP"], serverConfig["serverURL"]) } -func handleLogin(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodPost { - username := r.FormValue("username") - password := r.FormValue("password") - - if username == os.Getenv("USER") && password == os.Getenv("PASSWORD") { - http.Redirect(w, r, "/stats", http.StatusSeeOther) - } else { - http.Error(w, "Ungültiges Passwort oder Benutzername", http.StatusForbidden) - } - } else { - // Show login form - tmpl := ` -
` - fmt.Fprint(w, tmpl) - } -} - func handleSearch(w http.ResponseWriter, r *http.Request) { query := r.URL.Query().Get("query") websites := r.URL.Query()["websites"] @@ -101,6 +78,13 @@ func handleFavicon(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } +func redirectToHTTPS(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Forwarded-Proto") != "https" { + target := "https://" + r.Host + r.URL.RequestURI() + http.Redirect(w, r, target, http.StatusMovedPermanently) + } +} + func generateSearchUrl(website, query string) string { searchUrls := map[string]string{ // ----- Eletronik ----