-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (54 loc) · 1.18 KB
/
main.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
)
const (
austinLatitude = 30.2672
austinLongitude = -97.7431
)
var (
indexTemplateStr = `
<html>
<head>
<title>Will it rain in Austin today?</title>
<head>
<body>
<center>
<p>Will it rain in Austin today?</p>
<h1>
{{ if .Rain }}
Yes!
{{ else }}
Nope.
{{ end }}
</h1>
</center>
</body>
</html>
`
indexTemplate *template.Template
forecastIOKey string
)
func init() {
indexTemplate = template.Must(template.New("index").Parse(indexTemplateStr))
forecastIOKey = os.Getenv("FORECASTIO_API_KEY")
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
f, err := getForecast()
if err != nil {
http.Error(w, "error: "+err.Error(), http.StatusInternalServerError)
return
}
indexTemplate.Execute(w, map[string]bool{
"Rain": f.Daily.Data[0].PrecipitationProbability > 0.5,
})
}
func main() {
http.HandleFunc("/", indexHandler)
fmt.Println("starting server on port 8080...")
log.Fatal(http.ListenAndServe("localhost:8080", nil))
}