Separate db initialization from store opening

This commit is contained in:
bronku 2025-03-21 18:50:26 +01:00
parent 646501d609
commit b61bae7cd8
5 changed files with 36 additions and 30 deletions

25
main.go
View file

@ -4,33 +4,28 @@ import (
"html/template"
"log"
"net/http"
"time"
_ "github.com/mattn/go-sqlite3"
)
func main() {
var err error
var h handler
templates, err := template.ParseFiles("index.html", "order.html")
defer h.close()
h.tmpl, err = template.ParseFiles("index.html", "order.html")
if err != nil {
log.Fatal("can't parse templates: ", err)
}
h.tmpl = templates
// #todo: error handling
h.s, _ = NewStore("./foo.db")
h.s.saveCake(cake{"Sernik ulubiony", -1, 65, 0})
h.s.saveCake(cake{"Malinowa chmurka", -1, 150, 0})
h.s.saveCake(cake{"Mako sernik", -1, 150, 0})
h.s.saveCake(cake{"Rolada makowa", -1, 80, 0})
h.s.saveCake(cake{"Wieniec bezowy", -1, 120, 0})
h.s.saveOrder(order{-1, "Albert", "Camus", "123456789", "Kartuzy", time.Now().AddDate(0, 0, 7), time.Now(), "accepted", 0, nil})
h.s.saveOrder(order{-1, "George", "Orwell", "", "Kartuzy", time.Now().AddDate(0, 1, 0), time.Now(), "accepted", 0, nil})
h.s.saveOrder(order{-1, "Karl", "Marx", "0700", "Somonino", time.Now(), time.Now(), "accepted", 0, nil})
h.s, err = openStore("./foo.db")
if err != nil {
log.Fatal("can't open the databse", err)
}
http.HandleFunc("GET /order/", logger(h.form))
http.HandleFunc("GET /", logger(h.index))
http.HandleFunc("POST /", logger(h.addOrder))
http.ListenAndServe(":8080", nil)
go http.ListenAndServe(":8080", nil)
adminConsole()
}