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