Compare commits

...

1 Commits

Author SHA1 Message Date
Florian Walther
a029a38787 changed log to combined log format
All checks were successful
Docker Release Build / push_to_registry (push) Successful in 54s
2026-02-08 22:52:51 +01:00

52
main.go
View File

@@ -1,17 +1,17 @@
package main
import (
"os"
"crypto/rand"
"encoding/json"
"html/template"
"log"
"net"
"time"
"net/http"
"os"
"strconv"
"strings"
"sync"
"crypto/rand"
"html/template"
"log"
"net/http"
"encoding/json"
"time"
)
const (
@@ -44,18 +44,13 @@ func newResponseWriter(w http.ResponseWriter) *responseWriter {
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// ResponseWriter einpacken
rw := newResponseWriter(w)
// Den nächsten Handler ausführen
next.ServeHTTP(rw, r)
// Log-Daten sammeln
duration := time.Since(start)
clientIP := getClientIP(r) // Deine Funktion von vorhin
// Format: IP - - [Datum] "Method Path Proto" Status Duration
clientIP := getClientIP(r)
userAgent := r.UserAgent()
// Format: IP - - [Datum] "Method Path Proto" Status Duration User-Agent
// "Combined Log Format"
log.Printf("%s - - [%s] \"%s %s %s\" %d %v\n",
clientIP,
time.Now().Format("02/Jan/2006:15:04:05 -0700"),
@@ -64,6 +59,7 @@ func LoggingMiddleware(next http.Handler) http.Handler {
r.Proto,
rw.statusCode,
duration,
userAgent,
)
})
}
@@ -142,7 +138,9 @@ func loadTemplates() {
}
func generatePassword() string {
if debug { log.Printf("called generatePassword\n") }
if debug {
log.Printf("called generatePassword\n")
}
password := make([]byte, passwordLength)
_, err := rand.Read(password)
if err != nil {
@@ -156,7 +154,9 @@ func generatePassword() string {
}
func passwordHandler(w http.ResponseWriter, r *http.Request) {
if debug { log.Printf("called passwordHandler\n") }
if debug {
log.Printf("called passwordHandler\n")
}
password := generatePassword()
currentCount := GetPasswordCount()
response := map[string]interface{}{
@@ -173,14 +173,18 @@ func passwordHandler(w http.ResponseWriter, r *http.Request) {
}
func passwordAPIHandler(w http.ResponseWriter, r *http.Request) {
if debug { log.Printf("called passwordHandler\n") }
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) }
if debug {
log.Printf("call indexHandler: Request %s %s\n", r.Method, r.URL)
}
password := generatePassword()
//password := "load..."
data := struct {
@@ -188,7 +192,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
}{
Password: password,
}
if debug { log.Printf("prepare template for index\n") }
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)
@@ -197,7 +203,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
}
func helpHandler(w http.ResponseWriter, r *http.Request) {
if debug { log.Printf("call helpHandler\n") }
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)