2017-07-30 14:06:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-30 18:14:20 +00:00
|
|
|
"database/sql"
|
2017-08-01 13:37:58 +00:00
|
|
|
"encoding/json"
|
2017-07-30 15:20:48 +00:00
|
|
|
"log"
|
2017-07-30 14:06:38 +00:00
|
|
|
"net/http"
|
2017-08-01 13:37:58 +00:00
|
|
|
"os"
|
2017-07-30 14:06:38 +00:00
|
|
|
|
2017-07-31 17:37:52 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
2017-08-01 18:00:20 +00:00
|
|
|
. "meutel.net/meutel/go-bouquins/bouquins"
|
2017-07-30 14:06:38 +00:00
|
|
|
)
|
|
|
|
|
2017-08-01 13:37:58 +00:00
|
|
|
type BouquinsConf struct {
|
|
|
|
BindAddress string `json:"bind-address"`
|
|
|
|
DbPath string `json:"db-path"`
|
2017-08-04 18:06:10 +00:00
|
|
|
CalibrePath string `json:"calibre-path"`
|
2017-08-01 13:37:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 18:14:20 +00:00
|
|
|
var db *sql.DB
|
|
|
|
|
2017-08-01 13:37:58 +00:00
|
|
|
// load config
|
|
|
|
func ReadConfig() (*BouquinsConf, error) {
|
|
|
|
conf := new(BouquinsConf)
|
|
|
|
confPath := "bouquins.json"
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
confPath = os.Args[1]
|
|
|
|
}
|
|
|
|
confFile, err := os.Open(confPath)
|
|
|
|
if err == nil {
|
|
|
|
defer confFile.Close()
|
|
|
|
err = json.NewDecoder(confFile).Decode(conf)
|
|
|
|
} else {
|
|
|
|
log.Println("no conf file, using defaults")
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
// default values
|
2017-08-04 18:06:10 +00:00
|
|
|
if conf.CalibrePath == "" {
|
|
|
|
conf.CalibrePath = "."
|
|
|
|
}
|
2017-08-01 13:37:58 +00:00
|
|
|
if conf.DbPath == "" {
|
2017-08-04 18:06:10 +00:00
|
|
|
conf.DbPath = conf.CalibrePath + "/metadata.db"
|
2017-08-01 13:37:58 +00:00
|
|
|
}
|
|
|
|
if conf.BindAddress == "" {
|
|
|
|
conf.BindAddress = ":9000"
|
|
|
|
}
|
|
|
|
return conf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func initApp() *BouquinsConf {
|
2017-07-31 17:37:52 +00:00
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
2017-08-01 13:37:58 +00:00
|
|
|
conf, err := ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2017-08-04 17:47:15 +00:00
|
|
|
|
|
|
|
tpl, err := TemplatesFunc().ParseGlob("templates/*.html")
|
2017-07-30 15:20:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2017-08-01 13:37:58 +00:00
|
|
|
db, err = sql.Open("sqlite3", conf.DbPath)
|
2017-07-30 18:14:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2017-08-01 18:00:20 +00:00
|
|
|
app := &Bouquins{
|
2017-07-30 15:20:48 +00:00
|
|
|
tpl,
|
2017-07-30 18:14:20 +00:00
|
|
|
db,
|
2017-07-30 15:20:48 +00:00
|
|
|
}
|
2017-08-02 17:54:09 +00:00
|
|
|
err = app.PrepareAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2017-08-04 18:06:10 +00:00
|
|
|
assets(conf.CalibrePath)
|
2017-07-30 14:06:38 +00:00
|
|
|
router(app)
|
2017-08-01 13:37:58 +00:00
|
|
|
return conf
|
2017-07-30 14:06:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 18:06:10 +00:00
|
|
|
func assets(calibre string) {
|
2017-08-01 18:00:20 +00:00
|
|
|
http.Handle(URL_JS, http.FileServer(http.Dir("assets")))
|
|
|
|
http.Handle(URL_CSS, http.FileServer(http.Dir("assets")))
|
|
|
|
http.Handle(URL_FONTS, http.FileServer(http.Dir("assets")))
|
2017-08-04 18:06:10 +00:00
|
|
|
http.Handle(URL_CALIBRE, http.StripPrefix(URL_CALIBRE, http.FileServer(http.Dir(calibre))))
|
2017-07-30 14:06:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 18:00:20 +00:00
|
|
|
func router(app *Bouquins) {
|
|
|
|
http.HandleFunc(URL_INDEX, app.IndexPage)
|
|
|
|
http.HandleFunc(URL_BOOKS, app.BooksPage)
|
|
|
|
http.HandleFunc(URL_AUTHORS, app.AuthorsPage)
|
|
|
|
http.HandleFunc(URL_SERIES, app.SeriesPage)
|
2017-08-06 10:50:43 +00:00
|
|
|
http.HandleFunc(URL_SEARCH, app.SearchPage)
|
2017-07-30 14:06:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-08-01 13:37:58 +00:00
|
|
|
conf := initApp()
|
2017-07-30 18:14:20 +00:00
|
|
|
defer db.Close()
|
2017-08-01 13:37:58 +00:00
|
|
|
http.ListenAndServe(conf.BindAddress, nil)
|
2017-07-30 14:06:38 +00:00
|
|
|
}
|