added password counter
All checks were successful
Docker Release Build / push_to_registry (push) Successful in 58s
All checks were successful
Docker Release Build / push_to_registry (push) Successful in 58s
This commit is contained in:
72
main.go
72
main.go
@@ -1,10 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"crypto/rand"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -12,15 +17,47 @@ const (
|
||||
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
)
|
||||
|
||||
var templates = make(map[string]*template.Template)
|
||||
var AppVersion = "development"
|
||||
var (
|
||||
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
|
||||
},
|
||||
"getAppVersion": func() string { return AppVersion },
|
||||
"getPassCount": func() int { return GetPasswordCount() },
|
||||
}
|
||||
|
||||
// 2. Templates mit FuncMap laden
|
||||
@@ -39,6 +76,7 @@ func loadTemplates() {
|
||||
}
|
||||
|
||||
func generatePassword() string {
|
||||
log.Printf("called generatePassword\n")
|
||||
password := make([]byte, passwordLength)
|
||||
_, err := rand.Read(password)
|
||||
if err != nil {
|
||||
@@ -47,10 +85,29 @@ func generatePassword() string {
|
||||
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) {
|
||||
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) {
|
||||
log.Printf("called passwordHandler\n")
|
||||
password := generatePassword()
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write([]byte(password))
|
||||
@@ -59,11 +116,13 @@ func passwordHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("call indexHandler: Request %s %s\n", r.Method, r.URL)
|
||||
password := generatePassword()
|
||||
//password := "load..."
|
||||
data := struct {
|
||||
Password string
|
||||
}{
|
||||
Password: password,
|
||||
}
|
||||
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)
|
||||
@@ -86,7 +145,8 @@ func main() {
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
|
||||
http.HandleFunc("/", indexHandler)
|
||||
http.HandleFunc("/api/password", passwordHandler)
|
||||
http.HandleFunc("/api/password", passwordAPIHandler)
|
||||
http.HandleFunc("/json/password", passwordHandler)
|
||||
http.HandleFunc("/help", helpHandler)
|
||||
|
||||
log.Println("Server läuft auf http://localhost:8080")
|
||||
|
||||
Reference in New Issue
Block a user