2015-08-09 11:17:05 -07:00
|
|
|
package allowances
|
2013-02-20 21:27:09 -08:00
|
|
|
|
|
|
|
import (
|
2013-03-08 22:28:24 -08:00
|
|
|
"encoding/json"
|
2015-08-09 11:17:05 -07:00
|
|
|
"fmt"
|
2013-03-08 22:28:24 -08:00
|
|
|
"log"
|
2013-02-20 21:27:09 -08:00
|
|
|
"net/http"
|
2013-03-08 22:28:24 -08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-08-09 11:17:05 -07:00
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
2015-08-09 15:38:35 -07:00
|
|
|
"mcquay.me/allowances/children"
|
|
|
|
"mcquay.me/fs"
|
2013-02-20 21:27:09 -08:00
|
|
|
)
|
|
|
|
|
2015-08-09 11:17:05 -07:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.Lshortfile | log.Ltime)
|
|
|
|
}
|
|
|
|
|
|
|
|
type failure struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFailure(msg string) *failure {
|
|
|
|
return &failure{
|
|
|
|
Success: false,
|
|
|
|
Error: msg,
|
2013-02-21 00:19:38 -08:00
|
|
|
}
|
2013-02-20 21:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-08-09 11:17:05 -07:00
|
|
|
type Allowances struct {
|
2015-08-09 15:38:35 -07:00
|
|
|
db children.DB
|
|
|
|
dbfile string
|
2015-08-09 11:17:05 -07:00
|
|
|
hashes Passes
|
|
|
|
store *sessions.CookieStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAllowances(sm *http.ServeMux, dbfile, passfile, staticFiles string) (*Allowances, error) {
|
|
|
|
var err error
|
|
|
|
tmpls, err = getTemplates()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hashes, exists, err := GetHashes(passfile)
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("passes file doesn't exist: %q", passfile)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-09 15:38:35 -07:00
|
|
|
if !fs.Exists(dbfile) {
|
2015-08-09 11:17:05 -07:00
|
|
|
return nil, fmt.Errorf("child db file doesn't exist: %q", dbfile)
|
|
|
|
}
|
2015-08-09 15:38:35 -07:00
|
|
|
db, err := children.Load(dbfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-09 11:17:05 -07:00
|
|
|
r := &Allowances{
|
2015-08-09 15:38:35 -07:00
|
|
|
db: db,
|
|
|
|
dbfile: dbfile,
|
2015-08-09 11:17:05 -07:00
|
|
|
hashes: hashes,
|
|
|
|
store: sessions.NewCookieStore([]byte("hello world")),
|
|
|
|
}
|
|
|
|
addRoutes(sm, r, staticFiles)
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Allowances) home(w http.ResponseWriter, req *http.Request, uid string) error {
|
2015-08-09 15:38:35 -07:00
|
|
|
tmpls["home"].Execute(w, map[string]interface{}{"children": a.db})
|
2015-08-09 11:17:05 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Allowances) login(w http.ResponseWriter, req *http.Request) {
|
|
|
|
attempt := req.FormValue("passwd")
|
|
|
|
ok, err := a.hashes.Check(attempt)
|
|
|
|
if err != nil {
|
|
|
|
b, _ := json.Marshal(NewFailure(err.Error()))
|
|
|
|
http.Error(w, string(b), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
session, _ := a.store.Get(req, sessionName)
|
|
|
|
session.Values["uuid"] = "me"
|
2013-02-26 22:30:23 -08:00
|
|
|
session.Save(req, w)
|
2013-02-21 00:36:32 -08:00
|
|
|
http.Redirect(w, req, "/", http.StatusSeeOther)
|
2013-02-26 22:30:23 -08:00
|
|
|
return
|
|
|
|
}
|
2015-08-09 11:17:05 -07:00
|
|
|
tmpls["login"].Execute(w, map[string]interface{}{})
|
2013-02-20 21:27:09 -08:00
|
|
|
}
|
2013-02-26 22:39:26 -08:00
|
|
|
|
2015-08-09 11:17:05 -07:00
|
|
|
func (a *Allowances) logout(w http.ResponseWriter, req *http.Request, u string) error {
|
|
|
|
session, err := a.store.Get(req, sessionName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(session.Values, "uuid")
|
2013-02-26 23:19:21 -08:00
|
|
|
session.Save(req, w)
|
|
|
|
http.Redirect(w, req, "/", http.StatusSeeOther)
|
2015-08-09 11:17:05 -07:00
|
|
|
return nil
|
2013-02-26 22:39:26 -08:00
|
|
|
}
|
2013-03-08 22:28:24 -08:00
|
|
|
|
2015-08-09 11:17:05 -07:00
|
|
|
func (a *Allowances) add(w http.ResponseWriter, req *http.Request, uid string) error {
|
|
|
|
path := req.URL.Path[len(prefix["add"]):]
|
2013-03-08 22:28:24 -08:00
|
|
|
bits := strings.Split(path, "/")
|
|
|
|
child := bits[0]
|
|
|
|
amount, err := strconv.Atoi(bits[1])
|
|
|
|
if err != nil {
|
2015-08-09 11:17:05 -07:00
|
|
|
return fmt.Errorf("couldn't parse a dollar amount: %v", err)
|
2013-03-08 22:28:24 -08:00
|
|
|
}
|
2015-08-09 15:38:35 -07:00
|
|
|
a.db[child] += amount
|
|
|
|
a.db.Save(a.dbfile)
|
2013-03-08 22:28:24 -08:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2015-08-09 11:17:05 -07:00
|
|
|
b, err := json.Marshal(map[string]interface{}{
|
2015-08-09 15:38:35 -07:00
|
|
|
"amount": dollarize(a.db[child]),
|
2015-08-09 11:17:05 -07:00
|
|
|
"name": child,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-08 22:28:24 -08:00
|
|
|
w.Write(b)
|
2015-08-09 11:17:05 -07:00
|
|
|
return nil
|
2013-03-08 22:28:24 -08:00
|
|
|
}
|