69 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 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() {
 | |
| 	go cacheRefresher()
 | |
| 
 | |
| 	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
 | |
| 	http.HandleFunc("/", handler)
 | |
| 
 | |
| 	log.Printf("Server starting on port %s", serverPort)
 | |
| 	log.Fatal(http.ListenAndServe(":"+serverPort, nil))
 | |
| }
 | |
| 
 | |
| 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)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func handler(w http.ResponseWriter, r *http.Request) {
 | |
| 	log.Printf("Received request for: %s", r.URL.Path)
 | |
| 
 | |
| 	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()
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	err := tmpl.Execute(w, cachedData)
 | |
| 	if err != nil {
 | |
| 		http.Error(w, err.Error(), http.StatusInternalServerError)
 | |
| 	}
 | |
| } |