Got app working

This commit is contained in:
Stephen McQuay 2013-03-08 22:28:24 -08:00
parent 6a62e10c26
commit d48fd3f852
6 changed files with 49 additions and 17 deletions

View File

@ -1,6 +0,0 @@
package main
type Child struct {
Name string
Money int
}

13
db.go
View File

@ -53,7 +53,7 @@ func check_password(filename, attempt string) (result bool) {
return
}
func loadChildren(filename string) (children []Child) {
func loadChildren(filename string) (children map[string]int) {
dbMutex.RLock()
defer dbMutex.RUnlock()
b, err := ioutil.ReadFile(filename)
@ -66,3 +66,14 @@ func loadChildren(filename string) (children []Child) {
}
return
}
func dumpChildren(filename string, children map[string]int) {
dbMutex.Lock()
defer dbMutex.Unlock()
b, err := json.Marshal(children)
err = ioutil.WriteFile(filename, b, 0644)
if err != nil {
log.Fatal("serious issue writing children db file", err)
}
return
}

View File

@ -1,7 +1,11 @@
package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
"strings"
)
func homeHandler(w http.ResponseWriter, req *http.Request) {
@ -35,3 +39,22 @@ func logoutHandler(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
func addHandler(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path[len(addPath):]
bits := strings.Split(path, "/")
child := bits[0]
amount, err := strconv.Atoi(bits[1])
if err != nil {
log.Fatal("couldn't parse a dollar amount", err)
}
children := loadChildren(*db_file)
children[child] += amount
defer dumpChildren(*db_file, children)
w.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(map[string]interface{}{
"amount": dollarize(children[child]),
"name": child,
})
w.Write(b)
}

View File

@ -17,6 +17,8 @@ var template_dir = flag.String("templates", "templates", "template dir")
var add_pw = flag.String("passwd", "", "add this pass to the db")
var check_pw = flag.String("checkpw", "", "check if this pw is in db")
var addPath = "/add/"
var store = sessions.NewCookieStore([]byte("hello world"))
var templates *template.Template
@ -31,6 +33,7 @@ func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/login", loginHandler)
http.HandleFunc("/logout", logoutHandler)
http.HandleFunc(addPath, addHandler)
http.Handle("/s/", http.StripPrefix("/s/",
http.FileServer(http.Dir(*static_files))))
if err := http.ListenAndServe(*addr, nil); err != nil {

View File

@ -4,8 +4,9 @@ $(function() {
$(this).next(".controls").toggle();
});
$(".btn").click(function(e) {
var amount = $(this)[0].value;
var name = $(this).parent().parent().parent().prev().find(".name").text();
console.log(name + " " + amount);
var target = $(this).val();
var req = $.get(target, function(data) {
$("." + data["name"]).next().text(data["amount"]);
});
});
});

View File

@ -15,18 +15,18 @@
</tr>
</thead>
<tbody>
{{ range .children }}
{{ range $name, $money := .children }}
<tr class="child">
<td class="name">{{ .Name }}</td>
<td>{{ dollarize .Money }}</td>
<td class="name {{ $name }}">{{ $name }}</td>
<td class="amount">{{ dollarize $money }}</td>
</tr>
<tr class="controls">
<td colspan=2 style="text-align: center;">
<div class="btn-group">
<button class="btn" value=100>$1.00</button>
<button class="btn" value=25>25&cent;</button>
<button class="btn" value=10>10&cent;</button>
<button class="btn" value=5>5&cent;</button>
<button class="btn" value="/add/{{ $name}}/100">$1.00</button>
<button class="btn" value="/add/{{ $name}}/25">25&cent;</button>
<button class="btn" value="/add/{{ $name}}/10">10&cent;</button>
<button class="btn" value="/add/{{ $name}}/5">5&cent;</button>
</div>
</td>
</tr>