Skip to content

Commit

Permalink
Merge f365aee into 40b4f7b
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed Mar 22, 2017
2 parents 40b4f7b + f365aee commit 389fa51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func accountRecordsToAccountAttributeRecords(accountRecords []redis.AccountRecor
return cashBalanceRecords
}

// collapseAccountAttributeRecords collapses the records in a slice to remove
// sequences where the value does not change between adjacent records.
func collapseAccountAttributeRecords(r []AccountAttributeRecord) []AccountAttributeRecord {
if len(r) <= 1 {
return r
Expand Down
38 changes: 25 additions & 13 deletions account/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ import (
"net/http"
)

type accountInfoFunc func(m accountInfoManager) (interface{}, error)
type (
accountInfoFunc func(m accountInfoManager) (interface{}, error)

func accountAttributeHandler(f accountInfoFunc) http.Handler {
Handlers struct {
AccountInfoManager accountInfoManager
}
)

func NewHandlers() (Handlers, error) {
m, err := NewAccountInfoManager()
if err != nil {
return Handlers{}, err
}
return Handlers{AccountInfoManager: m}, nil
}

func (h Handlers) accountAttributeHandler(f accountInfoFunc) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
cbm, err := NewAccountInfoManager()
if err != nil {
w.Write([]byte(fmt.Sprintf("failed to get account information: %v", err)))
return
}
defer cbm.Close()
b, err := f(cbm)
b, err := f(h.AccountInfoManager)
if err != nil {
w.Write([]byte(fmt.Sprintf("failed to get account information: %v", err)))
return
Expand All @@ -31,16 +39,20 @@ func accountAttributeHandler(f accountInfoFunc) http.Handler {
return http.HandlerFunc(fn)
}

func CashBalanceHistoryHandler() http.Handler {
return accountAttributeHandler(
func (h Handlers) CashBalanceHistoryHandler() http.Handler {
return h.accountAttributeHandler(
func(m accountInfoManager) (interface{}, error) {
return m.CashBalanceHistory()
})
}

func AccountValueHistoryHandler() http.Handler {
return accountAttributeHandler(
func (h Handlers) AccountValueHistoryHandler() http.Handler {
return h.accountAttributeHandler(
func(m accountInfoManager) (interface{}, error) {
return m.AccountValueHistory()
})
}

func (h *Handlers) Close() {
h.AccountInfoManager.Close()
}
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ func main() {
flag.Parse()
log.Printf("starting up dashboard on port %d", *p)

http.Handle("/cashBalanceHistory", account.CashBalanceHistoryHandler())
http.Handle("/accountValueHistory", account.AccountValueHistoryHandler())
h, err := account.NewHandlers()
if err != nil {
log.Fatal(fmt.Sprintf("failed to initialize handlers: %v", err))
}
defer h.Close()
http.Handle("/cashBalanceHistory", h.CashBalanceHistoryHandler())
http.Handle("/accountValueHistory", h.AccountValueHistoryHandler())
http.Handle("/notes.json", notes.NotesHandler())

log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *p), nil))
Expand Down

0 comments on commit 389fa51

Please sign in to comment.