This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
132 lines (105 loc) · 3.91 KB
/
server.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"database/sql"
"encoding/json"
"github.com/brianglass/orthocal"
"github.com/gorilla/mux"
"io"
"log"
"net/http"
"strconv"
"time"
)
const (
CalendarMaxDays = 7 * 30
CacheControl = "max-age=14400"
)
type CalendarServer struct {
db *sql.DB
bible orthocal.Bible
useJulian bool
doJump bool
title string
}
func NewCalendarServer(router *mux.Router, db *sql.DB, useJulian, doJump bool, bible orthocal.Bible, title string) *CalendarServer {
var self CalendarServer
self.db = db
self.bible = bible
self.useJulian = useJulian
self.doJump = doJump
self.title = title
r := router.Methods("GET", "HEAD").Subrouter()
r.HandleFunc(`/`, self.todayHandler)
r.HandleFunc(`/ical/`, self.icalHandler)
r.HandleFunc(`/{year:\d+}/{month:\d+}/`, self.monthHandler)
r.HandleFunc(`/{year:\d+}/{month:\d+}/{day:\d+}/`, self.dayHandler)
return &self
}
func (self *CalendarServer) todayHandler(writer http.ResponseWriter, request *http.Request) {
today := time.Now().In(TZ)
factory := orthocal.NewDayFactory(self.useJulian, self.doJump, self.db)
Day := factory.NewDayWithContext(request.Context(), today.Year(), int(today.Month()), today.Day(), self.bible)
writer.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(writer)
encoder.SetIndent("", "\t")
if e := encoder.Encode(Day); e != nil {
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Could not marshal json for dayHandler: %#v.", e)
}
}
func (self *CalendarServer) dayHandler(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
// Mux is setup to only send things that match this pattern, so we don't
// need to handle the errors.
year, _ := strconv.Atoi(vars["year"])
month, _ := strconv.Atoi(vars["month"])
day, _ := strconv.Atoi(vars["day"])
factory := orthocal.NewDayFactory(self.useJulian, self.doJump, self.db)
Day := factory.NewDayWithContext(request.Context(), year, month, day, self.bible)
writer.Header().Set("Content-Type", "application/json")
writer.Header().Set("Cache-Control", CacheControl)
encoder := json.NewEncoder(writer)
encoder.SetIndent("", "\t")
e := encoder.Encode(Day)
if e != nil {
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Could not marshal json for dayHandler: %#v.", e)
}
}
func (self *CalendarServer) monthHandler(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
// Mux is setup to only send things that match this pattern, so we don't
// need to handle the errors.
year, _ := strconv.Atoi(vars["year"])
month, _ := strconv.Atoi(vars["month"])
factory := orthocal.NewDayFactory(self.useJulian, self.doJump, self.db)
writer.Header().Set("Content-Type", "application/json")
writer.Header().Set("Cache-Control", CacheControl)
encoder := json.NewEncoder(writer)
encoder.SetIndent("", "\t")
io.WriteString(writer, "[")
for day := 1; day <= 31; day++ {
date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
// time.Date automatically wraps, so break out once we hit the next month
if date.Month() != time.Month(month) {
break
}
d := factory.NewDayWithContext(request.Context(), date.Year(), int(date.Month()), date.Day(), nil)
if day > 1 {
io.WriteString(writer, ", ")
}
e := encoder.Encode(d)
if e != nil {
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
log.Printf("Could not marshal json for dayHandler: %#v.", e)
}
}
io.WriteString(writer, "]")
}
func (self *CalendarServer) icalHandler(writer http.ResponseWriter, request *http.Request) {
start := time.Now().In(TZ).AddDate(0, 0, -30)
factory := orthocal.NewDayFactory(self.useJulian, self.doJump, self.db)
writer.Header().Set("Content-Type", "text/calendar")
writer.Header().Set("Cache-Control", CacheControl)
GenerateCalendar(request.Context(), writer, start, CalendarMaxDays, factory, self.title)
}