Convert config.go into command line params
This commit is contained in:
parent
9d1f1ad5c0
commit
0e8f1e7b56
14 changed files with 217 additions and 189 deletions
|
@ -6,12 +6,21 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"gitlab.com/trantor/trantor/lib/database"
|
||||
)
|
||||
|
||||
const (
|
||||
booksFrontPage = 6
|
||||
daysNewsIndexpage = 15
|
||||
cacheMaxAge = 1800
|
||||
|
||||
epubFile = "book.epub"
|
||||
)
|
||||
|
||||
type statusData struct {
|
||||
S Status
|
||||
}
|
||||
|
@ -21,7 +30,7 @@ func aboutHandler(h handler) {
|
|||
data.S = GetStatus(h)
|
||||
data.S.Title = "About -- " + data.S.Title
|
||||
data.S.About = true
|
||||
loadTemplate(h, "about", data)
|
||||
h.template.load(h, "about", data)
|
||||
}
|
||||
|
||||
func helpHandler(h handler) {
|
||||
|
@ -29,7 +38,7 @@ func helpHandler(h handler) {
|
|||
data.S = GetStatus(h)
|
||||
data.S.Title = "Help -- " + data.S.Title
|
||||
data.S.Help = true
|
||||
loadTemplate(h, "help", data)
|
||||
h.template.load(h, "help", data)
|
||||
}
|
||||
|
||||
func logoutHandler(h handler) {
|
||||
|
@ -66,7 +75,7 @@ func bookHandler(h handler) {
|
|||
break
|
||||
}
|
||||
}
|
||||
loadTemplate(h, "book", data)
|
||||
h.template.load(h, "book", data)
|
||||
}
|
||||
|
||||
func downloadHandler(h handler) {
|
||||
|
@ -84,7 +93,7 @@ func downloadHandler(h handler) {
|
|||
}
|
||||
}
|
||||
|
||||
f, err := h.store.Get(book.Id, EPUB_FILE)
|
||||
f, err := h.store.Get(book.Id, epubFile)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
|
@ -129,11 +138,11 @@ func indexHandler(h handler) {
|
|||
data.Tags, _ = h.db.GetTags()
|
||||
data.S = GetStatus(h)
|
||||
data.S.Home = true
|
||||
data.Books, data.Count, _ = h.db.GetBooks("", BOOKS_FRONT_PAGE, 0)
|
||||
data.Books, data.Count, _ = h.db.GetBooks("", booksFrontPage, 0)
|
||||
data.VisitedBooks, _ = h.db.GetVisitedBooks()
|
||||
data.DownloadedBooks, _ = h.db.GetDownloadedBooks()
|
||||
data.News = getNews(1, DAYS_NEWS_INDEXPAGE, h.db)
|
||||
loadTemplate(h, "index", data)
|
||||
data.News = getNews(1, daysNewsIndexpage, h.db)
|
||||
h.template.load(h, "index", data)
|
||||
}
|
||||
|
||||
func notFound(h handler) {
|
||||
|
@ -142,11 +151,11 @@ func notFound(h handler) {
|
|||
data.S = GetStatus(h)
|
||||
data.S.Title = "Not found --" + data.S.Title
|
||||
h.w.WriteHeader(http.StatusNotFound)
|
||||
loadTemplate(h, "404", data)
|
||||
h.template.load(h, "404", data)
|
||||
}
|
||||
|
||||
func UpdateLogger() error {
|
||||
logger, err := log.LoggerFromConfigAsFile(LOGGER_CONFIG)
|
||||
func UpdateLogger(loggerConfig string) error {
|
||||
logger, err := log.LoggerFromConfigAsFile(loggerConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -154,8 +163,8 @@ func UpdateLogger() error {
|
|||
return log.ReplaceLogger(logger)
|
||||
}
|
||||
|
||||
func InitRouter(db *database.DB, sg *StatsGatherer) {
|
||||
const id_pattern = "[0-9a-zA-Z\\-\\_]{16}"
|
||||
func InitRouter(db *database.DB, sg *StatsGatherer, assetsPath string) {
|
||||
const idPattern = "[0-9a-zA-Z\\-\\_]{16}"
|
||||
|
||||
r := mux.NewRouter()
|
||||
var notFoundHandler http.HandlerFunc
|
||||
|
@ -163,24 +172,31 @@ func InitRouter(db *database.DB, sg *StatsGatherer) {
|
|||
r.NotFoundHandler = notFoundHandler
|
||||
|
||||
r.HandleFunc("/", sg.Gather(indexHandler))
|
||||
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) })
|
||||
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) })
|
||||
|
||||
r.HandleFunc("/book/{id:"+id_pattern+"}", sg.Gather(bookHandler))
|
||||
for _, file := range []string{"robots.txt", "description.json", "opensearch.xml", "key.asc"} {
|
||||
serveFunc := func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, path.Join(assetsPath, file))
|
||||
}
|
||||
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))
|
||||
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("/read/{id:"+idPattern+"}", sg.Gather(readStartHandler))
|
||||
r.HandleFunc("/read/{id:"+idPattern+"}/{file:.*}", sg.Gather(readHandler))
|
||||
r.HandleFunc("/content/{id:"+idPattern+"}/{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))
|
||||
r.HandleFunc("/download/{id:"+idPattern+"}/{epub:.*}", sg.Gather(downloadHandler))
|
||||
r.HandleFunc("/cover/{id:"+idPattern+"}/{size}/{img:.*}", sg.Gather(coverHandler))
|
||||
r.HandleFunc("/stats/", sg.Gather(statsHandler))
|
||||
r.HandleFunc("/flag/bad_quality/{id:"+id_pattern+"}", sg.Gather(flagHandler))
|
||||
r.HandleFunc("/flag/bad_quality/{id:"+idPattern+"}", sg.Gather(flagHandler))
|
||||
|
||||
r.HandleFunc("/login/", sg.Gather(loginHandler)).Methods("GET")
|
||||
r.HandleFunc("/login/", sg.Gather(loginPostHandler)).Methods("POST")
|
||||
|
@ -190,24 +206,21 @@ func InitRouter(db *database.DB, sg *StatsGatherer) {
|
|||
r.HandleFunc("/settings/", sg.Gather(settingsHandler))
|
||||
|
||||
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))
|
||||
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))
|
||||
|
||||
r.HandleFunc("/news/", sg.Gather(newsHandler))
|
||||
r.HandleFunc("/news/edit", sg.Gather(editNewsHandler)).Methods("GET")
|
||||
r.HandleFunc("/news/edit", sg.Gather(postNewsHandler)).Methods("POST")
|
||||
|
||||
r.HandleFunc("/img/{img}", fileServer(IMG_PATH, "/img/"))
|
||||
r.HandleFunc("/css/{css}", fileServer(CSS_PATH, "/css/"))
|
||||
r.HandleFunc("/js/{js}", fileServer(JS_PATH, "/js/"))
|
||||
http.Handle("/", r)
|
||||
}
|
||||
|
||||
func fileServer(path string, prefix string) func(w http.ResponseWriter, r *http.Request) {
|
||||
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(path))
|
||||
h := http.FileServer(http.Dir(servePath))
|
||||
handler := http.StripPrefix(prefix, h)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
addCacheControlHeader(w, false)
|
||||
|
@ -218,8 +231,8 @@ func fileServer(path string, prefix string) func(w http.ResponseWriter, r *http.
|
|||
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))
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, private", cacheMaxAge))
|
||||
} else {
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", CACHE_MAX_AGE))
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", cacheMaxAge))
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue