-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
292 lines (268 loc) · 7.32 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
tr "github.com/dethancosta/timeruler/internal"
)
type Server struct {
Owner string // TODO replace with actual credentials for auth
Addr string // Address of the server
Ntfy string
Schedule *tr.Schedule
}
type TaskModel struct {
Description string `json:"Description"`
Tag string `json:"Tag"`
Until string `json:"Until"`
}
func (s *Server) GetSchedule(w http.ResponseWriter, r *http.Request) {
// TODO test
// TODO authenticate
if s.Schedule == nil {
http.Error(w, "No schedule has been built yet.", http.StatusNotFound)
return
}
current, idx := s.Schedule.Tasks.GetTaskAtTime(time.Now())
if idx == -1 {
s.Schedule.CurrentTask = nil
s.Schedule.CurrentID = -1
} else if idx != s.Schedule.CurrentID {
err := s.Schedule.UpdateCurrentTask()
if err != nil {
log.Printf("GetSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
if s.Ntfy != "" {
currentModel := TaskModel{
current.Description,
current.Tag,
current.EndTime.Format(time.TimeOnly),
}
err = s.NtfyNewCurrent(s.Ntfy, currentModel)
if err != nil {
log.Printf("GetSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// TODO delete this line
log.Println("ntfy sent")
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
body := make(map[string]string)
body["Schedule"] = s.Schedule.String()
msg, err := json.Marshal(body)
if err != nil {
log.Printf("GetSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(msg)
}
func (s *Server) GetCurrentTask(w http.ResponseWriter, r *http.Request) {
// TODO test
// TODO authenticate
current, idx := s.Schedule.Tasks.GetTaskAtTime(time.Now())
if current == nil {
http.Error(w, "No current task found.", http.StatusNotFound)
return
}
if idx != s.Schedule.CurrentID {
err := s.Schedule.UpdateCurrentTask()
if err != nil {
log.Printf("GetCurrentTask: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
if s.Ntfy != "" {
currentModel := TaskModel{
current.Description,
current.Tag,
current.EndTime.Format(time.TimeOnly),
}
err = s.NtfyNewCurrent(s.Ntfy, currentModel)
if err != nil {
log.Printf("GetSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// TODO delete this line
log.Println("ntfy sent")
}
}
msg, err := json.Marshal(map[string]struct {
Description string `json:"Description"`
Tag string `json:"Tag"`
Until string `json:"Until"`
}{
"Task": {
Description: current.Description,
Tag: current.Tag,
Until: current.EndTime.Format(time.TimeOnly),
},
})
if err != nil {
log.Printf("GetCurrentTask: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(msg)
}
func (s *Server) ChangeCurrentTask(w http.ResponseWriter, r *http.Request) {
// TODO test
// TODO authenticate
var taskModel TaskModel
err := json.NewDecoder(r.Body).Decode(&taskModel)
if err != nil {
log.Printf("ChangeCurrentTask: %s", err.Error())
http.Error(w, "Invalid HTTP Body", http.StatusBadRequest)
return
}
// TODO validate time
end, err := time.Parse(time.TimeOnly, taskModel.Until)
now := time.Now()
end = time.Date(now.Year(), now.Month(), now.Day(), end.Hour(), end.Minute(), 0, 0, time.Local)
if err != nil {
log.Printf("ChangeCurrentTask: %s", err)
http.Error(w, fmt.Sprintf("Please give the time in the following format: %s", time.TimeOnly), http.StatusBadRequest)
return
}
err = s.Schedule.ChangeCurrentTaskUntil(taskModel.Description, taskModel.Tag, end)
if err != nil {
log.Printf("ChangeCurrentTask: %s", err.Error())
if errors.Is(err, tr.InvalidTimeError{}) {
http.Error(w, "Please give a valid time for the task to finish.", http.StatusBadRequest)
} else {
http.Error(w, "Encountered an internal server error.", http.StatusInternalServerError)
}
return
}
if s.Ntfy != "" {
currentModel := taskModel
err = s.NtfyNewCurrent(s.Ntfy, currentModel)
if err != nil {
log.Printf("GetSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Println("ntfy sent")
}
w.WriteHeader(http.StatusOK)
}
func (s *Server) UpdateTasks(w http.ResponseWriter, r *http.Request) {
if s.Schedule == nil {
http.Error(w, "No schedule has been built yet.", http.StatusBadRequest)
return
}
var tasks []tr.Task
err := json.NewDecoder(r.Body).Decode(&tasks)
if err != nil {
log.Printf("UpdateTasks: %s", err.Error())
http.Error(w, "Invalid HTTP Body", http.StatusBadRequest)
return
}
now := time.Now()
for _, t := range tasks {
if t.StartTime.Before(now) {
http.Error(w, "A task cannot start before the current time", http.StatusBadRequest)
return
}
}
err = s.Schedule.UpdateTimeBlock(tasks...)
// TODO check type of error and return appropriate response
if err != nil {
http.Error(w, "Update failed", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (s *Server) BuildSchedule(w http.ResponseWriter, r *http.Request) {
// TODO authenticate
// TODO test
if s.Schedule != nil {
http.Error(w, "Today's schedule has already been built.", http.StatusBadRequest)
return
}
err := r.ParseMultipartForm(8 << 20) // max file size 8 MB
if err != nil {
log.Printf("BuildSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
buildFile, h, err := r.FormFile("buildFile")
if err != nil {
log.Printf("BuildSchedule: %s", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
tmpfile, err := os.CreateTemp("./", h.Filename)
defer func() {
tmpfile.Close()
err = os.Remove(tmpfile.Name())
if err != nil {
log.Printf("BuildSchedule: %s", err.Error())
}
}()
if err != nil {
log.Printf("BuildSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = io.Copy(tmpfile, buildFile)
if err != nil {
log.Printf("BuildSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
s.Schedule, err = tr.BuildFromFile(tmpfile.Name())
if err != nil {
log.Printf("BuildSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
current, idx := s.Schedule.Tasks.GetTaskAtTime(time.Now())
if s.Ntfy != "" && idx != -1 {
currentModel := TaskModel{
current.Description,
current.Tag,
current.EndTime.Format(time.TimeOnly),
}
err = s.NtfyNewCurrent(s.Ntfy, currentModel)
if err != nil {
log.Printf("GetSchedule: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Println("ntfy sent")
}
w.WriteHeader(http.StatusOK)
}
func (s *Server) PlanSchedule(w http.ResponseWriter, r *http.Request) {
// TODO implement
}
func (s *Server) NtfyNewCurrent(ntfyId string, task TaskModel) error {
// TODO implement
req, err := http.NewRequest("POST", "https://ntfy.sh/"+ntfyId,
strings.NewReader("Until "+task.Until))
if err != nil {
return err
}
req.Header.Set("Title", task.Description)
req.Header.Set("Tags", "hourglass")
_, err = http.DefaultClient.Do(req)
return err
}
func (s *Server) NtfyNewSchedule() error {
// TODO implement
return nil
}