This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
/
hawkthorne.go
81 lines (63 loc) · 2.2 KB
/
hawkthorne.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
package main
import (
"encoding/base64"
"encoding/json"
"html/template"
"log"
"net/http"
"net/url"
"os"
)
func renderTemplate(path string) func(http.ResponseWriter, *http.Request) {
tmpl, err := template.ParseFiles("templates/base.html", "templates/"+path)
if err != nil {
log.Fatal(err)
}
return func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, nil)
}
}
func trackDownload(path string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
go func() {
datum := map[string]interface{}{
"event": "game.downloaded",
"properties": map[string]interface{}{
"token": os.Getenv("MIXPANEL_TOKEN"),
"ip": r.Header.Get("X-Forwarded-For"),
},
}
b, err := json.Marshal(datum)
if err != nil {
return
}
v := url.Values{}
v.Set("data", base64.StdEncoding.EncodeToString(b))
_, _ = http.Get("http://api.mixpanel.com/track/?" + v.Encode())
}()
url := "https://s3.amazonaws.com/hawkthorne.journey.builds/" + path
http.Redirect(w, r, url, http.StatusFound)
}
}
func main() {
http.HandleFunc("/", renderTemplate("index.html"))
http.HandleFunc("/sprites", renderTemplate("sprites.html"))
http.HandleFunc("/soundtrack/disc/1", renderTemplate("disc1.html"))
http.HandleFunc("/soundtrack/disc/2", renderTemplate("disc2.html"))
http.HandleFunc("/downloads/hawkthorne-win-x64.zip", trackDownload("hawkthorne-win-x64.zip"))
http.HandleFunc("/downloads/hawkthorne-win-x86.zip", trackDownload("hawkthorne-win-x86.zip"))
http.HandleFunc("/downloads/hawkthorne-osx.zip", trackDownload("hawkthrone-osx.zip"))
http.HandleFunc("/downloads/hawkthorne.love", trackDownload("hawkthorne.love"))
http.Handle("/sprites.html", http.RedirectHandler("/sprites", 301))
http.Handle("/audio.html", http.RedirectHandler("/soundtrack/disc/1", 301))
http.Handle("/soundtrack.html", http.RedirectHandler("/soundtrack/disc/1", 301))
http.Handle("/soundtrack-disc2.html", http.RedirectHandler("/soundtrack/disc/2", 301))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
pfx := "/static/"
h := http.StripPrefix(pfx, http.FileServer(http.Dir("static")))
http.Handle(pfx, h)
log.Fatal(http.ListenAndServe(":"+port, nil))
}