125 lines
3.1 KiB
Go
125 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"golang.org/x/oauth2"
|
|
"golang.org/x/oauth2/github"
|
|
|
|
_ "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)
|
|
}
|
|
|
|
oauthConf := &oauth2.Config{
|
|
ClientID: "8b0aedf07828f06918a0",
|
|
ClientSecret: "eb26ec9c986fc28bd169bdddf169b794861e0d65",
|
|
Scopes: []string{"user"},
|
|
Endpoint: github.Endpoint,
|
|
}
|
|
|
|
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf}
|
|
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.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)
|
|
}
|