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/lib/trantor.go

226 lines
6.6 KiB
Go
Raw Normal View History

2016-05-02 21:36:49 -04:00
package trantor
2012-07-30 23:23:38 +02:00
import (
2014-08-30 13:17:50 -05:00
log "github.com/cihub/seelog"
"fmt"
2013-04-12 01:05:40 +02:00
"io"
2012-07-30 23:23:38 +02:00
"net/http"
2013-05-31 00:34:11 +02:00
"strings"
"github.com/gorilla/mux"
2016-05-02 21:36:49 -04:00
"gitlab.com/trantor/trantor/lib/database"
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)
data.S.Title = "About -- " + data.S.Title
2012-08-21 10:57:54 +02:00
data.S.About = true
loadTemplate(h, "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)
data.S.Title = "Help -- " + data.S.Title
2013-06-01 04:56:35 +02:00
data.S.Help = true
loadTemplate(h, "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 {
2014-09-07 21:13:52 -05:00
S Status
Book database.Book
Description []string
FlaggedBadQuality bool
2012-08-15 13:17:27 +02:00
}
func bookHandler(h handler) {
2014-07-02 20:40:24 -05:00
id := mux.Vars(h.r)["id"]
var data bookData
data.S = GetStatus(h)
2014-07-02 20:40:24 -05:00
book, err := h.db.GetBookId(id)
if err != nil {
notFound(h)
return
2012-08-15 13:58:16 +02:00
}
2014-07-02 20:40:24 -05:00
data.Book = book
data.S.Title = book.Title + " by " + book.Author[0] + " -- " + data.S.Title
2013-05-31 00:34:11 +02:00
data.Description = strings.Split(data.Book.Description, "\n")
2014-09-07 21:13:52 -05:00
data.FlaggedBadQuality = false
for _, reporter := range book.BadQualityReporters {
if reporter == h.sess.User || reporter == h.sess.Id() {
data.FlaggedBadQuality = true
break
}
}
loadTemplate(h, "book", data)
2012-07-30 23:23:38 +02:00
}
func downloadHandler(h handler) {
2014-07-02 20:40:24 -05:00
id := mux.Vars(h.r)["id"]
book, err := h.db.GetBookId(id)
if err != nil {
notFound(h)
2013-04-12 01:05:40 +02:00
return
}
2013-04-12 01:44:00 +02:00
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
2014-08-21 19:24:23 -05:00
f, err := h.store.Get(book.Id, EPUB_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"}
2014-08-21 19:24:23 -05:00
headers["Content-Disposition"] = []string{"attachment; filename=\"" + book.Title + ".epub\""}
2013-04-12 01:05:40 +02:00
io.Copy(h.w, f)
}
2014-09-07 20:22:24 -05:00
func flagHandler(h handler) {
id := mux.Vars(h.r)["id"]
2014-09-07 21:13:52 -05:00
user := h.sess.Id()
if h.sess.User != "" {
user = h.sess.User
}
err := h.db.FlagBadQuality(id, user)
2014-09-07 20:22:24 -05:00
if err != nil {
log.Warn("An error ocurred while flaging ", id, ": ", err)
}
h.sess.Notify("Flagged!", "Book marked as bad quality, thank you", "success")
h.sess.Save(h.w, h.r)
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
}
2012-08-15 14:26:10 +02:00
type indexData struct {
2012-10-26 13:44:08 +02:00
S Status
2014-06-29 19:41:29 -05:00
Books []database.Book
VisitedBooks []database.Book
DownloadedBooks []database.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
data.Books, data.Count, _ = h.db.GetBooks("", BOOKS_FRONT_PAGE, 0)
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, "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)
data.S.Title = "Not found --" + data.S.Title
h.w.WriteHeader(http.StatusNotFound)
loadTemplate(h, "404", data)
2013-05-09 09:42:03 +02:00
}
2016-05-02 21:36:49 -04:00
func UpdateLogger() error {
2014-02-11 12:59:58 +01:00
logger, err := log.LoggerFromConfigAsFile(LOGGER_CONFIG)
if err != nil {
return err
}
return log.ReplaceLogger(logger)
}
2016-05-02 21:36:49 -04:00
func InitRouter(db *database.DB, sg *StatsGatherer) {
2014-08-21 19:24:23 -05:00
const id_pattern = "[0-9a-zA-Z\\-\\_]{16}"
2013-04-15 00:37:49 +02:00
r := mux.NewRouter()
2013-05-09 09:42:03 +02:00
var notFoundHandler http.HandlerFunc
2014-08-21 19:24:23 -05:00
notFoundHandler = sg.Gather(notFound)
2013-05-09 09:42:03 +02:00
r.NotFoundHandler = notFoundHandler
2014-08-21 19:24:23 -05:00
r.HandleFunc("/", sg.Gather(indexHandler))
2014-09-07 02:49:39 -05:00
r.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, ROBOTS_PATH) })
r.HandleFunc("/description.json", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, DESCRIPTION_PATH) })
2015-01-17 02:19:14 -06:00
r.HandleFunc("/opensearch.xml", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, OPENSEARCH_PATH) })
r.HandleFunc("/key.asc", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, KEY_PATH) })
2014-09-07 02:49:39 -05:00
2014-08-21 19:24:23 -05:00
r.HandleFunc("/book/{id:"+id_pattern+"}", sg.Gather(bookHandler))
r.HandleFunc("/search/", sg.Gather(searchHandler))
r.HandleFunc("/upload/", sg.Gather(uploadHandler)).Methods("GET")
r.HandleFunc("/upload/", sg.Gather(uploadPostHandler)).Methods("POST")
r.HandleFunc("/read/{id:"+id_pattern+"}", sg.Gather(readStartHandler))
r.HandleFunc("/read/{id:"+id_pattern+"}/{file:.*}", sg.Gather(readHandler))
r.HandleFunc("/content/{id:"+id_pattern+"}/{file:.*}", sg.Gather(contentHandler))
r.HandleFunc("/about/", sg.Gather(aboutHandler))
r.HandleFunc("/help/", sg.Gather(helpHandler))
r.HandleFunc("/download/{id:"+id_pattern+"}/{epub:.*}", sg.Gather(downloadHandler))
r.HandleFunc("/cover/{id:"+id_pattern+"}/{size}/{img:.*}", sg.Gather(coverHandler))
2014-09-07 02:49:39 -05:00
r.HandleFunc("/stats/", sg.Gather(statsHandler))
2014-09-07 20:22:24 -05:00
r.HandleFunc("/flag/bad_quality/{id:"+id_pattern+"}", sg.Gather(flagHandler))
2014-09-07 02:49:39 -05:00
r.HandleFunc("/login/", sg.Gather(loginHandler)).Methods("GET")
r.HandleFunc("/login/", sg.Gather(loginPostHandler)).Methods("POST")
r.HandleFunc("/create_user/", sg.Gather(createUserHandler)).Methods("POST")
r.HandleFunc("/logout/", sg.Gather(logoutHandler))
2014-08-21 19:24:23 -05:00
r.HandleFunc("/dashboard/", sg.Gather(dashboardHandler))
r.HandleFunc("/settings/", sg.Gather(settingsHandler))
2014-09-07 02:49:39 -05:00
r.HandleFunc("/new/", sg.Gather(newHandler))
r.HandleFunc("/save/{id:"+id_pattern+"}", sg.Gather(saveHandler)).Methods("POST")
r.HandleFunc("/edit/{id:"+id_pattern+"}", sg.Gather(editHandler))
r.HandleFunc("/store/{ids:("+id_pattern+"/)+}", sg.Gather(storeHandler))
r.HandleFunc("/delete/{ids:("+id_pattern+"/)+}", sg.Gather(deleteHandler))
2014-08-21 19:24:23 -05:00
r.HandleFunc("/news/", sg.Gather(newsHandler))
r.HandleFunc("/news/edit", sg.Gather(editNewsHandler)).Methods("GET")
r.HandleFunc("/news/edit", sg.Gather(postNewsHandler)).Methods("POST")
2014-09-07 02:49:39 -05:00
r.HandleFunc("/img/{img}", fileServer(IMG_PATH, "/img/"))
r.HandleFunc("/css/{css}", fileServer(CSS_PATH, "/css/"))
r.HandleFunc("/js/{js}", fileServer(JS_PATH, "/js/"))
2013-04-15 00:37:49 +02:00
http.Handle("/", r)
2012-07-30 23:23:38 +02:00
}
func fileServer(path string, prefix string) func(w http.ResponseWriter, r *http.Request) {
// FIXME: is there a cleaner way without handler?
h := http.FileServer(http.Dir(path))
handler := http.StripPrefix(prefix, h)
return func(w http.ResponseWriter, r *http.Request) {
addCacheControlHeader(w, false)
handler.ServeHTTP(w, r)
}
}
func addCacheControlHeader(w http.ResponseWriter, private bool) {
// FIXME: cache of download and cover don't depends on user login
if private {
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, private", CACHE_MAX_AGE))
} else {
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", CACHE_MAX_AGE))
}
}