55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func BasicAuth(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
user, pass, ok := r.BasicAuth()
|
|
if !ok || user != adminUser || pass != adminPass {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="Lehrerzimmer"`)
|
|
http.Error(w, "Unauthorized", 401)
|
|
return
|
|
}
|
|
next(w, r)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
redisAddr := getEnv("REDIS_ADDR", "localhost:6379")
|
|
adminUser = getEnv("ADMIN_USER", "lehrer")
|
|
adminPass = getEnv("ADMIN_PASS", "geheim123")
|
|
|
|
rdb = redis.NewClient(&redis.Options{Addr: redisAddr})
|
|
if _, err := rdb.Ping(ctx).Result(); err != nil {
|
|
log.Fatal("Redis:", err)
|
|
}
|
|
|
|
initGameConfig()
|
|
initBadWords()
|
|
|
|
fs := http.FileServer(http.Dir("./static"))
|
|
http.Handle("/", fs)
|
|
|
|
// API Routes (jetzt mit Logger!)
|
|
http.HandleFunc("/api/config", Logger(handleConfig))
|
|
http.HandleFunc("/api/start", Logger(handleStart))
|
|
http.HandleFunc("/api/validate", Logger(handleValidate))
|
|
http.HandleFunc("/api/submit-name", Logger(handleSubmitName))
|
|
http.HandleFunc("/api/leaderboard", Logger(handleLeaderboard))
|
|
http.HandleFunc("/api/claim/delete", Logger(handleClaimDelete))
|
|
|
|
// Admin Routes (Logger + BasicAuth kombinieren)
|
|
http.HandleFunc("/admin", Logger(BasicAuth(handleAdminPage)))
|
|
http.HandleFunc("/api/admin/badwords", Logger(BasicAuth(handleAdminBadwords)))
|
|
http.HandleFunc("/api/admin/list", Logger(BasicAuth(handleAdminList)))
|
|
http.HandleFunc("/api/admin/action", Logger(BasicAuth(handleAdminAction)))
|
|
|
|
log.Println("🦖 Server läuft auf :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|