2012-07-30 23:23:38 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-04-15 00:37:49 +02:00
|
|
|
"github.com/gorilla/mux"
|
2013-04-12 01:05:40 +02:00
|
|
|
"io"
|
2012-07-30 23:23:38 +02:00
|
|
|
"labix.org/v2/mgo/bson"
|
2012-10-29 21:53:04 +01:00
|
|
|
"log"
|
2012-07-30 23:23:38 +02:00
|
|
|
"net/http"
|
2013-05-31 00:34:11 +02:00
|
|
|
"strings"
|
2012-07-30 23:23:38 +02:00
|
|
|
)
|
|
|
|
|
2012-08-18 02:06:43 +02:00
|
|
|
type aboutData struct {
|
|
|
|
S Status
|
|
|
|
}
|
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
func aboutHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
2012-08-18 02:06:43 +02:00
|
|
|
var data aboutData
|
2012-08-19 02:29:34 +02:00
|
|
|
data.S = GetStatus(w, r)
|
2012-08-21 10:57:54 +02:00
|
|
|
data.S.About = true
|
2012-08-18 02:06:43 +02:00
|
|
|
loadTemplate(w, "about", data)
|
|
|
|
}
|
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
func logoutHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
2012-08-19 02:29:34 +02:00
|
|
|
sess.LogOut()
|
|
|
|
sess.Notify("Log out!", "Bye bye "+sess.User, "success")
|
|
|
|
sess.Save(w, r)
|
2012-10-29 21:53:04 +01:00
|
|
|
log.Println("User", sess.User, "log out")
|
2012-10-29 21:52:02 +01:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
func loginHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
2013-04-15 00:59:19 +02:00
|
|
|
user := r.FormValue("user")
|
|
|
|
pass := r.FormValue("pass")
|
|
|
|
if db.UserValid(user, pass) {
|
|
|
|
log.Println("User", user, "log in")
|
|
|
|
sess.LogIn(user)
|
|
|
|
sess.Notify("Successful login!", "Welcome "+user, "success")
|
|
|
|
} else {
|
|
|
|
log.Println("User", user, "bad user or password")
|
|
|
|
sess.Notify("Invalid login!", "user or password invalid", "error")
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
2013-04-15 00:59:19 +02:00
|
|
|
sess.Save(w, r)
|
2012-10-29 21:52:02 +01:00
|
|
|
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type bookData struct {
|
2013-05-31 00:34:11 +02:00
|
|
|
S Status
|
|
|
|
Book Book
|
|
|
|
Description []string
|
2012-08-15 13:17:27 +02:00
|
|
|
}
|
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
2013-05-09 09:42:58 +02:00
|
|
|
idStr := mux.Vars(r)["id"]
|
|
|
|
if !bson.IsObjectIdHex(idStr) {
|
|
|
|
notFound(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
var data bookData
|
|
|
|
data.S = GetStatus(w, r)
|
2013-05-09 09:42:58 +02:00
|
|
|
id := bson.ObjectIdHex(idStr)
|
2012-09-12 00:19:19 +02:00
|
|
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
|
|
|
if err != nil || len(books) == 0 {
|
2013-05-09 09:42:03 +02:00
|
|
|
notFound(w)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
2012-08-15 13:58:16 +02:00
|
|
|
}
|
2012-09-13 01:03:07 +02:00
|
|
|
db.IncVisit(id)
|
2012-09-12 00:19:19 +02:00
|
|
|
data.Book = books[0]
|
2013-05-31 00:34:11 +02:00
|
|
|
data.Description = strings.Split(data.Book.Description, "\n")
|
2012-09-12 00:19:19 +02:00
|
|
|
loadTemplate(w, "book", data)
|
2012-07-30 23:23:38 +02:00
|
|
|
}
|
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
2013-05-09 09:42:58 +02:00
|
|
|
idStr := mux.Vars(r)["id"]
|
|
|
|
if !bson.IsObjectIdHex(idStr) {
|
|
|
|
notFound(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := bson.ObjectIdHex(idStr)
|
2013-04-12 01:05:40 +02:00
|
|
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
|
|
|
if err != nil || len(books) == 0 {
|
2013-05-09 09:42:03 +02:00
|
|
|
notFound(w)
|
2013-04-12 01:05:40 +02:00
|
|
|
return
|
|
|
|
}
|
2013-04-12 01:44:00 +02:00
|
|
|
book := books[0]
|
|
|
|
|
|
|
|
if !book.Active {
|
|
|
|
sess := GetSession(r)
|
|
|
|
if sess.User == "" {
|
2013-05-09 09:42:03 +02:00
|
|
|
notFound(w)
|
2013-04-12 01:44:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 01:05:40 +02:00
|
|
|
|
|
|
|
fs := db.GetFS(FS_BOOKS)
|
2013-04-12 01:44:00 +02:00
|
|
|
f, err := fs.OpenId(book.File)
|
2013-04-12 01:05:40 +02:00
|
|
|
if err != nil {
|
2013-05-09 09:42:03 +02:00
|
|
|
notFound(w)
|
2013-04-12 01:05:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
headers := w.Header()
|
|
|
|
headers["Content-Type"] = []string{"application/epub+zip"}
|
|
|
|
headers["Content-Disposition"] = []string{"attachment; filename=\"" + f.Name() + "\""}
|
|
|
|
|
|
|
|
io.Copy(w, f)
|
|
|
|
db.IncDownload(id)
|
2012-09-13 01:03:07 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:26:10 +02:00
|
|
|
type indexData struct {
|
2012-10-26 13:44:08 +02:00
|
|
|
S Status
|
|
|
|
Books []Book
|
|
|
|
VisitedBooks []Book
|
2012-10-24 23:22:39 +02:00
|
|
|
DownloadedBooks []Book
|
2012-10-26 13:44:08 +02:00
|
|
|
Count int
|
|
|
|
Tags []string
|
2012-08-20 17:19:27 +02:00
|
|
|
}
|
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
2012-09-12 00:19:19 +02:00
|
|
|
var data indexData
|
2012-08-20 17:19:27 +02:00
|
|
|
|
2013-03-20 00:54:40 +01:00
|
|
|
data.Tags, _ = db.GetTags(TAGS_DISPLAY)
|
2012-09-12 00:19:19 +02:00
|
|
|
data.S = GetStatus(w, r)
|
|
|
|
data.S.Home = true
|
|
|
|
data.Books, data.Count, _ = db.GetBooks(bson.M{"active": true}, 6)
|
2012-10-24 23:22:39 +02:00
|
|
|
data.VisitedBooks, _ = db.GetVisitedBooks(6)
|
|
|
|
data.DownloadedBooks, _ = db.GetDownloadedBooks(6)
|
2012-09-12 00:19:19 +02:00
|
|
|
loadTemplate(w, "index", data)
|
2012-08-15 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
2013-05-09 09:42:03 +02:00
|
|
|
func notFound(w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
loadTemplate(w, "404", nil)
|
|
|
|
}
|
|
|
|
|
2012-07-30 23:23:38 +02:00
|
|
|
func main() {
|
2012-09-12 00:19:19 +02:00
|
|
|
db = initDB()
|
|
|
|
defer db.Close()
|
2012-07-30 23:23:38 +02:00
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
InitStats()
|
2013-05-03 00:43:26 +02:00
|
|
|
InitUpload()
|
2013-04-22 23:28:00 +02:00
|
|
|
|
2013-04-16 13:39:22 +02:00
|
|
|
setUpRouter()
|
|
|
|
panic(http.ListenAndServe(":"+PORT, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setUpRouter() {
|
2013-04-15 00:37:49 +02:00
|
|
|
r := mux.NewRouter()
|
2013-05-09 09:42:03 +02:00
|
|
|
var notFoundHandler http.HandlerFunc
|
|
|
|
notFoundHandler = GatherStats(func(w http.ResponseWriter, r *http.Request, sess *Session) { notFound(w) })
|
|
|
|
r.NotFoundHandler = notFoundHandler
|
|
|
|
|
2013-04-22 23:28:00 +02:00
|
|
|
r.HandleFunc("/", GatherStats(indexHandler))
|
|
|
|
r.HandleFunc("/book/{id:[0-9a-fA-F]+}", GatherStats(bookHandler))
|
|
|
|
r.HandleFunc("/search/", GatherStats(searchHandler))
|
|
|
|
r.HandleFunc("/upload/", GatherStats(uploadHandler)).Methods("GET")
|
|
|
|
r.HandleFunc("/upload/", GatherStats(uploadPostHandler)).Methods("POST")
|
|
|
|
r.HandleFunc("/login/", GatherStats(loginHandler)).Methods("POST")
|
|
|
|
r.HandleFunc("/logout/", GatherStats(logoutHandler))
|
|
|
|
r.HandleFunc("/new/", GatherStats(newHandler))
|
|
|
|
r.HandleFunc("/store/{ids:([0-9a-fA-F]+/)+}", GatherStats(storeHandler))
|
|
|
|
r.HandleFunc("/delete/{ids:([0-9a-fA-F]+/)+}", GatherStats(deleteHandler))
|
|
|
|
r.HandleFunc("/read/{id:[0-9a-fA-F]+}", GatherStats(readStartHandler))
|
|
|
|
r.HandleFunc("/read/{id:[0-9a-fA-F]+}/{file:.*}", GatherStats(readHandler))
|
|
|
|
r.HandleFunc("/content/{id:[0-9a-fA-F]+}/{file:.*}", GatherStats(contentHandler))
|
|
|
|
r.HandleFunc("/edit/{id:[0-9a-fA-F]+}", GatherStats(editHandler))
|
|
|
|
r.HandleFunc("/save/{id:[0-9a-fA-F]+}", GatherStats(saveHandler)).Methods("POST")
|
|
|
|
r.HandleFunc("/about/", GatherStats(aboutHandler))
|
|
|
|
r.HandleFunc("/download/{id:[0-9a-fA-F]+}/{epub:.*}", GatherStats(downloadHandler))
|
2013-04-15 22:10:48 +02:00
|
|
|
r.HandleFunc("/cover/{id:[0-9a-fA-F]+}/{size}/{img:.*}", coverHandler)
|
2013-04-22 23:28:00 +02:00
|
|
|
r.HandleFunc("/settings/", GatherStats(settingsHandler))
|
2012-10-28 19:21:42 +01:00
|
|
|
h := http.FileServer(http.Dir(IMG_PATH))
|
2013-04-15 00:37:49 +02:00
|
|
|
r.Handle("/img/{img}", http.StripPrefix("/img/", h))
|
2012-10-28 19:21:42 +01:00
|
|
|
h = http.FileServer(http.Dir(CSS_PATH))
|
2013-04-15 00:37:49 +02:00
|
|
|
r.Handle("/css/{css}", http.StripPrefix("/css/", h))
|
2012-10-28 19:21:42 +01:00
|
|
|
h = http.FileServer(http.Dir(JS_PATH))
|
2013-04-15 00:37:49 +02:00
|
|
|
r.Handle("/js/{js}", http.StripPrefix("/js/", h))
|
|
|
|
http.Handle("/", r)
|
2012-07-30 23:23:38 +02:00
|
|
|
}
|