From 383338ab47a8a2592acefb384c6614f95a43e8c1 Mon Sep 17 00:00:00 2001 From: sp Date: Thu, 4 Mar 2021 16:47:50 +1100 Subject: [PATCH] loan summary chart support is ready --- apiV1Response.go | 11 +++++++++++ apiV1login.go | 2 +- apiV1logout.go | 20 ++++++++++++++++++++ apiv1.go | 10 ++++++++-- chartTypeOfLoans.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 apiV1logout.go create mode 100644 chartTypeOfLoans.go diff --git a/apiV1Response.go b/apiV1Response.go index d13e63b..23d2300 100644 --- a/apiV1Response.go +++ b/apiV1Response.go @@ -4,6 +4,7 @@ import ( "biukop.com/sfm/loan" "encoding/json" "fmt" + log "github.com/sirupsen/logrus" "net/http" "time" ) @@ -64,3 +65,13 @@ func (m *apiV1Response) sendJson(w http.ResponseWriter) (ret []byte, e error) { fmt.Fprint(w, string(ret)) return } + +func apiV1SendJson(result interface{}, w http.ResponseWriter, r *http.Request, ss *loan.Session) { + out, e := json.Marshal(result) + if e != nil { + log.Warn("Cannot convert result to json ", result) + } + w.Header().Set("Content-Type", "text/json; charset=utf-8") + apiV1AddTrackingCookie(w, r, ss) //always the last one to set cookies + fmt.Fprint(w, string(out)) +} diff --git a/apiV1login.go b/apiV1login.go index 507097d..bc71e29 100644 --- a/apiV1login.go +++ b/apiV1login.go @@ -43,7 +43,7 @@ func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { return } else { //Audit user login, in db - log.Info("successful login ", l.Login) + log.Info("login success", l.Login) } //format response diff --git a/apiV1logout.go b/apiV1logout.go new file mode 100644 index 0000000..66c4219 --- /dev/null +++ b/apiV1logout.go @@ -0,0 +1,20 @@ +package main + +import ( + "biukop.com/sfm/loan" + log "github.com/sirupsen/logrus" + "net/http" + "time" +) + +func apiV1Logout(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + res := apiV1ResponseBlank() + + ss.Expire = time.Now().Add(-10000) //make sure it expired + + ssEmpty := loan.Session{} + log.Info("Logout user ", ss.User, " from session ", ss.Id) + //send out + apiV1AddTrackingCookie(w, r, &ssEmpty) //always the last one to set cookies + res.sendJson(w) +} diff --git a/apiv1.go b/apiv1.go index 8b16c54..75d602d 100644 --- a/apiv1.go +++ b/apiv1.go @@ -26,14 +26,20 @@ type apiV1HandlerMap struct { var apiV1Handler = setupApiV1Handler() func setupApiV1Handler() []apiV1HandlerMap { - if config.Debug { + if config.Debug { //debug only return []apiV1HandlerMap{ {"POST", "login", apiV1Login}, + {"*", "logout", apiV1Logout}, + {"GET", "chart/type-of-loans", apiV1ChartTypeOfLoans}, + {"GET", "chart/amount-of-loans", apiV1ChartTypeOfLoans}, {"GET", "login", apiV1DumpRequest}, } - } else { + } else { //production return []apiV1HandlerMap{ {"POST", "login", apiV1Login}, + {"*", "logout", apiV1Logout}, + {"GET", "chart/type-of-loans", apiV1ChartTypeOfLoans}, + {"GET", "chart/amount-of-loans", apiV1ChartTypeOfLoans}, {"GET", "login", apiV1EmptyResponse}, } } diff --git a/chartTypeOfLoans.go b/chartTypeOfLoans.go new file mode 100644 index 0000000..9d314e8 --- /dev/null +++ b/chartTypeOfLoans.go @@ -0,0 +1,29 @@ +package main + +import ( + "biukop.com/sfm/loan" + "net/http" +) + +type chartTypeOfLoans struct { + Kind string `json:"kind"` + Share float64 `json:"share"` + Amount float64 `json:"amount"` +} + +func apiV1ChartTypeOfLoans(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + data := loan.TypeOfLoan() + //send out + apiV1SendJson(data, w, r, ss) +} + +func getDummyData() []chartTypeOfLoans { + return []chartTypeOfLoans{ + {Kind: "Solar2", Share: 0.052}, + {Kind: "Wind1", Share: 0.225}, + {Kind: "Other1", Share: 0.192}, + {Kind: "Hydroelectric1", Share: 0.175}, + {Kind: "Nuclear1", Share: 0.238}, + {Kind: "Coal1", Share: 0.118}, + } +}