-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitlog.go
178 lines (142 loc) · 3.77 KB
/
visitlog.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
)
type urirecord struct {
count int
}
type visitlogserver struct {
db *Logdatabase
qm *QuizManager
}
type vistresult struct {
CannonicalURI string
Count int
}
func canonicalizeURI(u string) (string, error) {
result := u
for i := 0; i < 3; i++ {
unescaped, err := url.PathUnescape(result)
if err != nil {
return u, err
}
if unescaped == result {
break
}
result = unescaped
}
return result, nil
}
// Handle a hit on a URL
func (s *visitlogserver) handleHit() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// parse out the cannonical URI
uri := r.FormValue("uri")
// sanity check
if uri == "" {
http.Error(w, "No Uri", http.StatusBadRequest)
log.Print("BAD REQUEST - No uri")
return
}
realURI, err := canonicalizeURI(uri)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Print("BAD REQUEST - bad uri - " + uri)
return
}
var record logrecord
if r.Method == "POST" {
record = s.db.updateURI(realURI, "hit")
} else {
record, _ = s.db.getURI(realURI, "hit")
}
log.Print(fmt.Sprintf("%s %d", realURI, record.Count))
result := vistresult{CannonicalURI: realURI,
Count: record.Count}
b, _ := json.Marshal(result)
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
}
func (s *visitlogserver) handleStats() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "What?", http.StatusBadRequest)
return
}
result, _ := s.db.DumpDatabaseRealm("hit")
w.Header().Set("Content-Type", "application/json")
w.Write(result)
}
}
func (s *visitlogserver) handleLastDayStats() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "What?", http.StatusBadRequest)
return
}
result, _ := s.db.DumpRollingLog()
w.Header().Set("Content-Type", "application/json")
w.Write(result)
}
}
func (s *visitlogserver) handleAnswer() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "What? Require POST", http.StatusBadRequest)
return
}
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
http.Error(w, "What? Bad content type", http.StatusBadRequest)
return
}
type AnswerRequest struct {
QuizID string
Question int
AnswerID string
}
var ar AnswerRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&ar)
if err != nil {
http.Error(w, "What? Body did not decode", http.StatusBadRequest)
return
}
if (ar.QuizID == "") || (ar.AnswerID == "") {
http.Error(w, "What? Body did not contain the required attributes", http.StatusBadRequest)
return
}
result, err := s.qm.SubmitAnswer(ar.QuizID, ar.Question, ar.AnswerID)
if err != nil {
log.Printf("QUIZ ERROR %s", err.Error())
http.Error(w, "What?", http.StatusInternalServerError)
return
}
jsonResult, _ := json.MarshalIndent(result, "", " ")
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResult)
}
}
func main() {
f, err := os.OpenFile("/var/log/visitlog", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
wrt := io.MultiWriter(os.Stdout, f)
log.SetOutput(wrt)
defer f.Close()
vs := &visitlogserver{db: MakeLogDatabase(), qm: MakeQuizManager(QuizDatabaseDirectory{path: "quizes"})}
vs.db.LoadDatabase("visitlogdb")
http.HandleFunc("/log", vs.handleHit())
http.HandleFunc("/stats", vs.handleStats())
http.HandleFunc("/quiz/submit", vs.handleAnswer())
http.HandleFunc("/recentstats", vs.handleLastDayStats())
log.Fatal(http.ListenAndServe(":8080", nil))
}