From 2a1cd138c9e4e2e4c6b2233665b1ac15d4fc9f17 Mon Sep 17 00:00:00 2001 From: qypol342 Date: Thu, 5 Sep 2024 17:13:15 +0200 Subject: [PATCH] fix add calendar title --- docker-compose.yml | 2 ++ ical/gen.go | 7 +++++-- main.go | 25 +++++++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 211908e..e70e141 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,8 @@ services: environment: - MYCPE_USERNAME=${MYCPE_USERNAME} - MYCPE_PASSWORD=${MYCPE_PASSWORD} + - START_TIMESTAMP=${START_TIMESTAMP} + - END_TIMESTAMP=${END_TIMESTAMP} nginx: image: ghcr.io/loan-mgt/10m-caching:latest diff --git a/ical/gen.go b/ical/gen.go index db1b0fc..652334f 100644 --- a/ical/gen.go +++ b/ical/gen.go @@ -11,8 +11,11 @@ import ( const regexPattern = `(?P.*?)(?P[1-9][A-Z]{3,})(?P.*?)(?P(( |n)[A-Z]{3,} .*)|$)` // GenerateICS generates an ICS string from a list of events -func GenerateICS(events []Event) string { - ics := "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Your Organization//Your Product//EN\n" +func GenerateICS(events []Event, calendarName string) string { + ics := "BEGIN:VCALENDAR\n" + ics += "VERSION:2.0\n" + ics += "PRODID:-//Your Organization//Your Product//EN\n" + ics += fmt.Sprintf("X-WR-CALNAME:%s\n", calendarName) // Set calendar name // Define the layout for parsing the datetime with a timezone offset const layout = "2006-01-02T15:04:05-0700" diff --git a/main.go b/main.go index cad3d7e..fba7878 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,19 @@ import ( "cpe/calendar/request" "log" "net/http" + "os" "github.com/gorilla/mux" + "github.com/joho/godotenv" ) +func init() { + // Load environment variables from .env file + if err := godotenv.Load(); err != nil { + log.Fatalf("Error loading .env file: %v", err) + } +} + func main() { r := mux.NewRouter() @@ -23,10 +32,10 @@ func main() { } // generateICSHandler generates the ICS file and sends it in the response with a given filename -func generateICSHandler(w http.ResponseWriter, _ *http.Request, filename string) { - // Set start and end times (these could be retrieved from request parameters if needed) - start := "1725228000000" // Example start timestamp - end := "1728684000000" // Example end timestamp +func generateICSHandler(w http.ResponseWriter, _ *http.Request, filename, calendarName string) { + // Get start and end times from environment variables + start := os.Getenv("START_TIMESTAMP") + end := os.Getenv("END_TIMESTAMP") // Step 1: Fetch data from the source using the updated FetchData function data, err := request.FetchData(start, end) @@ -46,8 +55,8 @@ func generateICSHandler(w http.ResponseWriter, _ *http.Request, filename string) return } - // Step 3: Generate the iCal file - icsContent := ical.GenerateICS(events) + // Step 3: Generate the iCal file with the calendar name + icsContent := ical.GenerateICS(events, calendarName) // Step 4: Set headers for the iCal file response with the provided filename w.Header().Set("Content-Type", "text/calendar") @@ -59,6 +68,6 @@ func generateICSHandler(w http.ResponseWriter, _ *http.Request, filename string) // generate3IRCHandler is a wrapper around generateICSHandler that uses a specific filename func generate3IRCHandler(w http.ResponseWriter, r *http.Request) { - // Call generateICSHandler with the specific filename - generateICSHandler(w, r, "3irc_calendar.ics") + // Call generateICSHandler with the specific filename and calendar name + generateICSHandler(w, r, "3irc_calendar.ics", "3IRC Calendar") }