51 lines
1.4 KiB
Go
51 lines
1.4 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()
|
|
|
|
fs := http.FileServer(http.Dir("./static"))
|
|
http.Handle("/", fs)
|
|
|
|
http.HandleFunc("/api/config", handleConfig)
|
|
http.HandleFunc("/api/start", handleStart)
|
|
http.HandleFunc("/api/validate", handleValidate)
|
|
http.HandleFunc("/api/submit-name", handleSubmitName)
|
|
http.HandleFunc("/api/leaderboard", handleLeaderboard)
|
|
http.HandleFunc("/api/claim/delete", handleClaimDelete)
|
|
|
|
http.HandleFunc("/admin", BasicAuth(handleAdminPage))
|
|
http.HandleFunc("/api/admin/list", BasicAuth(handleAdminList))
|
|
http.HandleFunc("/api/admin/action", BasicAuth(handleAdminAction))
|
|
|
|
log.Println("🦖 Server läuft auf :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|