Files

69 lines
1.8 KiB
Go
Raw Permalink Normal View History

2025-03-21 18:47:49 +01:00
package main
/*
Copyright (C) 2023 Steffen Probst
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Entwickelt mit Unterstützung von Claude.ai, einem KI-Assistenten von Anthropic, PBC.
*/
import (
"log"
"net/http"
"time"
)
func main() {
2025-03-31 17:45:12 +02:00
go cacheRefresher()
2025-03-21 18:47:49 +01:00
2025-03-31 17:45:12 +02:00
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", handler)
2025-03-21 18:47:49 +01:00
2025-03-31 17:45:12 +02:00
log.Printf("Server starting on port %s", serverPort)
log.Fatal(http.ListenAndServe(":"+serverPort, nil))
2025-03-21 18:47:49 +01:00
}
2025-03-31 17:45:12 +02:00
func cacheRefresher() {
for {
newData, err := fetchDataFromLDAP()
if err != nil {
log.Printf("Error refreshing cache: %v", err)
} else if !dataEqual(cachedData, newData) {
cachedData = newData
lastUpdate = time.Now()
log.Println("Cache updated")
}
time.Sleep(5 * time.Minute)
2025-03-21 18:47:49 +01:00
}
}
2025-03-31 17:45:12 +02:00
func handler(w http.ResponseWriter, r *http.Request) {
log.Printf("Received request for: %s", r.URL.Path)
2025-03-21 18:47:49 +01:00
2025-03-31 17:45:12 +02:00
if time.Since(lastUpdate) > 5*time.Minute {
newData, err := fetchDataFromLDAP()
if err != nil {
log.Printf("Error refreshing cache: %v", err)
} else {
cachedData = newData
lastUpdate = time.Now()
2025-03-21 18:47:49 +01:00
}
}
2025-03-31 17:45:12 +02:00
err := tmpl.Execute(w, cachedData)
2025-03-21 18:47:49 +01:00
if err != nil {
2025-03-31 17:45:12 +02:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2025-03-21 18:47:49 +01:00
}
2025-03-31 17:52:55 +02:00
}