go-bouquins/main.go
2017-09-08 16:39:22 +02:00

129 lines
3.3 KiB
Go

package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"github.com/gorilla/sessions"
_ "github.com/mattn/go-sqlite3"
"meutel.net/meutel/go-bouquins/bouquins"
)
// BouquinsConf App configuration
type BouquinsConf struct {
BindAddress string `json:"bind-address"`
DbPath string `json:"db-path"`
CalibrePath string `json:"calibre-path"`
Prod bool `json:"prod"`
}
var db *sql.DB
// ReadConfig loads configuration file and initialize default value
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 := bouquins.TemplatesFunc(conf.Prod).ParseGlob("templates/*.html")
if err != nil {
log.Fatalln(err)
}
db, err = sql.Open("sqlite3", conf.DbPath)
if err != nil {
log.Fatalln(err)
}
// TODO conf by provider, client ID and secret in conf file
oauthConf := &oauth2.Config{
ClientID: "8b0aedf07828f06918a0",
ClientSecret: "eb26ec9c986fc28bd169bdddf169b794861e0d65",
Scopes: []string{"user:email"},
Endpoint: github.Endpoint,
}
// FIXME constructor, conf cookies secret
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK"))}
err = app.PrepareAll()
if err != nil {
log.Fatalln(err)
}
assets(conf.CalibrePath)
router(app)
return conf
}
func assets(calibre string) {
http.Handle(bouquins.URLJs, http.StripPrefix("/"+bouquins.Version, http.FileServer(http.Dir("assets"))))
http.Handle(bouquins.URLCss, http.StripPrefix("/"+bouquins.Version, http.FileServer(http.Dir("assets"))))
http.Handle(bouquins.URLFonts, http.StripPrefix("/"+bouquins.Version, http.FileServer(http.Dir("assets"))))
http.Handle(bouquins.URLCalibre, http.StripPrefix(bouquins.URLCalibre, http.FileServer(http.Dir(calibre))))
}
func handle(f func(res http.ResponseWriter, req *http.Request) error) func(res http.ResponseWriter, req *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
err := f(res, req)
if err != nil {
log.Println(err)
http.Error(res, err.Error(), 500)
}
}
}
func handleURL(url string, f func(res http.ResponseWriter, req *http.Request) error) {
http.HandleFunc(url, handle(f))
}
func router(app *bouquins.Bouquins) {
handleURL(bouquins.URLIndex, app.IndexPage)
handleURL(bouquins.URLLogin, app.LoginPage)
handleURL(bouquins.URLCallback, app.CallbackPage)
handleURL(bouquins.URLBooks, app.BooksPage)
handleURL(bouquins.URLAuthors, app.AuthorsPage)
handleURL(bouquins.URLSeries, app.SeriesPage)
handleURL(bouquins.URLSearch, app.SearchPage)
handleURL(bouquins.URLAbout, app.AboutPage)
}
func main() {
conf := initApp()
defer db.Close()
http.ListenAndServe(conf.BindAddress, nil)
}