go-bouquins/bouquins/bouquins.go

462 lines
12 KiB
Go
Raw Normal View History

2017-07-30 14:06:38 +00:00
package bouquins
2017-07-30 15:20:48 +00:00
import (
2017-07-30 18:14:20 +00:00
"database/sql"
2017-07-31 18:49:27 +00:00
"encoding/json"
2017-08-05 15:48:06 +00:00
"errors"
2017-08-04 18:06:10 +00:00
"fmt"
2017-07-30 15:20:48 +00:00
"html/template"
"log"
"net/http"
2017-08-04 17:47:15 +00:00
"net/url"
2017-08-01 18:00:20 +00:00
"strconv"
"strings"
2017-08-04 18:06:10 +00:00
2017-09-08 10:02:54 +00:00
"golang.org/x/oauth2"
2017-08-04 18:06:10 +00:00
"github.com/c2h5oh/datasize"
2017-09-08 14:39:22 +00:00
"github.com/gorilla/sessions"
2017-07-30 15:20:48 +00:00
)
const (
2017-09-08 18:41:30 +00:00
// Version defines application version
2017-09-05 14:54:25 +00:00
Version = "master"
2017-09-08 18:29:01 +00:00
tplBooks = "book.html"
tplAuthors = "author.html"
tplSeries = "series.html"
tplIndex = "index.html"
tplSearch = "search.html"
tplAbout = "about.html"
tplProvider = "provider.html"
2017-08-06 18:25:20 +00:00
pList = "list"
pOrder = "order"
pSort = "sort"
pPage = "page"
pPerPage = "perpage"
pTerm = "term"
// URLIndex url of index page
URLIndex = "/"
2017-09-08 10:02:54 +00:00
// URLLogin url of login page (OAuth 2)
URLLogin = "/login"
2017-09-08 16:13:22 +00:00
// URLLogout url of logout page
URLLogout = "/logout"
2017-09-08 10:32:26 +00:00
// URLCallback url of OAuth callback
URLCallback = "/callback"
2017-08-06 18:25:20 +00:00
// URLBooks url of books page
URLBooks = "/books/"
// URLAuthors url of authors page
URLAuthors = "/authors/"
// URLSeries url of series page
URLSeries = "/series/"
// URLSearch url of search page
URLSearch = "/search/"
// URLAbout url of about page
URLAbout = "/about/"
// URLJs url of js assets
2017-09-05 14:54:25 +00:00
URLJs = "/" + Version + "/js/"
2017-08-06 18:25:20 +00:00
// URLCss url of css assets
2017-09-05 14:54:25 +00:00
URLCss = "/" + Version + "/css/"
2017-08-06 18:25:20 +00:00
// URLFonts url of fonts assets
2017-09-05 14:54:25 +00:00
URLFonts = "/" + Version + "/fonts/"
2017-08-06 18:25:20 +00:00
// URLCalibre url of calibre resources (covers, ebooks files)
URLCalibre = "/calibre/"
2017-07-30 15:20:48 +00:00
)
2017-07-30 14:06:38 +00:00
2017-08-06 18:25:20 +00:00
// Bouquins contains application common resources: templates, database
2017-07-30 14:06:38 +00:00
type Bouquins struct {
2017-09-08 10:02:54 +00:00
Tpl *template.Template
DB *sql.DB
2017-09-08 18:29:01 +00:00
OAuthConf map[string]*oauth2.Config
2017-09-08 14:39:22 +00:00
Cookies *sessions.CookieStore
2017-07-30 14:06:38 +00:00
}
2017-08-06 18:25:20 +00:00
// Series is a book series.
2017-07-30 14:42:18 +00:00
type Series struct {
2017-08-06 18:25:20 +00:00
ID int64 `json:"id,omitempty"`
2017-07-31 18:49:27 +00:00
Name string `json:"name,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// Book contains basic data on book
2017-07-30 14:42:18 +00:00
type Book struct {
2017-08-06 18:25:20 +00:00
ID int64 `json:"id,omitempty"`
2017-07-31 18:49:27 +00:00
Title string `json:"title,omitempty"`
2017-08-01 18:00:20 +00:00
SeriesIndex float64 `json:"series_idx,omitempty"`
2017-07-31 18:49:27 +00:00
Series *Series `json:"series,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// Author contains basic data on author
2017-07-30 14:42:18 +00:00
type Author struct {
2017-08-06 18:25:20 +00:00
ID int64 `json:"id,omitempty"`
2017-07-31 18:49:27 +00:00
Name string `json:"name,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// AuthorAdv extends Author with number of books
2017-07-30 14:42:18 +00:00
type AuthorAdv struct {
Author
2017-07-31 18:49:27 +00:00
Count int `json:"count,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// BookData contains data for dowloadable book
2017-07-30 14:42:18 +00:00
type BookData struct {
2017-07-31 18:49:27 +00:00
Size int64 `json:"size,omitempty"`
Format string `json:"format,omitempty"`
Name string `json:"name,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// BookAdv extends Book with authors and tags
2017-07-30 14:42:18 +00:00
type BookAdv struct {
Book
2017-07-31 18:49:27 +00:00
Authors []*Author `json:"authors,omitempty"`
Tags []string `json:"tags,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// AuthorFull extends Author with books, series and co-authors
2017-07-30 14:42:18 +00:00
type AuthorFull struct {
Author
2017-08-04 16:57:15 +00:00
Books []*Book `json:"books,omitempty"`
Series []*Series `json:"series,omitempty"`
CoAuthors []*Author `json:"coauthors,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// BookFull extends BookAdv with all available data
2017-07-30 14:42:18 +00:00
type BookFull struct {
BookAdv
2017-08-01 18:00:20 +00:00
Data []*BookData `json:"data,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Pubdate int64 `json:"pubdate,omitempty"`
Isbn string `json:"isbn,omitempty"`
Lccn string `json:"lccn,omitempty"`
Path string `json:"path,omitempty"`
2017-08-06 18:25:20 +00:00
UUID string `json:"uuid,omitempty"`
HasCover bool `json:"has_cover,omitempty"`
2017-08-01 18:00:20 +00:00
Lang string `json:"lang,omitempty"`
Publisher string `json:"publisher,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// SeriesAdv extends Series with count of books and authors
2017-07-30 14:42:18 +00:00
type SeriesAdv struct {
Series
2017-07-31 18:49:27 +00:00
Count int64 `json:"count,omitempty"`
Authors []*Author `json:"authors,omitempty"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// SeriesFull extends SeriesAdv with related books
2017-07-30 14:42:18 +00:00
type SeriesFull struct {
SeriesAdv
2017-07-31 18:49:27 +00:00
Books []*Book `json:"books,omitempty"`
2017-07-30 15:20:48 +00:00
}
2017-08-06 18:25:20 +00:00
// Model is basic page model
type Model struct {
2017-09-08 16:13:22 +00:00
Title string
Page string
Version string
Username string
2017-07-30 16:09:27 +00:00
}
2017-09-09 07:16:46 +00:00
// NewModel constructor for Model
2017-09-08 16:13:22 +00:00
func (app *Bouquins) NewModel(title, page string, req *http.Request) *Model {
return &Model{
Title: title,
Page: page,
Version: Version,
Username: app.Username(req),
}
2017-07-30 16:09:27 +00:00
}
2017-08-06 18:25:20 +00:00
// IndexModel is the model for index page
2017-07-30 15:20:48 +00:00
type IndexModel struct {
2017-08-06 18:25:20 +00:00
Model
2017-08-05 14:44:05 +00:00
BooksCount int64 `json:"count"`
2017-07-30 14:42:18 +00:00
}
2017-08-06 18:25:20 +00:00
// NewIndexModel constructor IndexModel
2017-09-08 16:13:22 +00:00
func (app *Bouquins) NewIndexModel(title string, count int64, req *http.Request) *IndexModel {
return &IndexModel{*app.NewModel(title, "index", req), count}
2017-07-30 16:09:27 +00:00
}
2017-08-06 18:25:20 +00:00
// NewSearchModel constuctor for search page
2017-09-08 16:13:22 +00:00
func (app *Bouquins) NewSearchModel(req *http.Request) *Model {
return app.NewModel("Recherche", "search", req)
2017-08-06 10:50:43 +00:00
}
2017-08-06 18:25:20 +00:00
// ResultsModel is a generic model for list pages
2017-08-05 15:48:06 +00:00
type ResultsModel struct {
2017-08-06 10:50:43 +00:00
Type string `json:"type,omitempty"`
More bool `json:"more"`
CountResults int `json:"count,omitempty"`
2017-08-05 15:48:06 +00:00
}
2017-08-06 18:25:20 +00:00
// BooksResultsModel is the model for list of books
2017-08-05 14:44:05 +00:00
type BooksResultsModel struct {
2017-08-05 15:48:06 +00:00
ResultsModel
2017-08-05 14:44:05 +00:00
Results []*BookAdv `json:"results,omitempty"`
}
2017-08-06 18:25:20 +00:00
// NewBooksResultsModel constuctor for BooksResultsModel
2017-08-06 10:50:43 +00:00
func NewBooksResultsModel(books []*BookAdv, more bool, count int) *BooksResultsModel {
return &BooksResultsModel{ResultsModel{"books", more, count}, books}
2017-08-05 15:48:06 +00:00
}
2017-08-06 18:25:20 +00:00
// AuthorsResultsModel is the model for list of authors
2017-08-05 15:48:06 +00:00
type AuthorsResultsModel struct {
ResultsModel
Results []*AuthorAdv `json:"results,omitempty"`
}
2017-08-06 18:25:20 +00:00
// NewAuthorsResultsModel constuctor for AuthorsResultsModel
2017-08-06 10:50:43 +00:00
func NewAuthorsResultsModel(authors []*AuthorAdv, more bool, count int) *AuthorsResultsModel {
return &AuthorsResultsModel{ResultsModel{"authors", more, count}, authors}
2017-08-05 15:48:06 +00:00
}
2017-08-06 18:25:20 +00:00
// SeriesResultsModel is the model for list of series
2017-08-05 16:24:57 +00:00
type SeriesResultsModel struct {
2017-08-05 15:48:06 +00:00
ResultsModel
Results []*SeriesAdv `json:"results,omitempty"`
2017-08-05 14:44:05 +00:00
}
2017-08-06 18:25:20 +00:00
// NewSeriesResultsModel constuctor for SeriesResultsModel
2017-08-06 10:50:43 +00:00
func NewSeriesResultsModel(series []*SeriesAdv, more bool, count int) *SeriesResultsModel {
return &SeriesResultsModel{ResultsModel{"series", more, count}, series}
2017-08-05 15:48:06 +00:00
}
2017-08-05 14:44:05 +00:00
2017-08-06 18:25:20 +00:00
// BookModel is the model for single book page
2017-08-01 13:38:23 +00:00
type BookModel struct {
2017-08-06 18:25:20 +00:00
Model
2017-08-01 13:38:23 +00:00
*BookFull
}
2017-08-06 18:25:20 +00:00
// SeriesModel is the model for single series page
2017-08-03 17:51:56 +00:00
type SeriesModel struct {
2017-08-06 18:25:20 +00:00
Model
2017-08-03 17:51:56 +00:00
*SeriesFull
}
2017-08-06 18:25:20 +00:00
// AuthorModel is the model for single author page
2017-08-03 18:36:46 +00:00
type AuthorModel struct {
2017-08-06 18:25:20 +00:00
Model
2017-08-03 18:36:46 +00:00
*AuthorFull
}
2017-08-06 18:25:20 +00:00
// ReqParams contains request parameters for searches and lists
2017-08-06 10:50:43 +00:00
type ReqParams struct {
Limit int
Offset int
Sort string
Order string
Terms []string
AllWords bool
}
2017-08-06 18:25:20 +00:00
// TemplatesFunc adds functions to templates
2017-09-05 15:35:10 +00:00
func TemplatesFunc(prod bool) *template.Template {
2017-08-06 16:49:40 +00:00
return template.New("").Funcs(template.FuncMap{
2017-09-05 15:35:10 +00:00
"assetUrl": func(name string, ext string) string {
sep := "."
if prod {
sep = ".min."
}
return "/" + Version + "/" + ext + "/" + name + sep + ext
},
2017-08-04 17:47:15 +00:00
"humanSize": func(sz int64) string {
return datasize.ByteSize(sz).HumanReadable()
},
2017-08-04 18:06:10 +00:00
"bookCover": func(book *BookFull) string {
fmt.Println(book.Path)
return "/calibre/" + url.PathEscape(book.Path) + "/cover.jpg"
},
2017-08-04 17:47:15 +00:00
"bookLink": func(data *BookData, book *BookFull) string {
2017-08-04 18:06:10 +00:00
return "/calibre/" + url.PathEscape(book.Path) + "/" + url.PathEscape(data.Name) + "." + strings.ToLower(data.Format)
2017-08-04 17:47:15 +00:00
},
})
}
2017-07-30 16:09:27 +00:00
2017-09-08 16:13:22 +00:00
// RedirectHome redirects to home page
func RedirectHome(res http.ResponseWriter, req *http.Request) error {
http.Redirect(res, req, "/", http.StatusTemporaryRedirect)
return nil
}
2017-08-06 16:49:40 +00:00
// output page with template
func (app *Bouquins) render(res http.ResponseWriter, tpl string, model interface{}) error {
2017-08-06 18:25:20 +00:00
return app.Tpl.ExecuteTemplate(res, tpl, model)
2017-08-06 16:49:40 +00:00
}
// output as JSON
2017-08-06 18:25:20 +00:00
func writeJSON(res http.ResponseWriter, model interface{}) error {
2017-08-05 15:48:06 +00:00
res.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(res)
return enc.Encode(model)
}
2017-08-06 16:49:40 +00:00
// test if JSON requested
2017-08-06 18:25:20 +00:00
func isJSON(req *http.Request) bool {
2017-08-05 15:48:06 +00:00
return req.Header.Get("Accept") == "application/json"
}
2017-08-06 16:49:40 +00:00
// get integer parameter
2017-08-02 18:17:12 +00:00
func paramInt(name string, req *http.Request) int {
val := req.URL.Query().Get(name)
2017-08-06 10:50:43 +00:00
if val == "" {
return 0
}
2017-08-02 18:17:12 +00:00
valInt, err := strconv.Atoi(val)
if err != nil {
2017-08-05 09:16:19 +00:00
log.Println("Invalid value for", name, ":", val)
2017-08-02 18:17:12 +00:00
return 0
}
return valInt
}
2017-08-06 16:49:40 +00:00
// get order parameter
2017-08-02 18:17:12 +00:00
func paramOrder(req *http.Request) string {
2017-08-06 18:25:20 +00:00
val := req.URL.Query().Get(pOrder)
2017-08-02 18:17:12 +00:00
if val == "desc" || val == "asc" {
return val
}
return ""
}
2017-08-06 16:49:40 +00:00
// get common request parameters
2017-08-06 10:50:43 +00:00
func params(req *http.Request) *ReqParams {
2017-08-06 18:25:20 +00:00
page, perpage := paramInt(pPage, req), paramInt(pPerPage, req)
2017-08-05 17:40:58 +00:00
limit := perpage
if perpage == 0 {
2017-08-06 18:25:20 +00:00
limit = defaultLimit
2017-08-05 17:40:58 +00:00
}
offset := perpage * (page - 1)
if offset < 0 {
offset = 0
}
2017-08-06 18:25:20 +00:00
sort := req.URL.Query().Get(pSort)
2017-08-05 17:40:58 +00:00
order := paramOrder(req)
2017-08-06 18:25:20 +00:00
terms := req.URL.Query()[pTerm]
2017-08-06 10:50:43 +00:00
return &ReqParams{limit, offset, sort, order, terms, false}
2017-08-05 17:40:58 +00:00
}
2017-08-06 16:49:40 +00:00
// single element or list elements page
2017-08-06 18:25:20 +00:00
func listOrID(res http.ResponseWriter, req *http.Request, url string,
2017-08-06 16:49:40 +00:00
listFunc func(res http.ResponseWriter, req *http.Request) error,
idFunc func(idParam string, res http.ResponseWriter, req *http.Request) error) error {
if !strings.HasPrefix(req.URL.Path, url) {
return errors.New("Invalid URL") // FIXME 404
2017-07-30 18:14:20 +00:00
}
2017-08-06 16:49:40 +00:00
idParam := req.URL.Path[len(url):]
if len(idParam) == 0 {
return listFunc(res, req)
2017-07-31 18:49:27 +00:00
}
2017-08-06 16:49:40 +00:00
return idFunc(idParam, res, req)
2017-07-30 16:09:27 +00:00
}
2017-08-06 16:49:40 +00:00
// LIST ELEMENTS PAGES //
2017-08-06 18:25:20 +00:00
func (app *Bouquins) booksListPage(res http.ResponseWriter, req *http.Request) error {
if isJSON(req) {
2017-08-06 10:50:43 +00:00
books, count, more, err := app.BooksAdv(params(req))
2017-08-05 15:48:06 +00:00
if err != nil {
return err
}
2017-08-06 18:25:20 +00:00
return writeJSON(res, NewBooksResultsModel(books, more, count))
2017-08-05 15:48:06 +00:00
}
return errors.New("Invalid mime")
}
2017-08-06 18:25:20 +00:00
func (app *Bouquins) authorsListPage(res http.ResponseWriter, req *http.Request) error {
if isJSON(req) {
2017-08-06 10:50:43 +00:00
authors, count, more, err := app.AuthorsAdv(params(req))
2017-08-05 09:16:19 +00:00
if err != nil {
2017-08-05 15:48:06 +00:00
return err
2017-08-05 09:16:19 +00:00
}
2017-08-06 18:25:20 +00:00
return writeJSON(res, NewAuthorsResultsModel(authors, more, count))
2017-08-01 13:38:23 +00:00
}
2017-08-05 15:48:06 +00:00
return errors.New("Invalid mime")
2017-07-30 14:06:38 +00:00
}
2017-08-06 18:25:20 +00:00
func (app *Bouquins) seriesListPage(res http.ResponseWriter, req *http.Request) error {
if isJSON(req) {
2017-08-06 10:50:43 +00:00
series, count, more, err := app.SeriesAdv(params(req))
2017-08-05 16:24:57 +00:00
if err != nil {
return err
}
2017-08-06 18:25:20 +00:00
return writeJSON(res, NewSeriesResultsModel(series, more, count))
2017-08-03 17:51:56 +00:00
}
2017-08-05 16:24:57 +00:00
return errors.New("Invalid mime")
}
2017-08-06 16:49:40 +00:00
// SINGLE ELEMENT PAGES //
2017-08-06 18:25:20 +00:00
func (app *Bouquins) bookPage(idParam string, res http.ResponseWriter, req *http.Request) error {
2017-08-05 16:24:57 +00:00
id, err := strconv.Atoi(idParam)
2017-08-03 17:51:56 +00:00
if err != nil {
2017-08-05 16:24:57 +00:00
return err
2017-08-03 17:51:56 +00:00
}
2017-08-06 16:49:40 +00:00
book, err := app.BookFull(int64(id))
2017-08-03 18:36:46 +00:00
if err != nil {
2017-08-05 16:24:57 +00:00
return err
}
2017-09-08 16:13:22 +00:00
return app.render(res, tplBooks, &BookModel{*app.NewModel(book.Title, "book", req), book})
2017-08-05 16:24:57 +00:00
}
2017-08-06 18:25:20 +00:00
func (app *Bouquins) authorPage(idParam string, res http.ResponseWriter, req *http.Request) error {
2017-08-06 16:49:40 +00:00
id, err := strconv.Atoi(idParam)
if err != nil {
return err
2017-08-05 16:24:57 +00:00
}
2017-08-06 16:49:40 +00:00
author, err := app.AuthorFull(int64(id))
2017-08-05 16:24:57 +00:00
if err != nil {
2017-08-06 16:49:40 +00:00
return err
2017-08-03 18:36:46 +00:00
}
2017-09-08 16:13:22 +00:00
return app.render(res, tplAuthors, &AuthorModel{*app.NewModel(author.Name, "author", req), author})
2017-07-30 14:06:38 +00:00
}
2017-08-06 18:25:20 +00:00
func (app *Bouquins) seriePage(idParam string, res http.ResponseWriter, req *http.Request) error {
2017-08-06 16:49:40 +00:00
id, err := strconv.Atoi(idParam)
if err != nil {
return err
}
series, err := app.SeriesFull(int64(id))
2017-08-06 10:50:43 +00:00
if err != nil {
2017-08-06 16:49:40 +00:00
return err
2017-08-06 10:50:43 +00:00
}
2017-09-08 16:13:22 +00:00
return app.render(res, tplSeries, &SeriesModel{*app.NewModel(series.Name, "series", req), series})
2017-08-06 10:50:43 +00:00
}
2017-08-06 16:49:40 +00:00
// ROUTES //
2017-08-06 18:25:20 +00:00
// BooksPage displays a single books or a returns a list of books
2017-08-06 16:49:40 +00:00
func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) error {
2017-08-06 18:25:20 +00:00
return listOrID(res, req, URLBooks, app.booksListPage, app.bookPage)
2017-08-06 16:49:40 +00:00
}
2017-08-06 18:25:20 +00:00
// AuthorsPage displays a single author or returns a list of authors
2017-08-06 16:49:40 +00:00
func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) error {
2017-08-06 18:25:20 +00:00
return listOrID(res, req, URLAuthors, app.authorsListPage, app.authorPage)
2017-08-06 16:49:40 +00:00
}
2017-08-06 18:25:20 +00:00
// SeriesPage displays a single series or returns a list of series
2017-08-06 16:49:40 +00:00
func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) error {
2017-08-06 18:25:20 +00:00
return listOrID(res, req, URLSeries, app.seriesListPage, app.seriePage)
2017-08-06 16:49:40 +00:00
}
2017-08-06 18:25:20 +00:00
// SearchPage displays search form and results
2017-08-06 16:49:40 +00:00
func (app *Bouquins) SearchPage(res http.ResponseWriter, req *http.Request) error {
2017-09-08 16:13:22 +00:00
return app.render(res, tplSearch, app.NewSearchModel(req))
2017-08-06 16:49:40 +00:00
}
2017-08-06 18:25:20 +00:00
// AboutPage displays about page
2017-08-06 16:49:40 +00:00
func (app *Bouquins) AboutPage(res http.ResponseWriter, req *http.Request) error {
2017-09-08 16:13:22 +00:00
return app.render(res, tplAbout, app.NewModel("A propos", "about", req))
2017-08-06 16:49:40 +00:00
}
2017-08-06 18:25:20 +00:00
// IndexPage displays index page: list of books/authors/series
2017-08-06 16:49:40 +00:00
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) error {
count, err := app.BookCount()
2017-08-06 14:05:58 +00:00
if err != nil {
2017-08-06 16:49:40 +00:00
return err
}
2017-09-08 16:13:22 +00:00
model := app.NewIndexModel("", count, req)
2017-08-06 18:25:20 +00:00
if isJSON(req) {
return writeJSON(res, model)
2017-08-06 14:05:58 +00:00
}
2017-08-06 18:25:20 +00:00
return app.render(res, tplIndex, model)
2017-08-06 14:05:58 +00:00
}