forked from thesephist/frieden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrieden.go
144 lines (121 loc) · 3.04 KB
/
frieden.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
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
// static file server
// api endpoint that runs as a (cached?) proxy to gcal servers
// added comment
// durr
var config appConfig
type appConfig struct {
ApiKey string `json:"apiKey"`
CalendarIds []string `json:"calendars"`
}
type calendarIncomingReq struct {
TimeZone string `json:"timeZone"`
TimeMin string `json:"timeMin"`
TimeMax string `json:"timeMax"`
}
type calendarOutgoingReq struct {
TimeZone string `json:"timeZone"`
TimeMin string `json:"timeMin"`
TimeMax string `json:"timeMax"`
Calendars []calendarOutgoingReqId `json:"items"`
}
type calendarOutgoingReqId struct {
Id string `json:"id"`
}
func mustConfigure() {
configFile, err := os.Open("./secrets.json")
if err != nil {
log.Fatalf("Could not read secrets file!\n\tError: %s", err.Error())
}
defer configFile.Close()
configString, err := ioutil.ReadAll(configFile)
if err != nil {
log.Fatalf("Could not read config from file!\n\tError: %s", err.Error())
}
err = json.Unmarshal(configString, &config)
}
func handleHome(w http.ResponseWriter, r *http.Request) {
indexFile, err := os.Open("./static/index.html")
if err != nil {
io.WriteString(w, "error reading index")
return
}
defer indexFile.Close()
io.Copy(w, indexFile)
}
func getData(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
io.WriteString(w, "{}")
return
}
var reqParams calendarIncomingReq
err = json.Unmarshal(body, &reqParams)
if err != nil {
io.WriteString(w, "{}")
return
}
calendars := []calendarOutgoingReqId{}
for _, id := range config.CalendarIds {
calendars = append(calendars, calendarOutgoingReqId{
Id: id,
})
}
outgoingParams := calendarOutgoingReq{
TimeZone: reqParams.TimeZone,
TimeMin: reqParams.TimeMin,
TimeMax: reqParams.TimeMax,
Calendars: calendars,
}
jsonStr, err := json.Marshal(&outgoingParams)
proxyReq, err := http.NewRequest(
"POST",
fmt.Sprintf("https://www.googleapis.com/calendar/v3/freeBusy?alt=json&key=%s", config.ApiKey),
bytes.NewBuffer(jsonStr),
)
if err != nil {
// TODO
log.Fatal("TODO")
}
proxyReq.Header.Set("Accept", "application/json")
proxyReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(proxyReq)
if err != nil {
// TODO
log.Fatal("TODO")
}
defer resp.Body.Close()
_, err = io.Copy(w, resp.Body)
if err != nil {
// TODO
log.Fatal("TODO")
}
}
func main() {
mustConfigure()
r := mux.NewRouter()
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:7856",
WriteTimeout: 60 * time.Second,
ReadTimeout: 60 * time.Second,
}
r.HandleFunc("/", handleHome)
r.Methods("POST").Path("/data").HandlerFunc(getData)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
log.Printf("Frieden listening on %s\n", srv.Addr)
log.Fatal(srv.ListenAndServe())
}