package main import ( "database/sql" "encoding/json" "log" "net/http" "os" "golang.org/x/oauth2" "github.com/gorilla/sessions" _ "github.com/mattn/go-sqlite3" "meutel.net/meutel/go-bouquins/bouquins" ) var db *sql.DB // ReadConfig loads configuration file and initialize default value func ReadConfig() (*bouquins.BouquinsConf, error) { conf := new(bouquins.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() *bouquins.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 := make(map[string]*oauth2.Config) for _, provider := range bouquins.Providers { oauthConf[provider.Name()] = provider.Config(conf) } // FIXME constructor, conf cookies secret app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte(conf.CookieSecret))} 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.URLLogout, app.LogoutPage) 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) }