kaliber

package module
v0.27.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2020 License: GPL-3.0 Imports: 30 Imported by: 0

README

Kaliber

Golang GoDoc Go Report Issues Size Tag License View examples


Note

Please note that this is a work in progress. Changes – even API breaking changes – might happen at any time.

Purpose

I love books. Always have. Since I was a kid. Over the years several thousand books gathered in my flat; so many that I sort of ran out of space. Reluctantly I started to investigate and then use eBooks, first on my main desktop computer, later with a dedicated eBook reader.

Soon – and again – there were so many that I started looking for some convenient way to handle, store, and retrieve them for reading. That's when I became acquainted with Calibre, a great software for working with eBooks. Of course, there were some problems but since that software is actively maintained and extended all the time those problems either went away on their own with the next update or I found some way around them. And sure, it took some work to get all the eBooks into that library system, and since there are always coming new titles the work kind of never ends.

Another question soon became urgent: How to access my books when I'm not at home? As it turned out, Calibre comes bundled with its own web-server. After figuring out how to start it automatically when the machine gets restarted (which happens once in a while when some kernel software upgrade requires it) the server did its job. Quite another question, however, is how it does its job. And there the problem lies. The web-server coming with Calibre serves pages that are heavily dependent on JavaScript; so much so that the pages simply don't appear or work at all if you have JavaScript disabled in your browser e.g. for privacy or security reasons. For a while I grudgingly activated JavaScript whenever I wanted to access my books remotely. I asked the author of Calibre whether he'd be willing to provide a barrier-free alternative (i.e. without JavaScript) but unfortunately he declined: "Not going to happen."

Well, due to other projects I didn't find the time then to do something about it, and it took some more years before I started seriously to look for alternatives. Therefore now there's Kaliber, a barrier-free web-server for your Calibre book collection. It doesn't depend on – or even need – JavaScript, but just requires a web-browser capable to read plain HTML on the remote user's side. So even privacy or security conscious people or those who depend on assisting technologies (like e.g. screen-readers) can now access their library.

As an aside:

I never really understood the desire to multiply the work needed to be done for a web page.

A server is a server which means it should serve. When in a restaurant ordering a meal you surely expect it to be brought to you fully prepared and ready to be consumed. You'd probably were seriously annoyed if the waiter/server just brought you the ingredients and left it to you to prepare the meal for yourself.

For some strange reason, however, that's exactly what happens on a growing number of web-presentations: Instead of delivering a ready-to-read web-page they send every user just program code and let the user's machine do the preparing of the page. In other words: Instead of one server doing the work and delivering it to thousands of remote users nowadays the server forces thousands (or even millions) of remote users to spend time and electricity to just see a single web-page.

Since I couldn't find a documentation of the database structure and/or API used by Calibre to store its meta-data I had to reverse engineer ways to access the stored book data. The same is true – to a certain lesser degree – for the web-pages served by Kaliber: it's kind of a mix of Calibre's normal (i.e. JavaScript based) and mobile pages. The overall layout of the web-pages served by Kaliber is intentionally kept simple (KISS).

Features

  • Simplicity of use;
  • Barrier-free (no JavaScript required);
  • Books list layout either Cover Grid or Data List;
  • Easy navigation (First, Prev, Next, Last button/links);
  • Fulltext search as well as datafield-based searches;
  • Ordered in either ascending or descending direction;
  • Selectable number of books per page;
  • Sortable by acquisition, author, language, published, publisher, rating, series, size, tags, or title;
  • Anonymised access logging (privacy by default);
  • Optional user/password based access control.

Installation

You can use Go to install this package for you:

go get -u github.com/mwat56/kaliber

Usage

After downloading this package you go to its directory and compile, e.g.

cd $GOPATH/src/github.com/mwat56/kaliber
go build app/kaliber.go

which should produce an executable binary.

Commandline options
$ ./kaliber -h

Usage: ./kaliber [OPTIONS]

-accessLog string
	<filename> Name of the access logfile to write to
	(default "/home/matthias/kaliber/access.log")
-authAll
	<boolean> whether to require authentication for all pages
-booksPerPage int
	<number> the default number of books shown per page  (default 24)
-certKey string
	<fileName> the name of the TLS certificate key
-certPem string
	<fileName> the name of the TLS certificate PEM
-dataDir string
	<dirName> the directory with CSS, FONTS, IMG, SESSIONS, and VIEWS sub-directories
	(default "/home/matthias/kaliber")
-delWhitespace
	(optional) Delete superfluous whitespace in generated pages (default true)
-errorlog string
	<filename> Name of the error logfile to write to
	(default "/home/matthias/kaliber/error.log")
-gzip
	<boolean> use gzip compression for server responses (default true)
-ini string
	<fileName> the path/filename of the INI file to use
	(default "/home/matthias/.kaliber.ini")
-lang string
	the default language to use  (default "en")
-libraryName string
	Name of this Library (shown on every page)
		(default "MeiBucks")
-libraryPath string
	<pathname> Path name of/to the Calibre library
	(default "/var/opt/Calibre")
-listen string
	the host's IP to listen at  (default "0")
-logStack
	<boolean> Log a stack trace for recovered runtime errors  (default true)
-port int
	<portNumber> The IP port to listen to  (default 8383)
-realm string
	<hostName> Name of host/domain to secure by BasicAuth
	(default "eBooks Host")
-sessionTTL int
	<seconds> Number of seconds an unused session keeps valid (default 1200)
-sidName string
	<name> The name of the session ID to use
	(default "sid")
-sqlTrace string
	<filename> Name of the SQL logfile to write to
-theme string
	<name> The display theme to use ('light' or 'dark')
	(default "dark")
-ua string
	<userName> User add: add a username to the password file
-uc string
	<userName> User check: check a username in the password file
-ud string
	<userName> User delete: remove a username from the password file
-uf string
	<fileName> Passwords file storing user/passwords for BasicAuth
	(default "/home/matthias/kaliber/pwaccess.db")
-ul
	<boolean> User list: show all users in the password file
-uu string
	<userName> User update: update a username in the password file

Most options can be set in an INI file to keep the command-line short ;-)

$ _

As you can see there are quite a few options available, but almost all of them are optional since they come with reasonable default values most of which can be set in the accompanying INI-file (in fact, the "default" values shown above are coming from the INI-file used).

Please note that the default values shown above vary depending on the system where kaliber is called and especially on the contents of the INI file (it's read before the help text is produced).

INI file

You don't have to give all those commandline options listed above every time you want to start Kaliber. There's an INI file which can take all the options (apart from the user handling options) all in one place:

$ cat kaliber.ini
# Default configuration file for the Kaliber server

[Default]

# Name of the optional access logfile to write to.
#
# NOTE: a relative path/name will be appended to `dataDir` (below).
# (Normally this is either empty or the name of the logfile to use.)
accessLog = /dev/stdout

# Authenticate user for all pages and documents.
#
# If `false` only the download links need user authentication
# (see `passFile` below).
authAll = false

# Number of documents to show per page.
booksPerPage = 24

# Path-/filename of the TLS certificate's private key to enable
# TLS/HTTPS (if empty standard HTTP is used).
#
# NOTE: a relative path/name will be appended to `dataDir` (below).
# (Normally this is either empty or the name of the key-file to use.)
certKey = ./certs/server.key

# Path-/filename of the TLS (server) certificate to enable TLS/HTTPS
# (if empty standard HTTP is used).
#
# NOTE: A relative path/name will be appended to `dataDir` (below).
# (Normally this is either empty or the name of the cert-file to use.)
certPem = ./certs/server.pem

# The directory root for the "css", "fonts", "img", "sessions",
# and "views" sub-directories.
#
# NOTE: This should be an _absolute_ path name!
dataDir = ./

# Delete superfluous whitespace in generated pages.
delWhitespace = yes

# Name of the optional error logfile to write to.
#
# NOTE: a relative path/name will be appended to `dataDir` (above).
# (Normally this is either empty or the name of the logfile to use.)
errorLog = /dev/stderr

# Use GZip compression for server responses.
gzip = true

# The default UI language to use ("de" or "en").
lang = de

# Name of this library (shown on every page).
libraryName = "MeiBucks"

# Path of Calibre library.
#
# NOTE: this _must_ be the absolute pathname to your Calibre library.
libraryPath = "/var/opt/Calibre"

# The host's IP number to listen at.
#
# The special value "0" means to listen on all available interfaces.
listen = 127.0.0.1

# Whether or not log a stack trace for recovered runtime errors.
#
# NOTE: This is merely a debugging aid and should normally be `false`.
logStack = true

# The host's IP port to listen to.
port = 8383

# Password file for HTTP Basic Authentication.
#
# NOTE: a relative path/name will be appended to `dataDir` (above).
passFile = ./pwaccess.db

# Name of host/domain to secure by BasicAuth.
realm = "eBooks Host"

# Number of seconds an unused session stays valid.
sessionTTL = 1200

# Name of the session ID field.
sidName = sid

# Optional (debugging) SQL trace file.
#
# NOTE: a relative path/name will be appended to `dataDir` (above).
# (Normally this will be empty and used only for debugging purposes.)
sqlTrace = ./sqlTrace.sql

# Default web/display theme to use ("dark" or "light").
theme = dark

# _EoF_
$ _

An INI-file as shown above is looked for at five different places:

  1. in your (i.e. the current user's) current directory (./kaliber.ini),
  2. in the computer's main config directory (/etc/kaliber.ini"),
  3. in the current user's home directory (e.g. $HOME/.kaliber.ini),
  4. in the current user's configuration directory (e.g. $HOME/.config/kaliber.ini),
  5. in the -ini <filename> commandline option (if given).

All these files (if they exist) are read in the given order at startup before finally parsing the commandline options shown earlier. So each step overwrites the previous one, the commandline options having the highest priority.

Authentication

Why, you may ask, would you need an username/password file anyway? Well, there may be several reasons one of which could be Copyright problems.

If not all your books are in the public domain and Copyright-free in most countries you may not make them publicly available. In that case you're most likely the only actual user allowed to access the books in your library. Depending on your country's legislation you may or may not include your family members. If in doubt please consult a Copyright expert.

The authAll commandline option (and INI setting) allows you to specify whether access to all pages requires user authentication; if that flag is false then only the download links require authentication, if true any access requires a given username/password pair.

Whenever there's no password file given (either in the INI file passfile or the command-line -uf) all functionality requiring authentication will be disabled which in turn means that everybody can freely access your library. This is probably not what you want.

Note that the password file generated and used by this application resembles the htpasswd used by the Apache web-server, but both files are not interchangeable because the actual encryption algorithms used by both are different.

User/password file & handling

Only usable from the commandline are the -uXX options, most of which need an username and the name of the password file to use.

Note that whenever you're prompted to input a password this will not be echoed to the console.

$ ./kaliber -ua testuser1 -uf pwaccess.db

 password:
repeat pw:
    added 'testuser1' to list
$ _

Since we have the passfile setting already in our INI file (see above) we can omit the -uf option for the next options.

With -uc you can check a user's password:

$ ./kaliber -uc testuser1

 password:
    'testuser1' password check successful
$ _

This -uc you'll probably never actually use – it was just easy to implement.

If you want to remove a user then -ud will do the trick:

$ ./kaliber -ud testuser1
    removed 'testuser1' from list
$ _

When you want to know which users are stored in your password file then -ul is your friend:

$ ./kaliber -ul
matthias

$ _

Since we deleted the testuser1 before only one entry remains.

That only leaves -uu to update (change) a user's password.

$ ./kaliber -ua testuser2

 password:
repeat pw:
    added 'testuser2' to list

$ ./kaliber -uu testuser2

 password:
repeat pw:
    updated user 'testuser2' in list

$ ./kaliber -ul
matthias
testuser2

$ _

First we added (-ua) a new user, then we updated the password (-uu), and finally we asked for the list of users (-ul).

Directory structure

Under the directory given with the datadir entry in the INI file (or the -datadir commandline option) there are several sub-directories expected:

  • css: containing the CSS files used,
  • fonts: containing the fonts used,
  • img: containing the images used,
  • sessions: containing the remote users' session data,
  • views: the Go templates used to generate the pages.

All of this directories and files are part of the Kaliber package. You can use them as is or customise them as you see fit to suit your needs. However, please note: I will not support any customisations, you're on your own with that – and you should know what you're doing.

Caveats

There are some Calibre features which are not available (yet) with Kaliber and not currently supported:

  • custom columns defined by the respective Calibre user;
  • different/multiple libraries for the user to switch between;
  • OPDS formatted access;
  • book uploads are not planned to be included;
  • monitoring your read progress is unlikely to be implemented here (I feel that that's the book reader's responsibility, not the server's).

Once I figure out how they are realised by Calibre I expect some may find their way into Kaliber as well (provided I find actually time to do it).

Logging

Like almost every other web-server Kaliber writes all access data to a logfile (logfile = in the INI file and -log at the commandline).

As privacy becomes a serious concern for a growing number of people (including law makers) – the IP address is definitely to be considered as personal data – the logging facility anonymises the requesting users by setting the host-part of the respective remote address to zero (0). This option takes care of e.g. European servers who may not without explicit consent of the users store personal data; this includes IP addresses in logfiles and elsewhere (eg. statistical data gathered from logfiles).

Since the generated logfile resembles that of the popular Apache server you can use all tools written for Apache logfiles to analyse the access data.

Libraries

The following external libraries were used building Kaliber:

Licence

    Copyright © 2019, 2020 M.Watermann, 10247 Berlin, Germany
                    All rights reserved
                EMail : <support@mwat.de>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should have received a copy of the GNU General Public License along with this program. If not, see the GNU General Public License for details.


Documentation

Overview

Package kaliber implements a server for the `Calibre` library software.

Copyright © 2019, 2020 M.Watermann, 10247 Berlin, Germany
                All rights reserved
            EMail : <support@mwat.de>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should have received a copy of the GNU General Public License along with this program. If not, see the [GNU General Public License](http://www.gnu.org/licenses/gpl.html) for details.

----

NOTE: While formally this is a library (`package`) in reality it's sort of a toolbox for the `main` package (the file of which is in the `app` sub-directory). In other words: this library is not supposed to be imported into other projects.

The publicly documented functions and methods are an arbitrary selection. In fact, most of the functionality of this `library` is implemented in private functions. Most of the `public` functions/methods are only public because they may be called from outside the source code file they were written in.

For a real understanding you're encouraged to read the source
code and the comments therein.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitConfig added in v0.12.0

func InitConfig()

InitConfig sets up and reads all configuration data from INI files and commandline arguments.

func ListUsers added in v0.13.4

func ListUsers(aFilename string)

ListUsers reads `aFilename` and lists all users stored in there.

NOTE: This function does not return but terminates the program with error code `0` (zero) if successful, or `1` (one) otherwise.

`aFilename` name of the password file to use.

func SetThumbWidth added in v0.2.0

func SetThumbWidth(aWidth uint) uint

SetThumbWidth set the new width for generated thumbnails.

If `aWidth` is smaller than `64` it's increased to `64`.

`aWidth` The new thumbnail width to use.

func ShowHelp

func ShowHelp()

ShowHelp lists the commandline options to `Stderr`.

func ThumbWidth added in v0.2.0

func ThumbWidth() uint

ThumbWidth returns the configured width of generated thumbnails.

func Thumbnail added in v0.2.0

func Thumbnail(aDoc *db.TDocument) (string, error)

Thumbnail generates a thumbnail of the document's cover.

`aDoc` The document to check the thumbnail for.

func ThumbnailUpdate added in v0.2.0

func ThumbnailUpdate()

ThumbnailUpdate creates thumbnails for all existing documents.

func URLparts

func URLparts(aURL string) (rDir, rPath string)

URLparts returns two parts: `rDir` holds the base-directory of `aURL`, `rPath` holds the remaining part of `aURL`.

Depending on the actual value of `aURL` both return values may be empty or both may be filled; none of both will hold a leading slash.

func UserAdd added in v0.27.0

func UserAdd(aUser, aFilename string)

UserAdd reads a password for `aUser` from the commandline and adds it to `aFilename`.

NOTE: This function does not return but terminates the program with error code `0` (zero) if successful, or `1` (one) otherwise.

`aUser` the username to add to the password file.
`aFilename` name of the password file to use.

func UserCheck added in v0.27.0

func UserCheck(aUser, aFilename string)

UserCheck reads a password for `aUser` from the commandline and compares it with the one stored in `aFilename`.

NOTE: This function does not return but terminates the program with error code `0` (zero) if successful, or `1` (one) otherwise.

`aUser` the username to check in the password file.
`aFilename` name of the password file to use.

func UserDelete added in v0.27.0

func UserDelete(aUser, aFilename string)

UserDelete removes the entry for `aUser` from the password list `aFilename`.

NOTE: This function does not return but terminates the program with error code `0` (zero) if successful, or `1` (one) otherwise.

`aUser` the username to remove from the password file.
`aFilename` name of the password file to use.

func UserUpdate added in v0.27.0

func UserUpdate(aUser, aFilename string)

UserUpdate reads a password for `aUser` from the commandline and updates the entry in the password list `aFilename`.

NOTE: This function does not return but terminates the program with error code `0` (zero) if successful, or `1` (one) otherwise.

`aUser` the username to remove from the password file. `aFilename` name of the password file to use.

Types

type TAppArgs added in v0.27.0

type TAppArgs struct {
	AccessLog    string // (optional) name of page access logfile
	Addr         string // listen address ("1.2.3.4:5678")
	AuthAll      bool   // authenticate user for all pages and documents
	BooksPerPage int    // number of documents shown per web-page
	CertKey      string // TLS certificate key
	CertPem      string // private TLS certificate
	DataDir      string // base directory of application's data

	ErrorLog string // (optional) name of page error logfile
	GZip     bool   // send compressed data to remote browser
	// Intl       string // path/filename of the localisation file
	Lang    string // default GUI language
	LibName string // the library's name

	LogStack bool   // log stack trace in case of errors
	PassFile string // (optional) name of page access logfile

	Realm      string // host/domain to secure by BasicAuth
	SessionDir string // directory for session data

	Theme      string // `dark` or `light` display theme
	UserAdd    string // username to add to password list
	UserCheck  string // username to check in password list
	UserDelete string // username to delete from password list
	UserList   bool   // print out a list of current users
	UserUpdate string // username to update in password list
	// contains filtered or unexported fields
}

TAppArgs Collection of commandline arguments and INI values.

var (
	// AppArgs Commandline arguments and INI values.
	//
	// This structure should be considered R/O after it was
	// set up by a call to `InitConfig()`.
	AppArgs TAppArgs
)

func (TAppArgs) String added in v0.27.0

func (aa TAppArgs) String() string

String implements the `Stringer` interface returning a (pretty printed) string representation of the current `TAppArgs` instance.

NOTE: This method is meant mostly for debugging purposes.

type TPageHandler

type TPageHandler struct {
	// contains filtered or unexported fields
}

TPageHandler provides the handling of HTTP request/response.

func NewPageHandler

func NewPageHandler() (*TPageHandler, error)

NewPageHandler returns a new `TPageHandler` instance.

func (*TPageHandler) GetErrorPage

func (ph *TPageHandler) GetErrorPage(aData []byte, aStatus int) []byte

GetErrorPage returns an error page for `aStatus`, implementing the `TErrorPager` interface.

func (*TPageHandler) NeedAuthentication

func (ph *TPageHandler) NeedAuthentication(aRequest *http.Request) bool

NeedAuthentication returns `true` if authentication is needed, or `false` otherwise.

`aRequest` is the web request to check.

func (*TPageHandler) ServeHTTP

func (ph *TPageHandler) ServeHTTP(aWriter http.ResponseWriter, aRequest *http.Request)

ServeHTTP handles the incoming HTTP requests.

type TView

type TView struct {
	// contains filtered or unexported fields
}

TView combines a template and its logical name.

func NewView

func NewView(aBaseDir, aName string) (*TView, error)

NewView returns a new `TView` with `aName`.

`aBaseDir` is the path to the directory storing the template files.

`aName` is the name of the template file providing the page's main

body without the filename extension (i.e. w/o ".gohtml"). `aName` serves as both the main template's name as well as the view's name.

func (*TView) Render

func (v *TView) Render(aWriter http.ResponseWriter, aData *TemplateData) error

Render executes the template using the TView's properties.

`aWriter` is a http.ResponseWriter, or e.g. `os.Stdout` in console apps.

`aData` is a list of data to be injected into the template.

If an error occurs executing the template or writing its output, execution stops, and the method returns without writing anything to the output `aWriter`.

func (*TView) RenderedPage

func (v *TView) RenderedPage(aData *TemplateData) ([]byte, error)

RenderedPage returns the rendered template/page and a possible Error executing the template.

`aData` is a list of data to be injected into the template.

type TViewList

type TViewList tViewList

TViewList is a list of `TView` instances (to be used as a template pool).

func NewViewList

func NewViewList() *TViewList

NewViewList returns a new (empty) `TViewList` instance.

func (*TViewList) Add

func (vl *TViewList) Add(aView *TView) *TViewList

Add appends `aView` to the list.

`aView` is the view to add to this list.

The view's name (as specified in the `NewView()` function call) is used as the view's key in this list.

func (*TViewList) Get

func (vl *TViewList) Get(aName string) (*TView, bool)

Get returns the view with `aName`.

`aName` is the name (key) of the `TView` object to retrieve.

If `aName` doesn't exist, the return value is `nil`. The second value (ok) is a `bool` that is `true` if `aName` exists in the list, and `false` if not.

func (*TViewList) Render

func (vl *TViewList) Render(aName string, aWriter http.ResponseWriter, aData *TemplateData) error

Render executes the template with the key `aName`.

`aName` is the name of the template/view to use.

`aWriter` is a `http.ResponseWriter` to handle the executed template.

`aData` is a list of data to be injected into the template.

If an error occurs executing the template or writing its output, execution stops, and the method returns without writing anything to the output `aWriter`.

func (*TViewList) RenderedPage

func (vl *TViewList) RenderedPage(aName string, aData *TemplateData) ([]byte, error)

RenderedPage returns the rendered template/page with the key `aName`.

`aName` is the name of the template/view to use.

`aData` is a list of data to be injected into the template.

type TemplateData

type TemplateData map[string]interface{}

TemplateData is a list of values to be injected into a template.

func NewTemplateData

func NewTemplateData() *TemplateData

NewTemplateData returns a new (empty) `TDataList` instance.

func (*TemplateData) Set

func (dl *TemplateData) Set(aKey string, aValue interface{}) *TemplateData

Set inserts `aValue` identified by `aKey` to the list.

If there's already a list entry with `aKey` its current value gets replaced by `aValue`.

`aKey` is the values's identifier (as used as placeholder in the template).

`aValue` contains the data entry's value.

Directories

Path Synopsis
db module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL