-
Notifications
You must be signed in to change notification settings - Fork 0
/
hitcounter.go
83 lines (66 loc) · 1.82 KB
/
hitcounter.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
package main
import (
"fmt"
"github.com/writeas/impart"
"github.com/writeas/web-core/bots"
"github.com/writeas/web-core/log"
"image"
"image/color"
"image/gif"
"io/ioutil"
"net/http"
"time"
)
var counts = map[string]uint64{}
// CountView counts the view if it meets certain criteria, returning whether or not it was actually counted.
func CountView(r *http.Request) bool {
if r.Method == "HEAD" || bots.IsBot(r.UserAgent()) {
return false
}
path := r.FormValue("p")
if path == "" {
return false
}
// Count the hit
counts[path]++
SaveCounts()
return true
}
func handlePixel(w http.ResponseWriter, r *http.Request) error {
CountView(r)
m := image.NewRGBA(image.Rect(0, 0, 1, 1))
m.Set(0, 0, color.RGBA{255, 255, 255, 0})
w.Header().Set("Content-Type", "image/gif")
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
w.Header().Set("Expires", "Wed, 11 Nov 2002 11:11:11 GMT")
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0")
w.Header().Set("Pragma", "no-cache")
err := gif.Encode(w, m, nil)
if err != nil {
log.Error("Unable to encode gif: %s", err)
return err
}
return nil
}
func handleView(w http.ResponseWriter, r *http.Request) error {
CountView(r)
return handleViewHits(w, r)
}
func handleViewHits(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Access-Control-Allow-Origin", "*")
path := r.FormValue("p")
fmt.Fprintf(w, "%d", counts[path])
return nil
}
func handleHome(w http.ResponseWriter, r *http.Request) error {
if r.URL.Path != "/" {
return impart.HTTPError{http.StatusNotFound, "Not found"}
}
data, err := ioutil.ReadFile("index.html")
if err != nil {
return impart.HTTPError{http.StatusNotFound, "Not found"}
}
w.Header().Set("Content-Type", "text/html")
_, err = w.Write(data)
return err
}