Generate stats from visits and downloads

This commit is contained in:
Las Zenow 2012-09-13 01:03:07 +02:00
parent f3882881ce
commit b96215b7ff
2 changed files with 18 additions and 3 deletions

View file

@ -75,8 +75,16 @@ func (d *DB) RemoveBook(id bson.ObjectId) error {
return d.books.Remove(bson.M{"_id": id})
}
func (d *DB) UpdateBook(id bson.ObjectId, book interface{}) error {
return d.books.Update(bson.M{"_id": id}, bson.M{"$set": book})
func (d *DB) UpdateBook(id bson.ObjectId, data interface{}) error {
return d.books.Update(bson.M{"_id": id}, bson.M{"$set": data})
}
func (d *DB) IncVisit(id bson.ObjectId) error {
return d.books.Update(bson.M{"_id": id}, bson.M{"$inc": bson.M{"VisitsCount": 1}})
}
func (d *DB) IncDownload(path string) error {
return d.books.Update(bson.M{"path": path}, bson.M{"$inc": bson.M{"DownloadCount": 1}})
}
/* optional parameters: length and start index

View file

@ -55,10 +55,17 @@ func bookHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
db.IncVisit(id)
data.Book = books[0]
loadTemplate(w, "book", data)
}
func downloadHandler(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[1:]
db.IncDownload(file)
http.ServeFile(w, r, file)
}
func fileHandler(path string) {
h := http.FileServer(http.Dir(path[1:]))
http.Handle(path, http.StripPrefix(path, h))
@ -131,9 +138,9 @@ func main() {
http.HandleFunc("/save/", saveHandler)
http.HandleFunc("/delete/", deleteHandler)
http.HandleFunc("/about/", aboutHandler)
http.HandleFunc("/books/", downloadHandler)
fileHandler("/img/")
fileHandler("/cover/")
fileHandler("/books/")
fileHandler("/css/")
fileHandler("/js/")
http.HandleFunc("/", indexHandler)