forked from nu7hatch/egoistat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
egoistat.go
87 lines (67 loc) · 1.96 KB
/
egoistat.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
package main
import (
"encoding/json"
"flag"
"github.com/nu7hatch/egoistat/backend"
"log"
"net/http"
"net/url"
"strings"
"text/template"
)
var Addr string
func init() {
flag.StringVar(&Addr, "addr", ":8080", "The address to serve on")
flag.Parse()
}
var scriptTpl = "var egoistat={c:{{.Data}}, points: function(sn){return this.c[sn] || 0;}};\n{{.Callback}};"
func transformParams(form url.Values) (res map[string]string) {
res = make(map[string]string)
for k, v := range form {
res[k] = strings.Join(v, "\n")
}
return
}
func statHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var networks = strings.Split(r.FormValue("n"), ",")
var url = r.FormValue("url")
var params = transformParams(r.Form)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
request := egoistat.NewRequest(url, params)
results := request.Stat(networks...)
enc := json.NewEncoder(w)
enc.Encode(results)
}
type countScriptData struct {
Data string
Callback string
}
func statScriptHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var networks = strings.Split(r.FormValue("n"), ",")
var url = r.FormValue("url")
var params = transformParams(r.Form)
var callback = r.FormValue("cb")
if len(strings.TrimSpace(callback)) > 0 {
callback = callback + "()"
}
w.Header().Set("Content-Type", "text/javascript")
w.WriteHeader(200)
request := egoistat.NewRequest(url, params)
results := request.Stat(networks...)
data, _ := json.Marshal(results)
tmpl, _ := template.New("script").Parse(scriptTpl)
tmpl.Execute(w, countScriptData{string(data), callback})
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/index.html")
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./public")))
http.HandleFunc("/stat/", indexHandler)
http.HandleFunc("/api/v1/stat.json", statHandler)
http.HandleFunc("/api/v1/stat.js", statScriptHandler)
log.Fatal(http.ListenAndServe(Addr, nil))
}