diff options
Diffstat (limited to 'internal/web/server.go')
| -rw-r--r-- | internal/web/server.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/internal/web/server.go b/internal/web/server.go index 45ab06f..5ebed38 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -69,6 +69,7 @@ func NewServer(cfg config.Config) http.Handler { mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { indexHandler(s.get())(w, r) }) mux.HandleFunc("GET /readings", func(w http.ResponseWriter, r *http.Request) { readingsHandler(s.get())(w, r) }) mux.HandleFunc("GET /export", func(w http.ResponseWriter, r *http.Request) { exportHandler(s.get())(w, r) }) + mux.HandleFunc("GET /calendar", func(w http.ResponseWriter, r *http.Request) { calendarHandler(s.get())(w, r) }) mux.HandleFunc("GET /reader", func(w http.ResponseWriter, r *http.Request) { readerHandler(s.get(), s.table())(w, r) }) mux.HandleFunc("GET /theme.css", func(w http.ResponseWriter, r *http.Request) { themeCSSHandler(s.get())(w, r) }) mux.HandleFunc("GET /settings", settingsGet(s)) @@ -381,6 +382,41 @@ func exportHandler(cfg config.Config) http.HandlerFunc { } } +// calendarHandler serves GET /calendar?month=YYYY-MM&lectionary=: a printable +// A4 PDF month calendar of the celebration names + gospel references. Missing/ +// bad month defaults to the current month. +func calendarHandler(cfg config.Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + t, err := time.Parse("2006-01", r.URL.Query().Get("month")) + if err != nil { + t = time.Now() + } + year, month := t.Year(), int(t.Month()) + cfg.Lectionary = requestLectionary(cfg, r) + first := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) + + var days []export.CalendarDay + for d := first; int(d.Month()) == month; d = d.AddDate(0, 0, 1) { + cd := export.CalendarDay{Day: d.Day()} + if secs, info, lerr := readings.Load(cfg, readings.Options{Date: d.Format("2006-01-02"), Offline: cfg.Offline, All: false}); lerr == nil { + cd.Name = info.Name + cd.Colour = info.Colour + cd.Citation = readings.GospelCitation(secs) + } + days = append(days, cd) + } + + data, perr := export.CalendarPDF(year, month, days, cfg.Lectionary, cfg.UILanguage) + if perr != nil { + http.Error(w, perr.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/pdf") + w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="lectio-%04d-%02d-calendar.pdf"`, year, month)) + w.Write(data) + } +} + // renderOrError returns RenderReadings' fragment, or (on a readings.Load // error, or no sections at all for the date) a small escaped error // paragraph, localised via lang -- readings.Load errors are expected in |
