Browse Source

added children db, simple render

master
Stephen McQuay 9 years ago
parent
commit
200f109e19
  1. 17
      db.go
  2. 4
      handlers.go
  3. 9
      templates/index.html

17
db.go

@ -5,8 +5,11 @@ import (
"encoding/json"
"io/ioutil"
"log"
"sync"
)
var dbMutex = sync.RWMutex{}
func get_passes(filename string) (cur_passes []string, err error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
@ -49,3 +52,17 @@ func check_password(filename, attempt string) (result bool) {
}
return
}
func loadChildren(filename string) (children []Child) {
dbMutex.RLock()
defer dbMutex.RUnlock()
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(b, &children)
if err != nil {
log.Fatal(err)
}
return
}

4
handlers.go

@ -10,7 +10,9 @@ func homeHandler(w http.ResponseWriter, req *http.Request) {
if loggedIn == nil {
http.Redirect(w, req, "/login", http.StatusSeeOther)
}
T("index.html").Execute(w, map[string]interface{}{})
children := loadChildren(*db_file)
T("index.html").Execute(w, map[string]interface{}{
"children": children,})
}
func loginHandler(w http.ResponseWriter, req *http.Request) {

9
templates/index.html

@ -2,6 +2,13 @@
{{ define "content" }}
<div class="container pw">
This will have stuff
This will have stuff {{ .children }}
<ul>
{{ range .children }}
<li>
{{ .Name }} {{ .Money }}
</li>
{{ end }}
</ul>
</div>
{{ end }}

Loading…
Cancel
Save