Convert config.go into command line params

This commit is contained in:
Las Zenow 2016-05-03 01:03:23 -04:00
parent 9d1f1ad5c0
commit 0e8f1e7b56
14 changed files with 217 additions and 189 deletions

View file

@ -1,14 +1,15 @@
package trantor
import (
html_tmpl "html/template"
txt_tmpl "text/template"
log "github.com/cihub/seelog"
"encoding/json"
"errors"
"html/template"
"net/http"
"path"
"time"
"gitlab.com/trantor/trantor/lib/database"
@ -34,7 +35,7 @@ type Status struct {
func GetStatus(h handler) Status {
var s Status
s.BaseURL = "http://" + HOST_URL
s.BaseURL = "http://" + h.hostname
s.FullURL = s.BaseURL + h.r.RequestURI
s.Title = "Imperial Library of Trantor"
s.User = h.sess.User
@ -45,52 +46,65 @@ func GetStatus(h handler) Status {
return s
}
var tmpl_html = template.Must(template.ParseFiles(
TEMPLATE_PATH+"header.html",
TEMPLATE_PATH+"footer.html",
TEMPLATE_PATH+"404.html",
TEMPLATE_PATH+"index.html",
TEMPLATE_PATH+"about.html",
TEMPLATE_PATH+"news.html",
TEMPLATE_PATH+"edit_news.html",
TEMPLATE_PATH+"book.html",
TEMPLATE_PATH+"search.html",
TEMPLATE_PATH+"upload.html",
TEMPLATE_PATH+"login.html",
TEMPLATE_PATH+"new.html",
TEMPLATE_PATH+"read.html",
TEMPLATE_PATH+"edit.html",
TEMPLATE_PATH+"dashboard.html",
TEMPLATE_PATH+"settings.html",
TEMPLATE_PATH+"stats.html",
TEMPLATE_PATH+"help.html",
))
type Template struct {
tmpl_html *html_tmpl.Template
tmpl_rss *txt_tmpl.Template
tmpl_opds *txt_tmpl.Template
}
var tmpl_rss = txt_tmpl.Must(txt_tmpl.ParseFiles(
TEMPLATE_PATH+"search.rss",
TEMPLATE_PATH+"news.rss",
))
func InitTemplate(assetsPath string) *Template {
var t Template
templatePath := path.Join(assetsPath, "templates")
var tmpl_opds = txt_tmpl.Must(txt_tmpl.ParseFiles(
TEMPLATE_PATH+"index.opds",
TEMPLATE_PATH+"search.opds",
))
t.tmpl_html = html_tmpl.Must(html_tmpl.ParseFiles(
path.Join(templatePath, "header.html"),
path.Join(templatePath, "footer.html"),
path.Join(templatePath, "404.html"),
path.Join(templatePath, "index.html"),
path.Join(templatePath, "about.html"),
path.Join(templatePath, "news.html"),
path.Join(templatePath, "edit_news.html"),
path.Join(templatePath, "book.html"),
path.Join(templatePath, "search.html"),
path.Join(templatePath, "upload.html"),
path.Join(templatePath, "login.html"),
path.Join(templatePath, "new.html"),
path.Join(templatePath, "read.html"),
path.Join(templatePath, "edit.html"),
path.Join(templatePath, "dashboard.html"),
path.Join(templatePath, "settings.html"),
path.Join(templatePath, "stats.html"),
path.Join(templatePath, "help.html"),
))
func loadTemplate(h handler, tmpl string, data interface{}) {
t.tmpl_rss = txt_tmpl.Must(txt_tmpl.ParseFiles(
path.Join(templatePath, "search.rss"),
path.Join(templatePath, "news.rss"),
))
t.tmpl_opds = txt_tmpl.Must(txt_tmpl.ParseFiles(
path.Join(templatePath, "index.opds"),
path.Join(templatePath, "search.opds"),
))
return &t
}
func (t Template) load(h handler, tmpl string, data interface{}) {
var err error
fmt := h.r.FormValue("fmt")
switch fmt {
case "rss":
err = tmpl_rss.ExecuteTemplate(h.w, tmpl+".rss", data)
err = t.tmpl_rss.ExecuteTemplate(h.w, tmpl+".rss", data)
case "opds":
err = tmpl_opds.ExecuteTemplate(h.w, tmpl+".opds", data)
err = t.tmpl_opds.ExecuteTemplate(h.w, tmpl+".opds", data)
case "json":
err = loadJson(h.w, tmpl, data)
default:
err = tmpl_html.ExecuteTemplate(h.w, tmpl+".html", data)
err = t.tmpl_html.ExecuteTemplate(h.w, tmpl+".html", data)
}
if err != nil {
tmpl_html.ExecuteTemplate(h.w, "404.html", data)
t.tmpl_html.ExecuteTemplate(h.w, "404.html", data)
log.Warn("An error ocurred loading the template ", tmpl, ".", fmt, ": ", err)
}
}