go-bouquins/main.go
2017-08-06 16:05:58 +02:00

100 lines
2.1 KiB
Go

package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
. "meutel.net/meutel/go-bouquins/bouquins"
)
type BouquinsConf struct {
BindAddress string `json:"bind-address"`
DbPath string `json:"db-path"`
CalibrePath string `json:"calibre-path"`
}
var db *sql.DB
// 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
if conf.CalibrePath == "" {
conf.CalibrePath = "."
}
if conf.DbPath == "" {
conf.DbPath = conf.CalibrePath + "/metadata.db"
}
if conf.BindAddress == "" {
conf.BindAddress = ":9000"
}
return conf, err
}
func initApp() *BouquinsConf {
log.SetFlags(log.LstdFlags | log.Lshortfile)
conf, err := ReadConfig()
if err != nil {
log.Fatalln(err)
}
tpl, err := TemplatesFunc().ParseGlob("templates/*.html")
if err != nil {
log.Fatalln(err)
}
db, err = sql.Open("sqlite3", conf.DbPath)
if err != nil {
log.Fatalln(err)
}
app := &Bouquins{
tpl,
db,
}
err = app.PrepareAll()
if err != nil {
log.Fatalln(err)
}
assets(conf.CalibrePath)
router(app)
return conf
}
func assets(calibre string) {
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")))
http.Handle(URL_CALIBRE, http.StripPrefix(URL_CALIBRE, http.FileServer(http.Dir(calibre))))
}
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)
http.HandleFunc(URL_SEARCH, app.SearchPage)
http.HandleFunc(URL_ABOUT, app.AboutPage)
}
func main() {
conf := initApp()
defer db.Close()
http.ListenAndServe(conf.BindAddress, nil)
}