package main import ( "os" "strconv" "strings" "sync" "crypto/rand" "html/template" "log" "net/http" "encoding/json" ) const ( passwordLength = 32 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ) var ( debug = false templates = make(map[string]*template.Template) AppVersion = "development" counterFile = "/data/counter.txt" mu sync.Mutex ) // Diese Funktion wird nur intern aufgerufen, wenn der Mutex bereits gesperrt ist func getCount() int { data, err := os.ReadFile(counterFile) if err != nil { return 0 } count, _ := strconv.Atoi(strings.TrimSpace(string(data))) return count } // Öffentliche Funktion für das Template (mit Lock) func GetPasswordCount() int { mu.Lock() defer mu.Unlock() return getCount() } // Öffentliche Funktion zum Erhöhen (mit Lock) func IncrementPasswordCount() { mu.Lock() defer mu.Unlock() // Wir rufen jetzt die interne Funktion auf, die NICHT versucht, // den Mutex erneut zu sperren count := getCount() count++ os.WriteFile(counterFile, []byte(strconv.Itoa(count)), 0644) } func loadTemplates() { // 1. FuncMap definieren funcMap := template.FuncMap{ "getAppVersion": func() string { return AppVersion }, "getPassCount": func() int { return GetPasswordCount() }, } // 2. Templates mit FuncMap laden // Wir nutzen New("base.html"), da base.html meist das Haupt-Layout definiert templates["index.html"] = template.Must(template.New("base.html").Funcs(funcMap).ParseFiles( "templates/base.html", "templates/index.html", )) templates["help.html"] = template.Must(template.New("base.html").Funcs(funcMap).ParseFiles( "templates/base.html", "templates/help.html", )) log.Printf("Alle Templates erfolgreich geladen") } func generatePassword() string { if debug { log.Printf("called generatePassword\n") } password := make([]byte, passwordLength) _, err := rand.Read(password) if err != nil { log.Fatal(err) } for i := 0; i < passwordLength; i++ { password[i] = chars[int(password[i])%len(chars)] } IncrementPasswordCount() return string(password) } func passwordHandler(w http.ResponseWriter, r *http.Request) { if debug { log.Printf("called passwordHandler\n") } password := generatePassword() currentCount := GetPasswordCount() response := map[string]interface{}{ "password": password, "count": currentCount, } w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(response) if err != nil { log.Printf("Fehler beim Senden des JSON: %v", err) http.Error(w, "Interner Fehler", http.StatusInternalServerError) return } } func passwordAPIHandler(w http.ResponseWriter, r *http.Request) { if debug { log.Printf("called passwordHandler\n") } password := generatePassword() w.Header().Set("Content-Type", "text/plain") w.Write([]byte(password)) } func indexHandler(w http.ResponseWriter, r *http.Request) { if debug { log.Printf("call indexHandler: Request %s %s\n", r.Method, r.URL) } password := generatePassword() //password := "load..." data := struct { Password string }{ Password: password, } if debug { log.Printf("prepare template for index\n") } err := templates["index.html"].ExecuteTemplate(w, "base.html", data) if err != nil { log.Printf("Fehler beim Rendern des Templates: %v", err) http.Error(w, "Interner Serverfehler", http.StatusInternalServerError) } } func helpHandler(w http.ResponseWriter, r *http.Request) { if debug { log.Printf("call helpHandler\n") } err := templates["help.html"].ExecuteTemplate(w, "base.html", nil) if err != nil { log.Printf("Fehler beim Rendern des Templates: %v", err) http.Error(w, "Interner Serverfehler", http.StatusInternalServerError) } } func main() { loadTemplates() fs := http.FileServer(http.Dir("static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) http.HandleFunc("/", indexHandler) http.HandleFunc("/api/password", passwordAPIHandler) http.HandleFunc("/json/password", passwordHandler) http.HandleFunc("/help", helpHandler) log.Println("Server läuft auf http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) }