go-bouquins/main.go
2017-07-31 19:37:52 +02:00

61 lines
1.1 KiB
Go

package main
import (
"database/sql"
"html/template"
"log"
"net/http"
_ "github.com/mattn/go-sqlite3"
"meutel.net/meutel/go-bouquins/bouquins"
)
const (
INDEX = "/"
BOOKS = "/books"
AUTHORS = "/authors"
SERIES = "/series"
JS = "/js/"
CSS = "/css/"
FONTS = "/fonts/"
)
var db *sql.DB
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
tpl, err := template.ParseGlob("templates/*.html")
if err != nil {
log.Fatalln(err)
}
db, err = sql.Open("sqlite3", "calibre.db")
if err != nil {
log.Fatalln(err)
}
app := &bouquins.Bouquins{
tpl,
db,
}
assets()
router(app)
}
func assets() {
http.Handle(JS, http.FileServer(http.Dir("assets")))
http.Handle(CSS, http.FileServer(http.Dir("assets")))
http.Handle(FONTS, http.FileServer(http.Dir("assets")))
}
func router(app *bouquins.Bouquins) {
http.HandleFunc(INDEX, app.IndexPage)
http.HandleFunc(BOOKS, app.BooksPage)
http.HandleFunc(AUTHORS, app.AuthorsPage)
http.HandleFunc(SERIES, app.SeriesPage)
}
func main() {
defer db.Close()
http.ListenAndServe(":9000", nil)
}