-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
46 lines (43 loc) · 1.07 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"time"
"github.com/lestrrat/go-ical"
"net/http"
"github.com/gorilla/mux"
)
func handleCalendar(w http.ResponseWriter, r *http.Request) {
c := ical.New()
rows, err := db.getEvents()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
for rows.Next() {
var uid, summary string
var start, end time.Time
err := rows.Scan(&uid, &summary, &start, &end)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
e := ical.NewEvent()
var startTxt, endTxt string
startTxt = start.Format(iCalDateFormat)
endTxt = end.Format(iCalDateFormat)
e.AddProperty("uid", uid)
e.AddProperty("summary", summary)
e.AddProperty("dtstart", startTxt)
e.AddProperty("dtend", endTxt)
c.AddEntry(e)
}
if err := rows.Err(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
ical.NewEncoder(w).Encode(c)
}
func serveHttp() error {
router := mux.NewRouter().StrictSlash(true)
router.Methods("GET").Path("/calendar/ical.ics").HandlerFunc(handleCalendar)
return http.ListenAndServe(":8080", router)
}