-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
215 lines (199 loc) · 5.72 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
package nasa
import (
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
// NewServer a http web-server that serves APOD pictures
// / - today's APOD
// /random-apod - returns a random APOD
// TODO: /apod/YYYY-MM-DD - returns apod for specified date
func NewServer(listenAddr string) (*http.Server, error) {
var err error
tmpl, err = template.New("tmpl").Parse(tmplHTML)
if err != nil {
return nil, fmt.Errorf("unable to parse template: %v", err)
}
rh := &randomHandler{
lastUpdate: time.Now().Add(-10 * time.Hour),
cachedApod: &Image{},
tmpl: tmpl,
}
http.HandleFunc("/", handleIndex)
http.Handle("/random-apod/", rh)
return &http.Server{
Addr: listenAddr,
ReadTimeout: 30 * time.Second,
IdleTimeout: 5 * time.Minute,
WriteTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 20,
}, nil
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
apod, err := APODToday()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
TmplData{Apod: *apod}.Render(w)
}
type randomHandler struct {
tmpl *template.Template
mu sync.RWMutex // protects the values below
lastUpdate time.Time
cachedApod *Image
}
func (h *randomHandler) last() time.Time {
h.mu.RLock()
last := h.lastUpdate
h.mu.RUnlock()
return last
}
func (h *randomHandler) apod() Image {
h.mu.RLock()
apod := *h.cachedApod
h.mu.RUnlock()
return apod
}
func (h *randomHandler) update(apod Image, t time.Time) {
h.mu.Lock()
h.cachedApod = &apod
h.lastUpdate = time.Now()
h.mu.Unlock()
}
func (h *randomHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/random-apod/" {
http.NotFound(w, r)
return
}
apod := h.apod()
// Update if cached apod is older than a second
if time.Now().Sub(h.last()) > time.Second {
if newApod, err := RandomAPOD(); err == nil {
if newApod.URL != "" {
apod = *newApod
}
}
h.update(apod, time.Now())
}
td := TmplData{
Apod: h.apod(),
SD: r.URL.Query().Get("sd") != "",
AutoReload: r.URL.Query().Get("auto") != "" || r.URL.Query().Get("interval") != "",
Legacy: r.URL.Query().Get("legacy") != "",
}
i := r.URL.Query().Get("interval")
if i != "" {
if n, err := strconv.Atoi(i); err == nil {
td.AutoReloadInterval = n
}
}
if td.AutoReloadInterval < 1 {
td.AutoReloadInterval = 5 * 60 // default reload every 5 minutes
}
td.Render(w)
}
var tmpl *template.Template
// TmplData defines the data used to render the html template (tmpl)
type TmplData struct {
Page string
Title string
Apod Image
SD bool // Standard definition display
AutoReload bool
Legacy bool // legacy browser does not support new reload
IsYoutube bool
AutoReloadInterval int
}
// Render returns an html to the responsewriter based on the template data
func (td TmplData) Render(wr http.ResponseWriter) {
if strings.Contains(td.Apod.URL, "youtube") {
td.IsYoutube = true
if i := strings.Index(td.Apod.URL, "?"); i > 1 {
td.Apod.URL = td.Apod.URL[:i]
}
}
if td.Apod.URL == "" && td.Apod.HDURL == "" {
http.Error(wr, "NASA API currently unavailable, it's experiencing downtime :(", http.StatusServiceUnavailable)
return
}
if err := tmpl.Execute(wr, td); err != nil {
log.Print(err)
}
}
const tmplHTML = `<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>{{with .Title}}{{.}}{{else}}NASA Astronomy Picture of the Day{{end}}</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
{{if .AutoReload -}}
{{if not .Legacy }}
<meta http-equiv="refresh" content="{{.AutoReloadInterval}}" >
{{end}}
{{end -}}
<style>html,body{ margin:0; padding:0}
body{background-color:#000;color:#fff}
#imgwrap{
position:fixed; top:0;left:0;
min-width:100%; max-width:100%;
overflow:hidden;
}
#imgwrap img{
display:block; margin:0 auto;padding:0;
max-width:100%; max-height:100%;
}
#apod{ display:block; position:fixed; bottom:0; left:30px; right:30px;}
#explanation {
display:none;
background-color:rgba(50,50,50,0.5);
padding:10px; border-radius:10px;
}
#apod:hover #explanation{display:block}
@media screen and (max-width:600px){
#explanation{display:none;}
}
</style>
{{if and .AutoReload .Legacy}}
<script type='text/javascript'>
function TimedRefresh( t ) { setTimeout("location.reload(true);", t); }
</script>
{{end}}
<body {{if and .AutoReload .Legacy}}onload="javascript:TimedRefresh({{.AutoReloadInterval}}*1000)"{{end}}>
<div id="imgwrap">
{{if .IsYoutube}}
<div style="position: fixed; z-index: -99; width: 100%; height: 100%">
<iframe frameborder="0" height="100%" width="100%"
src="{{.Apod.URL}}?autoplay=1&controls=0&showinfo=0&autohide=1">
</iframe>
</div>
{{else}}
<img src="{{if .SD}}{{.Apod.URL}}{{else}}{{.Apod.HDURL}}{{end}}" id="bg" alt="{{.Apod.Title}}" />
{{end}}
</div>
<div id="apod">
<div id="explanation">
<h4>{{.Apod.Title}}</h4>
<p>{{.Apod.Explanation}}</p>
<p>NASA Astronomy Picture of the Day {{.Apod.Date}} <a href="{{.Apod.HDURL}}" style="display:inline-block; color:#efefef"><i>Open Image in HD</i></a> </p>
</div>
<h4>{{.Apod.Title}}</h4>
<p style="text-align:right">
View in fullscreen (F11) for best experience ☺.
<i>Reload random pic every <a href="/random-apod/?auto=1&interval=60" style="color:#fff">1 min</a>, <a href="/random-apod/?auto=1&interval=600" style="color:#fff">10 min</a></i>
<b>{{.Title}}</b>
<i>This project is on Github.</i>
<a class="github-button" href="https://github.com/peteretelej/nasa" data-icon="octicon-star" data-show-count="true" aria-label="Star peteretelej/nasa on GitHub">Star</a>
</p>
</div>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</body>
</html>`