Refactor router initialization into constructor
This commit is contained in:
parent
4830fbc870
commit
08d41b0348
3 changed files with 25 additions and 15 deletions
1
TODO.md
1
TODO.md
|
|
@ -28,6 +28,7 @@
|
||||||
## Security
|
## Security
|
||||||
|
|
||||||
- [x] Authentication System
|
- [x] Authentication System
|
||||||
|
- [ ] Persistant logins accross server restarts
|
||||||
- [x] User login
|
- [x] User login
|
||||||
- [ ] User logout
|
- [ ] User logout
|
||||||
- [ ] Role-based access (admin, staff)
|
- [ ] Role-based access (admin, staff)
|
||||||
|
|
|
||||||
18
main.go
18
main.go
|
|
@ -10,21 +10,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
h, err := router.New()
|
||||||
var h router.Router
|
if err != nil {
|
||||||
var a auth.Authenticator
|
log.Fatal(err)
|
||||||
|
}
|
||||||
defer h.Close()
|
defer h.Close()
|
||||||
|
|
||||||
err = h.LoadTemplates()
|
var a auth.Authenticator
|
||||||
if err != nil {
|
|
||||||
log.Fatal("can't parse templates: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = h.OenStore()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("can't open the databse", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
a = auth.New()
|
a = auth.New()
|
||||||
|
|
||||||
// #todo: make handler functions private, and create a servemux in router
|
// #todo: make handler functions private, and create a servemux in router
|
||||||
|
|
|
||||||
|
|
@ -22,19 +22,36 @@ func (h *Router) Close() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Router) OenStore() error {
|
func (h *Router) openStore() error {
|
||||||
var err error
|
var err error
|
||||||
h.s, err = store.OpenStore("./foo.db")
|
h.s, err = store.OpenStore("./foo.db")
|
||||||
|
if err != nil {
|
||||||
|
h.s.Close()
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// #todo embed templates, and load them in init function
|
// #todo embed templates, and load them in init function
|
||||||
func (h *Router) LoadTemplates() error {
|
func (h *Router) loadTemplates() error {
|
||||||
var err error
|
var err error
|
||||||
h.tmpl, err = template.ParseFiles("index.html", "order.html")
|
h.tmpl, err = template.ParseFiles("index.html", "order.html")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func New() (*Router, error) {
|
||||||
|
var router Router
|
||||||
|
var err error
|
||||||
|
err = router.loadTemplates()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = router.openStore()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &router, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Router) Form(w http.ResponseWriter, r *http.Request) {
|
func (h *Router) Form(w http.ResponseWriter, r *http.Request) {
|
||||||
url := strings.Split(r.URL.String(), "/")
|
url := strings.Split(r.URL.String(), "/")
|
||||||
o := store.Order{
|
o := store.Order{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue