-
Notifications
You must be signed in to change notification settings - Fork 13
/
cmd_server.go
136 lines (123 loc) · 3.01 KB
/
cmd_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
//
// Copyright (c) 2017 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-11-26
//
package main
import (
"context"
"html/template"
"io"
"log"
"net/http"
"net/url"
"path/filepath"
"sync"
"time"
)
const (
previewServerURL = "localhost:61433"
quitAfter = 90 * time.Second
)
// previewURL returns a preview server URL.
func previewURL(t time.Time, eventID string) string {
u, _ := url.Parse("http://" + previewServerURL)
v := u.Query()
v.Set("date", midnight(t).Format(timeFormat))
v.Set("event", eventID)
u.RawQuery = v.Encode()
return u.String()
}
// doStartServer starts the preview server.
func doStartServer() error {
log.Printf("[preview] starting preview server on %s ...", previewServerURL)
var (
lastRequest = time.Now()
mu = sync.Mutex{}
c = make(chan struct{})
templates = template.Must(template.ParseFiles(filepath.Join(wf.Dir(), "preview.html")))
mux = http.NewServeMux()
srv = &http.Server{
Addr: previewServerURL,
Handler: mux,
}
)
go func() {
if err := srv.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
log.Print("[preview] server stopped")
} else {
log.Printf("[preview] ERR: server failed: %v", err)
}
}
c <- struct{}{}
}()
go func() {
c := time.Tick(30 * time.Second)
for now := range c {
mu.Lock()
d := now.Sub(lastRequest)
mu.Unlock()
log.Printf("[preview] %0.0fs since last request", d.Seconds())
if d >= quitAfter {
if err := srv.Shutdown(context.Background()); err != nil {
log.Printf("[preview] server shutdown error: %v", err)
}
// log.Printf("[preview] server stopped")
}
}
}()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
defer func() {
mu.Lock()
lastRequest = time.Now()
mu.Unlock()
}()
var (
v = req.URL.Query()
dateStr = v.Get("date")
eventID = v.Get("event")
event *Event
)
log.Printf("[preview] date=%s, event=%s", dateStr, eventID)
// Load events
t, err := time.Parse(timeFormat, dateStr)
if err != nil {
if _, err := io.WriteString(w, "bad date\n"); err != nil {
log.Printf("[error] write server response: %v", err)
}
return
}
cals, err := activeCalendars()
if err != nil {
log.Printf("[preview] ERR: load active calendars: %v", err)
return
}
log.Printf("[preview] %d active calendar(s)", len(cals))
events, err := loadEvents(t, cals...)
if err != nil {
log.Printf("[preview] ERR: load events: %v", err)
return
}
for _, e := range events {
if e.ID == eventID {
event = e
event.MapURL = mapURL(event.Location)
break
}
}
if event == nil {
if err := templates.ExecuteTemplate(w, "fail", eventID); err != nil {
log.Printf(`[preview] ERR: execute template "fail": %v`, err)
}
return
}
if err := templates.ExecuteTemplate(w, "event", event); err != nil {
log.Printf(`[preview] ERR: execute template "event": %v`, err)
}
})
<-c
return nil
}