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-09-08 10:02:54 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/github"
|
|
|
|
|
2017-09-08 14:39:22 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2017-07-31 17:37:52 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
2017-08-06 18:25:20 +00:00
|
|
|
"meutel.net/meutel/go-bouquins/bouquins"
|
2017-07-30 14:06:38 +00:00
|
|
|
)
|
|
|
|
|
2017-08-06 18:25:20 +00:00
|
|
|
// BouquinsConf App configuration
|
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-09-05 15:35:10 +00:00
|
|
|
Prod bool `json:"prod"`
|
2017-08-01 13:37:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 18:14:20 +00:00
|
|
|
var db *sql.DB
|
|
|
|
|
2017-08-06 18:25:20 +00:00
|
|
|
// ReadConfig loads configuration file and initialize default value
|
2017-08-01 13:37:58 +00:00
|
|
|
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
|
|
|
|
2017-09-05 15:35:10 +00:00
|
|
|
tpl, err := bouquins.TemplatesFunc(conf.Prod).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-09-08 10:02:54 +00:00
|
|
|
|
2017-09-08 14:39:22 +00:00
|
|
|
// TODO conf by provider, client ID and secret in conf file
|
2017-09-08 18:29:01 +00:00
|
|
|
oauthConf := make(map[string]*oauth2.Config)
|
|
|
|
oauthConf["github"] = &oauth2.Config{
|
2017-09-08 10:02:54 +00:00
|
|
|
ClientID: "8b0aedf07828f06918a0",
|
|
|
|
ClientSecret: "eb26ec9c986fc28bd169bdddf169b794861e0d65",
|
2017-09-08 14:39:22 +00:00
|
|
|
Scopes: []string{"user:email"},
|
2017-09-08 10:02:54 +00:00
|
|
|
Endpoint: github.Endpoint,
|
|
|
|
}
|
|
|
|
|
2017-09-08 14:39:22 +00:00
|
|
|
// FIXME constructor, conf cookies secret
|
|
|
|
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK"))}
|
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-09-05 14:54:25 +00:00
|
|
|
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"))))
|
2017-08-06 18:25:20 +00:00
|
|
|
http.Handle(bouquins.URLCalibre, http.StripPrefix(bouquins.URLCalibre, http.FileServer(http.Dir(calibre))))
|
2017-07-30 14:06:38 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 16:49:40 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 18:25:20 +00:00
|
|
|
func handleURL(url string, f func(res http.ResponseWriter, req *http.Request) error) {
|
2017-08-06 16:49:40 +00:00
|
|
|
http.HandleFunc(url, handle(f))
|
|
|
|
}
|
|
|
|
|
2017-08-06 18:25:20 +00:00
|
|
|
func router(app *bouquins.Bouquins) {
|
|
|
|
handleURL(bouquins.URLIndex, app.IndexPage)
|
2017-09-08 10:02:54 +00:00
|
|
|
handleURL(bouquins.URLLogin, app.LoginPage)
|
2017-09-08 16:13:22 +00:00
|
|
|
handleURL(bouquins.URLLogout, app.LogoutPage)
|
2017-09-08 10:32:26 +00:00
|
|
|
handleURL(bouquins.URLCallback, app.CallbackPage)
|
2017-08-06 18:25:20 +00:00
|
|
|
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)
|
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
|
|
|
}
|