Major refactor
- use embedded static assets - added pw management subcommands
This commit is contained in:
+88
-30
@@ -1,60 +1,118 @@
|
||||
package main
|
||||
package allowances
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
func homeHandler(w http.ResponseWriter, req *http.Request) {
|
||||
session, _ := store.Get(req, "creds")
|
||||
loggedIn := session.Values["logged in"]
|
||||
if loggedIn == nil {
|
||||
http.Redirect(w, req, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
children := loadChildren(*db_file)
|
||||
T("index.html").Execute(w, map[string]interface{}{
|
||||
"children": children})
|
||||
func init() {
|
||||
log.SetFlags(log.Lshortfile | log.Ltime)
|
||||
}
|
||||
|
||||
func loginHandler(w http.ResponseWriter, req *http.Request) {
|
||||
pwAttempt := req.FormValue("passwd")
|
||||
if check_password(*passes_file, pwAttempt) {
|
||||
session, _ := store.Get(req, "creds")
|
||||
session.Values["logged in"] = true
|
||||
type failure struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func NewFailure(msg string) *failure {
|
||||
return &failure{
|
||||
Success: false,
|
||||
Error: msg,
|
||||
}
|
||||
}
|
||||
|
||||
type Allowances struct {
|
||||
db string
|
||||
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
|
||||
}
|
||||
|
||||
if !Exists(dbfile) {
|
||||
return nil, fmt.Errorf("child db file doesn't exist: %q", dbfile)
|
||||
}
|
||||
r := &Allowances{
|
||||
db: dbfile,
|
||||
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 {
|
||||
children := loadChildren(a.db)
|
||||
tmpls["home"].Execute(w, map[string]interface{}{"children": children})
|
||||
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"
|
||||
session.Save(req, w)
|
||||
http.Redirect(w, req, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
T("login.html").Execute(w, map[string]interface{}{})
|
||||
tmpls["login"].Execute(w, map[string]interface{}{})
|
||||
}
|
||||
|
||||
func logoutHandler(w http.ResponseWriter, req *http.Request) {
|
||||
session, _ := store.Get(req, "creds")
|
||||
delete(session.Values, "logged in")
|
||||
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")
|
||||
session.Save(req, w)
|
||||
http.Redirect(w, req, "/", http.StatusSeeOther)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
func addHandler(w http.ResponseWriter, req *http.Request) {
|
||||
path := req.URL.Path[len(addPath):]
|
||||
func (a *Allowances) add(w http.ResponseWriter, req *http.Request, uid string) error {
|
||||
path := req.URL.Path[len(prefix["add"]):]
|
||||
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)
|
||||
return fmt.Errorf("couldn't parse a dollar amount: %v", err)
|
||||
}
|
||||
children := loadChildren(*db_file)
|
||||
children := loadChildren(a.db)
|
||||
children[child] += amount
|
||||
defer dumpChildren(*db_file, children)
|
||||
defer dumpChildren(a.db, children)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
b, err := json.Marshal(map[string]interface{}{
|
||||
"amount": dollarize(children[child]),
|
||||
"name": child,
|
||||
})
|
||||
b, err := json.Marshal(map[string]interface{}{
|
||||
"amount": dollarize(children[child]),
|
||||
"name": child,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Write(b)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user