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

227 lines
6.5 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"
"path"
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
)
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
}
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
h.load("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
h.load("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 {
2016-07-30 07:46:35 -04:00
S Status
Book database.Book
Description []string
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)
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 {
notFound(h)
return
2012-08-15 13:58:16 +02:00
}
2014-07-02 20:40:24 -05:00
data.Book = book
author := ""
if len(book.Authors) > 0 {
author = " by " + book.Authors[0]
}
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")
h.load("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"]
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 {
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 {
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\""}
addCacheControlHeader(h.w, false)
2013-04-12 01:05:40 +02:00
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
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
data.S = GetStatus(h)
data.S.Home = true
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
h.load("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)
h.load("404", data)
2013-05-09 09:42:03 +02: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 {
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
for _, file := range []string{"robots.txt", "description.json", "opensearch.xml", "key.asc"} {
path := path.Join(assetsPath, file)
serveFunc := func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path)
}
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))
2014-08-21 19:24:23 -05:00
r.HandleFunc("/upload/", sg.Gather(uploadHandler)).Methods("GET")
r.HandleFunc("/upload/", sg.Gather(uploadPostHandler)).Methods("POST")
r.HandleFunc("/submission/{submissionID:"+idPattern+"}", sg.Gather(submissionHandler))
2018-04-07 23:42:41 +00:00
r.HandleFunc("/submission/{submissionID:"+idPattern+"}/comment/{id:"+idPattern+"}", sg.Gather(submissionCommentHandler)).Methods("POST")
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))
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))
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))
r.HandleFunc("/save/{id:"+idPattern+"}", sg.Gather(saveHandler)).Methods("POST")
r.HandleFunc("/edit/{id:"+idPattern+"}", sg.Gather(editHandler))
r.HandleFunc("/store/{ids:(?:"+idPattern+"/)+}", sg.Gather(storeHandler))
r.HandleFunc("/delete/{ids:(?:"+idPattern+"/)+}", sg.Gather(deleteHandler))
2018-04-08 10:55:13 +00:00
r.HandleFunc("/admin/users/", sg.Gather(userAdminHandler)).Methods("GET")
r.HandleFunc("/admin/users/", sg.Gather(userAdminPostHandler)).Methods("POST")
2018-04-08 11:11:27 +00:00
r.HandleFunc("/admin/users/add/", sg.Gather(addUserHandler)).Methods("POST")
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
}
func fileServer(servePath string, prefix string) func(w http.ResponseWriter, r *http.Request) {
// FIXME: is there a cleaner way without handler?
h := http.FileServer(http.Dir(servePath))
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 {
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, private", cacheMaxAge))
} else {
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", cacheMaxAge))
}
}