mirror of
https://github.com/Dvorinka/PPve.git
synced 2026-06-05 04:52:58 +00:00
Add files via upload
This commit is contained in:
+75
-57
@@ -69,7 +69,19 @@ 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", reloadData)
|
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))
|
||||||
|
})
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
@@ -83,60 +95,31 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadData() {
|
func loadData() {
|
||||||
// Check if Excel file exists
|
// Check file every 5 minutes
|
||||||
if _, err := os.Stat(xlsxFile); os.IsNotExist(err) {
|
ticker := time.NewTicker(1 * time.Hour)
|
||||||
log.Printf("Excel file %s not found, using empty data", xlsxFile)
|
go func() {
|
||||||
currentData = &ContactData{
|
for range ticker.C {
|
||||||
Contacts: []Contact{},
|
checkFileAndReload()
|
||||||
InternalContacts: []Contact{},
|
|
||||||
LastUpdated: time.Now(),
|
|
||||||
FileHash: "",
|
|
||||||
}
|
}
|
||||||
return
|
}()
|
||||||
|
|
||||||
|
// Initial load
|
||||||
|
checkFileAndReload()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate current file hash
|
func checkFileAndReload() {
|
||||||
currentHash, err := calculateFileHash(xlsxFile)
|
currentHash, err := calculateFileHash(xlsxFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error calculating file hash: %v", err)
|
log.Printf("Hash check error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if cached data exists and is up to date
|
if currentHash != currentData.FileHash {
|
||||||
if cachedData, err := loadCachedData(); err == nil {
|
log.Println("Detected file changes - reloading data")
|
||||||
if cachedData.FileHash == currentHash {
|
reloadData()
|
||||||
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) {
|
||||||
file, err := os.Open(filename)
|
file, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -315,6 +298,54 @@ 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)
|
||||||
@@ -346,19 +377,6 @@ 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">
|
||||||
|
|||||||
Reference in New Issue
Block a user