This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/trantor.go

201 lines
5.3 KiB
Go
Raw Normal View History

2012-07-30 23:23:38 +02:00
package main
import log "github.com/cihub/seelog"
2012-07-30 23:23:38 +02:00
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"
"net/http"
2013-05-31 00:34:11 +02:00
"strings"
2012-07-30 23:23:38 +02:00
)
2013-06-01 04:56:35 +02:00
type statusData struct {
2012-08-18 02:06:43 +02:00
S Status
}
func aboutHandler(h handler) {
2013-06-01 04:56:35 +02:00
var data statusData
data.S = GetStatus(h)
2012-08-21 10:57:54 +02:00
data.S.About = true
loadTemplate(h.w, "about", data)
2012-08-18 02:06:43 +02:00
}
func helpHandler(h handler) {
2013-06-01 04:56:35 +02:00
var data statusData
data.S = GetStatus(h)
2013-06-01 04:56:35 +02:00
data.S.Help = true
loadTemplate(h.w, "help", data)
2013-06-01 04:56:35 +02:00
}
func logoutHandler(h handler) {
h.sess.LogOut()
h.sess.Notify("Log out!", "Bye bye "+h.sess.User, "success")
h.sess.Save(h.w, h.r)
2014-02-11 13:13:43 +01:00
log.Info("User ", h.sess.User, " log out")
http.Redirect(h.w, h.r, "/", 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
}
func bookHandler(h handler) {
idStr := mux.Vars(h.r)["id"]
2013-05-09 09:42:58 +02:00
if !bson.IsObjectIdHex(idStr) {
notFound(h)
2013-05-09 09:42:58 +02:00
return
}
var data bookData
data.S = GetStatus(h)
2013-05-09 09:42:58 +02:00
id := bson.ObjectIdHex(idStr)
books, _, err := h.db.GetBooks(bson.M{"_id": id})
if err != nil || len(books) == 0 {
notFound(h)
return
2012-08-15 13:58:16 +02:00
}
data.Book = books[0]
2013-05-31 00:34:11 +02:00
data.Description = strings.Split(data.Book.Description, "\n")
loadTemplate(h.w, "book", data)
2012-07-30 23:23:38 +02:00
}
func downloadHandler(h handler) {
idStr := mux.Vars(h.r)["id"]
2013-05-09 09:42:58 +02:00
if !bson.IsObjectIdHex(idStr) {
notFound(h)
2013-05-09 09:42:58 +02:00
return
}
id := bson.ObjectIdHex(idStr)
books, _, err := h.db.GetBooks(bson.M{"_id": id})
2013-04-12 01:05:40 +02:00
if err != nil || len(books) == 0 {
notFound(h)
2013-04-12 01:05:40 +02:00
return
}
2013-04-12 01:44:00 +02:00
book := books[0]
if !book.Active {
if !h.sess.IsAdmin() {
notFound(h)
2013-04-12 01:44:00 +02:00
return
}
}
2013-04-12 01:05:40 +02:00
fs := h.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 {
notFound(h)
2013-04-12 01:05:40 +02:00
return
}
defer f.Close()
headers := h.w.Header()
2013-04-12 01:05:40 +02:00
headers["Content-Type"] = []string{"application/epub+zip"}
headers["Content-Disposition"] = []string{"attachment; filename=\"" + f.Name() + "\""}
io.Copy(h.w, f)
}
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
DownloadedBooks []Book
2012-10-26 13:44:08 +02:00
Count int
Tags []string
2013-07-18 11:42:46 +02:00
News []newsEntry
2012-08-20 17:19:27 +02:00
}
func indexHandler(h handler) {
var data indexData
2012-08-20 17:19:27 +02:00
2014-02-18 22:16:38 +01:00
data.Tags, _ = h.db.GetTags()
data.S = GetStatus(h)
data.S.Home = true
2014-02-18 21:50:44 +01:00
data.Books, data.Count, _ = h.db.GetBooks(bson.M{"active": true}, BOOKS_FRONT_PAGE)
2014-02-18 22:16:38 +01:00
data.VisitedBooks, _ = h.db.GetVisitedBooks()
data.DownloadedBooks, _ = h.db.GetDownloadedBooks()
data.News = getNews(1, DAYS_NEWS_INDEXPAGE, h.db)
loadTemplate(h.w, "index", data)
2012-08-15 14:26:10 +02:00
}
func notFound(h handler) {
2013-06-01 20:51:21 +02:00
var data statusData
data.S = GetStatus(h)
h.w.WriteHeader(http.StatusNotFound)
loadTemplate(h.w, "404", data)
2013-05-09 09:42:03 +02:00
}
2014-02-11 12:59:58 +01:00
func updateLogger() error {
logger, err := log.LoggerFromConfigAsFile(LOGGER_CONFIG)
if err != nil {
return err
}
return log.ReplaceLogger(logger)
}
2012-07-30 23:23:38 +02:00
func main() {
defer log.Flush()
2014-02-11 14:17:59 +01:00
err := updateLogger()
if err != nil {
log.Error("Error loading the logger xml: ", err)
}
log.Info("Start the imperial library of trantor")
db := initDB()
defer db.Close()
2012-07-30 23:23:38 +02:00
InitTasks(db)
InitStats(db)
InitUpload(db)
2013-04-22 23:28:00 +02:00
2014-02-09 18:38:43 +01:00
initRouter(db)
2014-02-11 11:29:15 +01:00
log.Error(http.ListenAndServe(":"+PORT, nil))
}
2014-02-09 18:38:43 +01:00
func initRouter(db *DB) {
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(notFound, db)
2013-05-09 09:42:03 +02:00
r.NotFoundHandler = notFoundHandler
r.HandleFunc("/", GatherStats(indexHandler, db))
r.HandleFunc("/book/{id:[0-9a-fA-F]+}", GatherStats(bookHandler, db))
r.HandleFunc("/search/", GatherStats(searchHandler, db))
r.HandleFunc("/upload/", GatherStats(uploadHandler, db)).Methods("GET")
r.HandleFunc("/upload/", GatherStats(uploadPostHandler, db)).Methods("POST")
r.HandleFunc("/login/", GatherStats(loginHandler, db)).Methods("GET")
r.HandleFunc("/login/", GatherStats(loginPostHandler, db)).Methods("POST")
r.HandleFunc("/create_user/", GatherStats(createUserHandler, db)).Methods("POST")
r.HandleFunc("/logout/", GatherStats(logoutHandler, db))
r.HandleFunc("/new/", GatherStats(newHandler, db))
r.HandleFunc("/store/{ids:([0-9a-fA-F]+/)+}", GatherStats(storeHandler, db))
r.HandleFunc("/delete/{ids:([0-9a-fA-F]+/)+}", GatherStats(deleteHandler, db))
r.HandleFunc("/read/{id:[0-9a-fA-F]+}", GatherStats(readStartHandler, db))
r.HandleFunc("/read/{id:[0-9a-fA-F]+}/{file:.*}", GatherStats(readHandler, db))
r.HandleFunc("/content/{id:[0-9a-fA-F]+}/{file:.*}", GatherStats(contentHandler, db))
r.HandleFunc("/edit/{id:[0-9a-fA-F]+}", GatherStats(editHandler, db))
r.HandleFunc("/save/{id:[0-9a-fA-F]+}", GatherStats(saveHandler, db)).Methods("POST")
r.HandleFunc("/about/", GatherStats(aboutHandler, db))
r.HandleFunc("/help/", GatherStats(helpHandler, db))
r.HandleFunc("/download/{id:[0-9a-fA-F]+}/{epub:.*}", GatherStats(downloadHandler, db))
r.HandleFunc("/cover/{id:[0-9a-fA-F]+}/{size}/{img:.*}", GatherStats(coverHandler, db))
r.HandleFunc("/settings/", GatherStats(settingsHandler, db))
r.HandleFunc("/stats/", GatherStats(statsHandler, db))
r.HandleFunc("/news/", GatherStats(newsHandler, db))
r.HandleFunc("/news/edit", GatherStats(editNewsHandler, db)).Methods("GET")
r.HandleFunc("/news/edit", GatherStats(postNewsHandler, db)).Methods("POST")
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
}