go-bouquins/main.go

134 lines
3.1 KiB
Go
Raw Permalink Normal View History

2017-07-30 14:06:38 +00:00
package main
import (
2017-07-30 18:14:20 +00:00
"database/sql"
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
"gopkg.in/yaml.v2"
2017-09-08 10:02:54 +00:00
"golang.org/x/oauth2"
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
)
var (
version string
)
2017-08-06 18:25:20 +00:00
// ReadConfig loads configuration file and initialize default value
2017-09-09 11:27:07 +00:00
func ReadConfig() (*bouquins.Conf, error) {
conf := new(bouquins.Conf)
2019-09-12 12:34:26 +00:00
confPath := "bouquins.yml"
2017-08-01 13:37:58 +00:00
if len(os.Args) > 1 {
confPath = os.Args[1]
}
confFile, err := os.Open(confPath)
if err == nil {
defer confFile.Close()
2019-09-12 12:34:26 +00:00
err = yaml.NewDecoder(confFile).Decode(conf)
2017-08-01 13:37:58 +00:00
} else {
log.Println("no conf file, using defaults")
err = nil
}
// default values
2017-08-04 18:06:10 +00:00
if conf.CalibrePath == "" {
2019-09-28 09:58:45 +00:00
conf.CalibrePath = "./data"
2017-08-04 18:06:10 +00:00
}
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
}
2017-09-09 15:12:37 +00:00
if conf.UserDbPath == "" {
2019-09-28 09:58:45 +00:00
conf.UserDbPath = "./data/users.db"
2017-09-09 15:12:37 +00:00
}
2017-08-01 13:37:58 +00:00
if conf.BindAddress == "" {
conf.BindAddress = ":9000"
}
return conf, err
}
2017-09-09 15:12:37 +00:00
func initApp() *bouquins.Bouquins {
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
tpl, err := bouquins.TemplatesFunc(conf.Prod, version).ParseGlob("templates/*.html")
2017-07-30 15:20:48 +00:00
if err != nil {
log.Fatalln(err)
}
2017-09-09 15:12:37 +00:00
db, err := sql.Open("sqlite3", conf.DbPath)
if err != nil {
log.Fatalln(err)
}
userdb, err := sql.Open("sqlite3", conf.UserDbPath)
2017-07-30 18:14:20 +00:00
if err != nil {
log.Fatalln(err)
}
2017-09-08 10:02:54 +00:00
2017-09-09 15:12:37 +00:00
app := &bouquins.Bouquins{
Tpl: tpl,
DB: db,
UserDB: userdb,
Conf: conf,
OAuthConf: make(map[string]*oauth2.Config),
Cookies: sessions.NewCookieStore([]byte(conf.CookieSecret)),
}
2017-09-09 07:34:19 +00:00
for _, provider := range bouquins.Providers {
2017-09-09 15:12:37 +00:00
app.OAuthConf[provider.Name()] = provider.Config(conf)
2017-09-08 10:02:54 +00:00
}
2017-08-02 17:54:09 +00:00
err = app.PrepareAll()
if err != nil {
log.Fatalln(err)
}
2017-07-30 14:06:38 +00:00
router(app)
2017-09-09 15:12:37 +00:00
return app
2017-07-30 14:06:38 +00:00
}
2017-08-04 18:06:10 +00:00
func assets(calibre string) {
http.Handle(bouquins.URLJs, http.FileServer(http.Dir("assets")))
http.Handle(bouquins.URLCss, http.FileServer(http.Dir("assets")))
http.Handle(bouquins.URLFonts, http.FileServer(http.Dir("assets")))
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) {
2017-09-09 16:03:59 +00:00
assets(app.Conf.CalibrePath)
http.Handle(bouquins.URLCalibre, app.CalibreFileServer())
2017-08-06 18:25:20 +00:00
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() {
log.Println("go-bouquins", version)
2017-09-09 15:12:37 +00:00
app := initApp()
defer app.DB.Close()
defer app.UserDB.Close()
http.ListenAndServe(app.Conf.BindAddress, nil)
2017-07-30 14:06:38 +00:00
}