| "biukop.com/sfm/loan" | "biukop.com/sfm/loan" | ||||
| "encoding/json" | "encoding/json" | ||||
| "fmt" | "fmt" | ||||
| log "github.com/sirupsen/logrus" | |||||
| "net/http" | "net/http" | ||||
| "time" | "time" | ||||
| ) | ) | ||||
| fmt.Fprint(w, string(ret)) | fmt.Fprint(w, string(ret)) | ||||
| return | 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)) | |||||
| } |
| return | return | ||||
| } else { | } else { | ||||
| //Audit user login, in db | //Audit user login, in db | ||||
| log.Info("successful login ", l.Login) | |||||
| log.Info("login success", l.Login) | |||||
| } | } | ||||
| //format response | //format response |
| 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) | |||||
| } |
| var apiV1Handler = setupApiV1Handler() | var apiV1Handler = setupApiV1Handler() | ||||
| func setupApiV1Handler() []apiV1HandlerMap { | func setupApiV1Handler() []apiV1HandlerMap { | ||||
| if config.Debug { | |||||
| if config.Debug { //debug only | |||||
| return []apiV1HandlerMap{ | return []apiV1HandlerMap{ | ||||
| {"POST", "login", apiV1Login}, | {"POST", "login", apiV1Login}, | ||||
| {"*", "logout", apiV1Logout}, | |||||
| {"GET", "chart/type-of-loans", apiV1ChartTypeOfLoans}, | |||||
| {"GET", "chart/amount-of-loans", apiV1ChartTypeOfLoans}, | |||||
| {"GET", "login", apiV1DumpRequest}, | {"GET", "login", apiV1DumpRequest}, | ||||
| } | } | ||||
| } else { | |||||
| } else { //production | |||||
| return []apiV1HandlerMap{ | return []apiV1HandlerMap{ | ||||
| {"POST", "login", apiV1Login}, | {"POST", "login", apiV1Login}, | ||||
| {"*", "logout", apiV1Logout}, | |||||
| {"GET", "chart/type-of-loans", apiV1ChartTypeOfLoans}, | |||||
| {"GET", "chart/amount-of-loans", apiV1ChartTypeOfLoans}, | |||||
| {"GET", "login", apiV1EmptyResponse}, | {"GET", "login", apiV1EmptyResponse}, | ||||
| } | } | ||||
| } | } |
| 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}, | |||||
| } | |||||
| } |