| @@ -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)) | |||
| } | |||
| @@ -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 | |||
| @@ -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) | |||
| } | |||
| @@ -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}, | |||
| } | |||
| } | |||
| @@ -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}, | |||
| } | |||
| } | |||