-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (60 loc) · 1.42 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"html/template"
"net/http"
"os"
"time"
"github.com/opeonikute/panda"
)
type HomePageData struct {
PandaFound bool
ImageURL string
Source string
FileName string
WordOfTheDay string
}
func main() {
port := os.Getenv("PORT")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := HomePageData{
PandaFound: false,
}
panda, err := getPandaOfTheDay()
if err != nil {
fmt.Print(err)
} else {
data.PandaFound = true
data.ImageURL = panda.URL
data.Source = panda.Source
data.FileName = panda.FileName
// Would prefer to do this using a ternary,
// but if-else is the idiomatic way to do it in Go.
data.WordOfTheDay = panda.WordOfTheDay
if data.WordOfTheDay == "" {
data.WordOfTheDay = "Pandas are the best."
}
}
tmpl := template.Must(template.ParseFiles("index.html"))
tmpl.Execute(w, data)
})
fs := http.FileServer(http.Dir("static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fmt.Printf("Server listening on port: %s\n", port)
http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
}
func getPandaOfTheDay() (panda.Entry, error) {
var en panda.Entry
goPanda := panda.GoPanda{
Config: panda.Settings{
MongoURL: os.Getenv("MONGO_URL"),
MongoDB: os.Getenv("MONGO_DATABASE"),
},
}
tm := time.Now()
res, err := goPanda.GetPOD(tm)
if err != nil {
return en, err
}
return res, nil
}