Add files via upload

This commit is contained in:
Tomáš Dvořák
2025-05-23 10:47:33 +02:00
committed by GitHub
parent 6d677b78d3
commit 95dc182b85
+62 -80
View File
@@ -38,7 +38,7 @@ var (
) )
func startAutoReload() { func startAutoReload() {
ticker := time.NewTicker(3 * 24 * time.Hour) ticker := time.NewTicker(1 * time.Hour)
quit := make(chan struct{}) quit := make(chan struct{})
go func() { go func() {
for { for {
@@ -69,19 +69,7 @@ func main() {
// Set up HTTP handlers // Set up HTTP handlers
http.HandleFunc("/", serveIndex) http.HandleFunc("/", serveIndex)
http.HandleFunc("/contacts", serveContacts) http.HandleFunc("/contacts", serveContacts)
http.HandleFunc("/reload", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/reload", reloadData)
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
log.Println("Manual reload requested")
reloadData()
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"status": "reloaded", "contacts_count": %d}`,
len(currentData.Contacts)+len(currentData.InternalContacts))
})
// Start server // Start server
port := os.Getenv("PORT") port := os.Getenv("PORT")
@@ -95,29 +83,58 @@ func main() {
} }
func loadData() { func loadData() {
// Check file every 5 minutes // Check if Excel file exists
ticker := time.NewTicker(1 * time.Hour) if _, err := os.Stat(xlsxFile); os.IsNotExist(err) {
go func() { log.Printf("Excel file %s not found, using empty data", xlsxFile)
for range ticker.C { currentData = &ContactData{
checkFileAndReload() Contacts: []Contact{},
InternalContacts: []Contact{},
LastUpdated: time.Now(),
FileHash: "",
} }
}()
// Initial load
checkFileAndReload()
}
func checkFileAndReload() {
currentHash, err := calculateFileHash(xlsxFile)
if err != nil {
log.Printf("Hash check error: %v", err)
return return
} }
if currentHash != currentData.FileHash { // Calculate current file hash
log.Println("Detected file changes - reloading data") currentHash, err := calculateFileHash(xlsxFile)
reloadData() if err != nil {
log.Printf("Error calculating file hash: %v", err)
return
} }
// Check if cached data exists and is up to date
if cachedData, err := loadCachedData(); err == nil {
if cachedData.FileHash == currentHash {
log.Println("Using cached data (file unchanged)")
currentData = cachedData
return
}
}
// Parse Excel file
log.Println("Parsing Excel file...")
contacts, err := parseExcelFile(xlsxFile)
if err != nil {
log.Printf("Error parsing Excel file: %v", err)
// Use empty data if parsing fails
currentData = &ContactData{
Contacts: []Contact{},
InternalContacts: []Contact{},
LastUpdated: time.Now(),
FileHash: currentHash,
}
return
}
currentData = processContacts(contacts)
currentData.FileHash = currentHash
// Save to cache
if err := saveCachedData(currentData); err != nil {
log.Printf("Warning: Could not save cached data: %v", err)
}
log.Printf("Loaded %d contacts from Excel file", len(currentData.Contacts)+len(currentData.InternalContacts))
} }
func calculateFileHash(filename string) (string, error) { func calculateFileHash(filename string) (string, error) {
@@ -298,54 +315,6 @@ func processContacts(contacts []Contact) *ContactData {
return &data return &data
} }
func reloadData() {
currentHash, err := calculateFileHash(xlsxFile)
if err != nil {
log.Printf("Hash check error: %v", err)
return
}
if currentHash != currentData.FileHash {
log.Println("Detected file changes - reloading data")
// Check if Excel file exists
if _, err := os.Stat(xlsxFile); os.IsNotExist(err) {
log.Printf("Excel file %s not found, using empty data", xlsxFile)
currentData = &ContactData{
Contacts: []Contact{},
InternalContacts: []Contact{},
LastUpdated: time.Now(),
FileHash: "",
}
return
}
// Parse Excel file
log.Println("Parsing Excel file...")
contacts, err := parseExcelFile(xlsxFile)
if err != nil {
log.Printf("Error parsing Excel file: %v", err)
// Use empty data if parsing fails
currentData = &ContactData{
Contacts: []Contact{},
InternalContacts: []Contact{},
LastUpdated: time.Now(),
FileHash: currentHash,
}
return
}
currentData = processContacts(contacts)
currentData.FileHash = currentHash
// Save to cache
if err := saveCachedData(currentData); err != nil {
log.Printf("Warning: Could not save cached data: %v", err)
}
log.Printf("Loaded %d contacts from Excel file", len(currentData.Contacts)+len(currentData.InternalContacts))
}
}
func serveIndex(w http.ResponseWriter, r *http.Request) { func serveIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
http.NotFound(w, r) http.NotFound(w, r)
@@ -377,6 +346,19 @@ func serveContacts(w http.ResponseWriter, r *http.Request) {
encoder.Encode(currentData) encoder.Encode(currentData)
} }
func reloadData(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
log.Println("Manual reload requested")
loadData()
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"status": "reloaded", "contacts_count": %d}`, len(currentData.Contacts)+len(currentData.InternalContacts))
}
func getEmbeddedHTML() string { func getEmbeddedHTML() string {
return `<!DOCTYPE html> return `<!DOCTYPE html>
<html lang="cs"> <html lang="cs">