mirror of
https://github.com/Dvorinka/PPve.git
synced 2026-06-03 20:12:59 +00:00
Add files via upload
This commit is contained in:
+62
-80
@@ -38,7 +38,7 @@ var (
|
||||
)
|
||||
|
||||
func startAutoReload() {
|
||||
ticker := time.NewTicker(3 * 24 * time.Hour)
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
quit := make(chan struct{})
|
||||
go func() {
|
||||
for {
|
||||
@@ -69,19 +69,7 @@ func main() {
|
||||
// Set up HTTP handlers
|
||||
http.HandleFunc("/", serveIndex)
|
||||
http.HandleFunc("/contacts", serveContacts)
|
||||
http.HandleFunc("/reload", func(w http.ResponseWriter, r *http.Request) {
|
||||
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))
|
||||
})
|
||||
http.HandleFunc("/reload", reloadData)
|
||||
|
||||
// Start server
|
||||
port := os.Getenv("PORT")
|
||||
@@ -95,29 +83,58 @@ func main() {
|
||||
}
|
||||
|
||||
func loadData() {
|
||||
// Check file every 5 minutes
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
checkFileAndReload()
|
||||
// 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: "",
|
||||
}
|
||||
}()
|
||||
|
||||
// Initial load
|
||||
checkFileAndReload()
|
||||
}
|
||||
|
||||
func checkFileAndReload() {
|
||||
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")
|
||||
reloadData()
|
||||
// Calculate current file hash
|
||||
currentHash, err := calculateFileHash(xlsxFile)
|
||||
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) {
|
||||
@@ -298,54 +315,6 @@ func processContacts(contacts []Contact) *ContactData {
|
||||
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) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
@@ -377,6 +346,19 @@ func serveContacts(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="cs">
|
||||
|
||||
Reference in New Issue
Block a user