-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitor.go
60 lines (54 loc) · 1.46 KB
/
visitor.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
package app
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"sync"
"time"
"github.com/revel/revel"
)
var VisitorCount int64 = 0
var countLock sync.Mutex
var ReaderCounts = make(map[string]int)
func loadVisitorCount() {
content, err := ioutil.ReadFile(BaseDir + "/visitors_count")
if err != nil {
revel.ERROR.Printf("Cannot load visitor count, %s", err.Error())
VisitorCount = 0
} else {
VisitorCount, _ = strconv.ParseInt(string(content), 10, 64)
}
}
func RecordVisit(c *revel.Controller) revel.Result {
target := c.Request.URL.String()
from := c.Request.Header.Get("X-Real-Ip")
if from == "" {
from = c.Request.RemoteAddr
}
go func(t string, f string) {
today := time.Now().Format("Jan_02_2006")
visitTime := time.Now().Format("15:04:05")
countLock.Lock()
defer countLock.Unlock()
VisitorCount += 1
msg := fmt.Sprintf("%d|%s|%s|%s\r\n", VisitorCount, t, f, visitTime)
revel.INFO.Print(msg)
if f, err := os.OpenFile(BaseDir+"/visitors_"+today,
os.O_CREATE|os.O_APPEND|os.O_WRONLY,
0644); err == nil {
defer f.Close()
if _, err = f.WriteString(msg); err != nil {
revel.ERROR.Printf("Cannot log visitor info, %s\r\n", err.Error())
}
} else {
revel.ERROR.Printf("Cannot log visitor info, %s\r\n", err.Error())
}
err := ioutil.WriteFile(BaseDir+"/visitors_count",
[]byte(fmt.Sprintf("%d", VisitorCount)), 0644)
if err != nil {
revel.ERROR.Printf("Cannot update visitor info: %s", err.Error())
}
}(target, from)
return nil
}