Add json fmt support
This commit is contained in:
parent
c132c0fdf6
commit
1a897cd1bd
1 changed files with 129 additions and 7 deletions
136
template.go
136
template.go
|
@ -1,9 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import log "github.com/cihub/seelog"
|
import (
|
||||||
import txt_tmpl "text/template"
|
txt_tmpl "text/template"
|
||||||
|
|
||||||
import "html/template"
|
"git.gitorious.org/trantor/trantor.git/database"
|
||||||
|
log "github.com/cihub/seelog"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
|
@ -32,7 +39,8 @@ func GetStatus(h handler) Status {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmpl_html = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
|
var tmpl_html = template.Must(template.ParseFiles(
|
||||||
|
TEMPLATE_PATH+"header.html",
|
||||||
TEMPLATE_PATH+"footer.html",
|
TEMPLATE_PATH+"footer.html",
|
||||||
TEMPLATE_PATH+"404.html",
|
TEMPLATE_PATH+"404.html",
|
||||||
TEMPLATE_PATH+"index.html",
|
TEMPLATE_PATH+"index.html",
|
||||||
|
@ -52,16 +60,20 @@ var tmpl_html = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
|
||||||
TEMPLATE_PATH+"help.html",
|
TEMPLATE_PATH+"help.html",
|
||||||
))
|
))
|
||||||
|
|
||||||
var tmpl_rss = txt_tmpl.Must(txt_tmpl.ParseFiles(TEMPLATE_PATH+"search.rss",
|
var tmpl_rss = txt_tmpl.Must(txt_tmpl.ParseFiles(
|
||||||
|
TEMPLATE_PATH+"search.rss",
|
||||||
TEMPLATE_PATH+"news.rss",
|
TEMPLATE_PATH+"news.rss",
|
||||||
))
|
))
|
||||||
|
|
||||||
func loadTemplate(h handler, tmpl string, data interface{}) {
|
func loadTemplate(h handler, tmpl string, data interface{}) {
|
||||||
var err error
|
var err error
|
||||||
fmt := h.r.FormValue("fmt")
|
fmt := h.r.FormValue("fmt")
|
||||||
if fmt == "rss" {
|
switch fmt {
|
||||||
|
case "rss":
|
||||||
err = tmpl_rss.ExecuteTemplate(h.w, tmpl+".rss", data)
|
err = tmpl_rss.ExecuteTemplate(h.w, tmpl+".rss", data)
|
||||||
} else {
|
case "json":
|
||||||
|
err = loadJson(h.w, tmpl, data)
|
||||||
|
default:
|
||||||
err = tmpl_html.ExecuteTemplate(h.w, tmpl+".html", data)
|
err = tmpl_html.ExecuteTemplate(h.w, tmpl+".html", data)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,3 +81,113 @@ func loadTemplate(h handler, tmpl string, data interface{}) {
|
||||||
log.Warn("An error ocurred loading the template ", tmpl, ".", fmt, ": ", err)
|
log.Warn("An error ocurred loading the template ", tmpl, ".", fmt, ": ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadJson(w http.ResponseWriter, tmpl string, data interface{}) error {
|
||||||
|
var res []byte
|
||||||
|
var err error
|
||||||
|
switch tmpl {
|
||||||
|
case "index":
|
||||||
|
res, err = indexJson(data)
|
||||||
|
case "book":
|
||||||
|
res, err = bookJson(data)
|
||||||
|
case "news":
|
||||||
|
res, err = newsJson(data)
|
||||||
|
case "search":
|
||||||
|
res, err = searchJson(data)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write(res)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexJson(data interface{}) ([]byte, error) {
|
||||||
|
index, ok := data.(indexData)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("Data is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
books := make([]map[string]interface{}, len(index.Books))
|
||||||
|
for i, book := range index.Books {
|
||||||
|
books[i] = bookJsonRaw(book)
|
||||||
|
}
|
||||||
|
news := newsJsonRaw(index.News)
|
||||||
|
|
||||||
|
return json.Marshal(map[string]interface{}{
|
||||||
|
"title": "Imperial Library of Trantor",
|
||||||
|
"url": index.S.BaseURL,
|
||||||
|
"count": index.Count,
|
||||||
|
"news": news,
|
||||||
|
"tags": index.Tags,
|
||||||
|
"last added": books,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func bookJson(data interface{}) ([]byte, error) {
|
||||||
|
book, ok := data.(bookData)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("Data is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(bookJsonRaw(book.Book))
|
||||||
|
}
|
||||||
|
|
||||||
|
func newsJson(data interface{}) ([]byte, error) {
|
||||||
|
news, ok := data.(newsData)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("Data is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(newsJsonRaw(news.News))
|
||||||
|
}
|
||||||
|
|
||||||
|
func newsJsonRaw(news []newsEntry) []map[string]string {
|
||||||
|
list := make([]map[string]string, len(news))
|
||||||
|
for i, n := range news {
|
||||||
|
list[i] = map[string]string{
|
||||||
|
"date": n.Date,
|
||||||
|
"text": n.Text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
func searchJson(data interface{}) ([]byte, error) {
|
||||||
|
search, ok := data.(searchData)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("Data is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
books := make([]map[string]interface{}, len(search.Books))
|
||||||
|
for i, book := range search.Books {
|
||||||
|
books[i] = bookJsonRaw(book)
|
||||||
|
}
|
||||||
|
return json.Marshal(books)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bookJsonRaw(book database.Book) map[string]interface{} {
|
||||||
|
cover := ""
|
||||||
|
coverSmall := ""
|
||||||
|
if book.Cover {
|
||||||
|
cover = "/cover/" + book.Id + "/big/" + book.Title + ".jpg"
|
||||||
|
coverSmall = "/cover/" + book.Id + "/small/" + book.Title + ".jpg"
|
||||||
|
}
|
||||||
|
return map[string]interface{}{
|
||||||
|
"id": book.Id,
|
||||||
|
"title": book.Title,
|
||||||
|
"author": book.Author,
|
||||||
|
"contributor": book.Contributor,
|
||||||
|
"publisher": book.Publisher,
|
||||||
|
"description": book.Description,
|
||||||
|
"subject": book.Subject,
|
||||||
|
"date": book.Date,
|
||||||
|
"lang": book.Lang,
|
||||||
|
"isbn": book.Isbn,
|
||||||
|
"size": book.FileSize,
|
||||||
|
"cover": cover,
|
||||||
|
"cover_small": coverSmall,
|
||||||
|
"download": "/download/" + book.Id + "/" + book.Title + ".epub",
|
||||||
|
"read": "/read/" + book.Id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in a new issue