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"
|
|
|
|
|
2015-04-27 20:15:42 -04:00
|
|
|
"fmt"
|
2013-04-12 01:05:40 +02:00
|
|
|
"io"
|
2012-07-30 23:23:38 +02:00
|
|
|
"net/http"
|
2016-05-03 01:03:23 -04:00
|
|
|
"path"
|
2013-05-31 00:34:11 +02:00
|
|
|
"strings"
|
2014-08-30 01:25:16 -05:00
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-05-03 01:03:23 -04:00
|
|
|
const (
|
|
|
|
daysNewsIndexpage = 15
|
|
|
|
cacheMaxAge = 1800
|
|
|
|
|
|
|
|
epubFile = "book.epub"
|
|
|
|
)
|
|
|
|
|
2013-06-01 04:56:35 +02:00
|
|
|
type statusData struct {
|
2012-08-18 02:06:43 +02:00
|
|
|
S Status
|
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func aboutHandler(h handler) {
|
2013-06-01 04:56:35 +02:00
|
|
|
var data statusData
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2016-01-28 18:13:26 -05:00
|
|
|
data.S.Title = "About -- " + data.S.Title
|
2012-08-21 10:57:54 +02:00
|
|
|
data.S.About = true
|
2017-06-06 14:18:47 +00:00
|
|
|
h.load("about", data)
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func helpHandler(h handler) {
|
2013-06-01 04:56:35 +02:00
|
|
|
var data statusData
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2016-01-28 18:13:26 -05:00
|
|
|
data.S.Title = "Help -- " + data.S.Title
|
2013-06-01 04:56:35 +02:00
|
|
|
data.S.Help = true
|
2017-06-06 14:18:47 +00:00
|
|
|
h.load("help", data)
|
2013-06-01 04:56:35 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +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")
|
2013-09-23 16:27:31 +02:00
|
|
|
http.Redirect(h.w, h.r, "/", http.StatusFound)
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type bookData struct {
|
2016-07-30 07:46:35 -04:00
|
|
|
S Status
|
|
|
|
Book database.Book
|
|
|
|
Description []string
|
2012-08-15 13:17:27 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func bookHandler(h handler) {
|
2014-07-02 20:40:24 -05:00
|
|
|
id := mux.Vars(h.r)["id"]
|
2012-09-12 00:19:19 +02:00
|
|
|
var data bookData
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2016-07-30 07:59:30 -04:00
|
|
|
book, err := h.db.GetBookID(id)
|
2014-07-02 20:40:24 -05:00
|
|
|
if err != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
2012-08-15 13:58:16 +02:00
|
|
|
}
|
2014-07-02 20:40:24 -05:00
|
|
|
data.Book = book
|
2017-02-05 14:14:22 +00:00
|
|
|
|
|
|
|
author := ""
|
2016-07-30 07:36:58 -04:00
|
|
|
if len(book.Authors) > 0 {
|
|
|
|
author = " by " + book.Authors[0]
|
2017-02-05 14:14:22 +00:00
|
|
|
}
|
|
|
|
data.S.Title = book.Title + author + " -- " + data.S.Title
|
|
|
|
|
2013-05-31 00:34:11 +02:00
|
|
|
data.Description = strings.Split(data.Book.Description, "\n")
|
2017-06-06 14:18:47 +00:00
|
|
|
h.load("book", data)
|
2012-07-30 23:23:38 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func downloadHandler(h handler) {
|
2014-07-02 20:40:24 -05:00
|
|
|
id := mux.Vars(h.r)["id"]
|
2016-07-30 07:59:30 -04:00
|
|
|
book, err := h.db.GetBookID(id)
|
2014-07-02 20:40:24 -05:00
|
|
|
if err != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
notFound(h)
|
2013-04-12 01:05:40 +02:00
|
|
|
return
|
|
|
|
}
|
2013-04-12 01:44:00 +02:00
|
|
|
|
2016-07-30 07:59:30 -04:00
|
|
|
f, err := h.store.Get(book.ID, epubFile)
|
2013-04-12 01:05:40 +02:00
|
|
|
if err != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
notFound(h)
|
2013-04-12 01:05:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
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\""}
|
2017-07-06 08:13:15 +00:00
|
|
|
addCacheControlHeader(h.w, false)
|
2013-04-12 01:05:40 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
io.Copy(h.w, f)
|
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
|
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
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func indexHandler(h handler) {
|
2012-09-12 00:19:19 +02:00
|
|
|
var data indexData
|
2012-08-20 17:19:27 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
data.S.Home = true
|
2016-05-03 01:03:23 -04:00
|
|
|
data.News = getNews(1, daysNewsIndexpage, h.db)
|
2017-06-05 23:03:24 +00:00
|
|
|
|
|
|
|
frontPage := h.db.GetFrontPage()
|
|
|
|
data.Tags = frontPage.Tags
|
|
|
|
data.Books = frontPage.Last
|
|
|
|
data.Count = frontPage.Count
|
|
|
|
data.VisitedBooks = frontPage.Visited
|
|
|
|
data.DownloadedBooks = frontPage.Download
|
|
|
|
|
2017-06-06 14:18:47 +00:00
|
|
|
h.load("index", data)
|
2012-08-15 14:26:10 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func notFound(h handler) {
|
2013-06-01 20:51:21 +02:00
|
|
|
var data statusData
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2016-01-28 18:13:26 -05:00
|
|
|
data.S.Title = "Not found --" + data.S.Title
|
2013-09-23 16:27:31 +02:00
|
|
|
h.w.WriteHeader(http.StatusNotFound)
|
2017-06-06 14:18:47 +00:00
|
|
|
h.load("404", data)
|
2013-05-09 09:42:03 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:03:23 -04:00
|
|
|
func UpdateLogger(loggerConfig string) error {
|
|
|
|
logger, err := log.LoggerFromConfigAsFile(loggerConfig)
|
2014-02-11 12:59:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return log.ReplaceLogger(logger)
|
|
|
|
}
|
|
|
|
|
2017-06-05 16:17:14 +00:00
|
|
|
func InitRouter(db database.DB, sg *StatsGatherer, assetsPath string) http.Handler {
|
2016-05-03 01:03:23 -04:00
|
|
|
const idPattern = "[0-9a-zA-Z\\-\\_]{16}"
|
2014-08-21 19:24:23 -05:00
|
|
|
|
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
|
|
|
|
2016-05-03 01:03:23 -04:00
|
|
|
for _, file := range []string{"robots.txt", "description.json", "opensearch.xml", "key.asc"} {
|
2017-05-29 09:45:38 +00:00
|
|
|
path := path.Join(assetsPath, file)
|
2016-05-03 01:03:23 -04:00
|
|
|
serveFunc := func(w http.ResponseWriter, r *http.Request) {
|
2017-05-29 09:45:38 +00:00
|
|
|
http.ServeFile(w, r, path)
|
2016-05-03 01:03:23 -04:00
|
|
|
}
|
|
|
|
r.HandleFunc("/"+file, serveFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, folder := range []string{"img", "css", "js"} {
|
|
|
|
r.HandleFunc("/"+folder+"/{"+folder+"}", fileServer(path.Join(assetsPath, folder), "/"+folder+"/"))
|
|
|
|
}
|
|
|
|
|
|
|
|
r.HandleFunc("/book/{id:"+idPattern+"}", sg.Gather(bookHandler))
|
2014-08-21 19:24:23 -05:00
|
|
|
r.HandleFunc("/search/", sg.Gather(searchHandler))
|
2018-04-07 22:58:59 +00:00
|
|
|
|
2014-08-21 19:24:23 -05:00
|
|
|
r.HandleFunc("/upload/", sg.Gather(uploadHandler)).Methods("GET")
|
|
|
|
r.HandleFunc("/upload/", sg.Gather(uploadPostHandler)).Methods("POST")
|
2018-04-07 22:58:59 +00:00
|
|
|
r.HandleFunc("/submission/{submissionID:"+idPattern+"}", sg.Gather(submissionHandler))
|
|
|
|
r.HandleFunc("/submission/{submissionID:"+idPattern+"}/edit/{id:"+idPattern+"}", sg.Gather(editHandler))
|
|
|
|
r.HandleFunc("/submission/{submissionID:"+idPattern+"}/save/{id:"+idPattern+"}", sg.Gather(saveHandler)).Methods("POST")
|
|
|
|
r.HandleFunc("/submission/{submissionID:"+idPattern+"}/delete/{ids:(?:"+idPattern+"/)+}", sg.Gather(deleteHandler))
|
|
|
|
|
2016-05-03 01:03:23 -04:00
|
|
|
r.HandleFunc("/read/{id:"+idPattern+"}", sg.Gather(readStartHandler))
|
|
|
|
r.HandleFunc("/read/{id:"+idPattern+"}/{file:.*}", sg.Gather(readHandler))
|
|
|
|
r.HandleFunc("/content/{id:"+idPattern+"}/{file:.*}", sg.Gather(contentHandler))
|
2014-08-21 19:24:23 -05:00
|
|
|
r.HandleFunc("/about/", sg.Gather(aboutHandler))
|
|
|
|
r.HandleFunc("/help/", sg.Gather(helpHandler))
|
2016-05-03 01:03:23 -04:00
|
|
|
r.HandleFunc("/download/{id:"+idPattern+"}/{epub:.*}", sg.Gather(downloadHandler))
|
|
|
|
r.HandleFunc("/cover/{id:"+idPattern+"}/{size}/{img:.*}", sg.Gather(coverHandler))
|
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))
|
2016-05-03 01:03:23 -04:00
|
|
|
r.HandleFunc("/save/{id:"+idPattern+"}", sg.Gather(saveHandler)).Methods("POST")
|
|
|
|
r.HandleFunc("/edit/{id:"+idPattern+"}", sg.Gather(editHandler))
|
2017-02-05 00:59:56 +00:00
|
|
|
r.HandleFunc("/store/{ids:(?:"+idPattern+"/)+}", sg.Gather(storeHandler))
|
|
|
|
r.HandleFunc("/delete/{ids:(?:"+idPattern+"/)+}", sg.Gather(deleteHandler))
|
2014-09-07 02:49:39 -05:00
|
|
|
|
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
|
|
|
|
2017-06-05 16:17:14 +00:00
|
|
|
return r
|
2012-07-30 23:23:38 +02:00
|
|
|
}
|
2015-04-27 20:15:42 -04:00
|
|
|
|
2016-05-03 01:03:23 -04:00
|
|
|
func fileServer(servePath string, prefix string) func(w http.ResponseWriter, r *http.Request) {
|
2015-04-27 20:15:42 -04:00
|
|
|
// FIXME: is there a cleaner way without handler?
|
2016-05-03 01:03:23 -04:00
|
|
|
h := http.FileServer(http.Dir(servePath))
|
2015-04-27 20:15:42 -04:00
|
|
|
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) {
|
|
|
|
if private {
|
2016-05-03 01:03:23 -04:00
|
|
|
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, private", cacheMaxAge))
|
2015-04-27 20:15:42 -04:00
|
|
|
} else {
|
2016-05-03 01:03:23 -04:00
|
|
|
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", cacheMaxAge))
|
2015-04-27 20:15:42 -04:00
|
|
|
}
|
|
|
|
}
|