diff --git a/src/hhu/handler.go b/src/hhu/handler.go new file mode 100644 index 0000000..a6671bd --- /dev/null +++ b/src/hhu/handler.go @@ -0,0 +1,26 @@ +package hhu + +import ( + "net/http" + + "github.com/jordic/goics" +) + + +func Handler(w http.ResponseWriter, r *http.Request) { + events, err := GetHhuComputerScienceEvent() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) + header := w.Header() + header.Set("Content-type", "text/calendar") + header.Set("charset", "utf-8") + header.Set("Content-Disposition", "inline") + header.Set("filename", "calendar.ics") + enc := goics.NewICalEncode(w) + enc.Encode(events) +} + diff --git a/src/scrape/hhu.go b/src/hhu/scrape.go similarity index 98% rename from src/scrape/hhu.go rename to src/hhu/scrape.go index 5400f7d..b4124e3 100644 --- a/src/scrape/hhu.go +++ b/src/hhu/scrape.go @@ -1,4 +1,4 @@ -package scrape +package hhu import ( "encoding/json" diff --git a/src/main.go b/src/main.go index b5d9ff9..a43ce37 100644 --- a/src/main.go +++ b/src/main.go @@ -5,9 +5,8 @@ import ( "net/http" "os" - "github.com/cato-001/calapi/scrape" + "github.com/cato-001/calapi/hhu" "github.com/gorilla/mux" - "github.com/jordic/goics" arg "github.com/alexflint/go-arg" ) @@ -25,7 +24,7 @@ func main() { r := mux.NewRouter() r.HandleFunc("/", HomeHandler) - r.HandleFunc("/hhu", HhuHandler) + r.HandleFunc("/hhu", hhu.Handler) url := fmt.Sprintf(":%d", args.Port) http.ListenAndServe(url, r) @@ -35,14 +34,3 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func HhuHandler(w http.ResponseWriter, r *http.Request) { - events, err := scrape.GetHhuComputerScienceEvent() - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusOK) - enc := goics.NewICalEncode(w) - enc.Encode(events) -}