Meutel
229c2d535e
Some checks reported errors
continuous-integration/drone/push Build was killed
* log on startup * display on about page * use compile flag * set with git tag value
134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"github.com/gorilla/sessions"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"meutel.net/meutel/go-bouquins/bouquins"
|
|
)
|
|
|
|
var (
|
|
version string
|
|
)
|
|
|
|
// ReadConfig loads configuration file and initialize default value
|
|
func ReadConfig() (*bouquins.Conf, error) {
|
|
conf := new(bouquins.Conf)
|
|
confPath := "bouquins.yml"
|
|
if len(os.Args) > 1 {
|
|
confPath = os.Args[1]
|
|
}
|
|
confFile, err := os.Open(confPath)
|
|
if err == nil {
|
|
defer confFile.Close()
|
|
err = yaml.NewDecoder(confFile).Decode(conf)
|
|
} else {
|
|
log.Println("no conf file, using defaults")
|
|
err = nil
|
|
}
|
|
// default values
|
|
if conf.CalibrePath == "" {
|
|
conf.CalibrePath = "./data"
|
|
}
|
|
if conf.DbPath == "" {
|
|
conf.DbPath = conf.CalibrePath + "/metadata.db"
|
|
}
|
|
if conf.UserDbPath == "" {
|
|
conf.UserDbPath = "./data/users.db"
|
|
}
|
|
if conf.BindAddress == "" {
|
|
conf.BindAddress = ":9000"
|
|
}
|
|
return conf, err
|
|
}
|
|
|
|
func initApp() *bouquins.Bouquins {
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
conf, err := ReadConfig()
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
tpl, err := bouquins.TemplatesFunc(conf.Prod, version).ParseGlob("templates/*.html")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
db, err := sql.Open("sqlite3", conf.DbPath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
userdb, err := sql.Open("sqlite3", conf.UserDbPath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
app := &bouquins.Bouquins{
|
|
Tpl: tpl,
|
|
DB: db,
|
|
UserDB: userdb,
|
|
Conf: conf,
|
|
OAuthConf: make(map[string]*oauth2.Config),
|
|
Cookies: sessions.NewCookieStore([]byte(conf.CookieSecret)),
|
|
}
|
|
for _, provider := range bouquins.Providers {
|
|
app.OAuthConf[provider.Name()] = provider.Config(conf)
|
|
}
|
|
err = app.PrepareAll()
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
router(app)
|
|
return app
|
|
}
|
|
|
|
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")))
|
|
}
|
|
|
|
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) {
|
|
assets(app.Conf.CalibrePath)
|
|
http.Handle(bouquins.URLCalibre, app.CalibreFileServer())
|
|
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() {
|
|
log.Println("go-bouquins", version)
|
|
app := initApp()
|
|
defer app.DB.Close()
|
|
defer app.UserDB.Close()
|
|
http.ListenAndServe(app.Conf.BindAddress, nil)
|
|
}
|