Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b93307f9e | ||
|
|
a029a38787 |
54
main.go
54
main.go
@@ -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,19 +44,14 @@ 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
|
||||
log.Printf("%s - - [%s] \"%s %s %s\" %d %v\n",
|
||||
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 \"%s\"\n",
|
||||
clientIP,
|
||||
time.Now().Format("02/Jan/2006:15:04:05 -0700"),
|
||||
r.Method,
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user