/
mcs-stats.go
59 lines (51 loc) · 1.23 KB
/
mcs-stats.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
package main
import (
"github.com/coopernurse/gorp"
"github.com/pmylund/go-cache"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"github.com/zenazn/goji/web/middleware"
"encoding/json"
"math/rand"
"net/http"
"time"
"os"
)
var (
config = configuration{}
server Server
dbmap *gorp.DbMap
dataCache *cache.Cache
emailQueue chan *Email
)
func init() {
// Seed the RNG. Only needs doing once at startup.
rand.Seed(time.Now().UTC().UnixNano())
dataCache = cache.New(time.Minute, 5*time.Second)
// Set up the database.
dbmap = initDatabase()
}
func main() {
// Default Middleware
goji.Use(middleware.EnvInit)
goji.Use(middleware.Recoverer)
goji.Post("/api/pings", HandlePing)
// Static Files
goji.Get("/*", http.StripPrefix("/", http.FileServer(http.Dir(config.StaticLocation))))
// Serve using the magic of Goji!
goji.Serve()
}
func HandlePing(c web.C, w http.Responsewriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
ping := &Ping{}
err := decoder.Decode(ping)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write("Malformed Body")
return
}
ping.Timestamp = time.Now()
ping.NextPing = time.Now().Add(15*time.Minute)
// Begin validation of different fields.
if ping.Timestamp
}