| package main | |||||
| import ( | |||||
| "biukop.com/sfm/loan" | |||||
| "encoding/json" | |||||
| log "github.com/sirupsen/logrus" | |||||
| "net/http" | |||||
| ) | |||||
| func decodePayInFilter(r *http.Request) (ret loan.PayInListFilter, e error) { | |||||
| decoder := json.NewDecoder(r.Body) | |||||
| //decoder.DisallowUnknownFields() | |||||
| e = decoder.Decode(&ret) | |||||
| if e != nil { | |||||
| log.Error("failed decoding PayIn list filter", e.Error()) | |||||
| return | |||||
| } | |||||
| return | |||||
| } | |||||
| func apiV1PayInList(w http.ResponseWriter, r *http.Request, ss *loan.Session) { | |||||
| filter, e := decodePayInFilter(r) | |||||
| if e != nil { | |||||
| log.Error("invalid pay in list filter", filter, e) | |||||
| apiV1Client404Error(w, r, ss) | |||||
| return | |||||
| } | |||||
| ret, e := filter.RetrieveData() | |||||
| if e != nil { | |||||
| log.Error("failed to retrieve PayIn list", filter, e) | |||||
| apiV1Server500Error(w, r) | |||||
| return | |||||
| } | |||||
| apiV1SendJson(ret, w, r, ss) | |||||
| } |
| if e != nil { | if e != nil { | ||||
| return | return | ||||
| } | } | ||||
| //time.Sleep(5* time.Second); | |||||
| // if this is image itself, serve it directly | // if this is image itself, serve it directly | ||||
| if strings.Contains(strings.ToLower(ul.Upload.Format), "image") { | if strings.Contains(strings.ToLower(ul.Upload.Format), "image") { | ||||
| f, e := os.Open(ul.filePath()) | f, e := os.Open(ul.filePath()) | ||||
| if forceHttpDownload(r) { | if forceHttpDownload(r) { | ||||
| w.Header().Set("Content-Disposition", "attachment; filename="+ul.Upload.FileName) | w.Header().Set("Content-Disposition", "attachment; filename="+ul.Upload.FileName) | ||||
| } | } | ||||
| http.ServeContent(w, r, ul.filePath(), fi.ModTime(), f) | |||||
| http.ServeContent(w, r, ul.Upload.FileName, fi.ModTime(), f) | |||||
| return | return | ||||
| } | } | ||||
| } | } |
| {"GET", "people-list/", apiV1PeopleList}, | {"GET", "people-list/", apiV1PeopleList}, | ||||
| {"GET", "broker-list/", apiV1BrokerList}, | {"GET", "broker-list/", apiV1BrokerList}, | ||||
| {"POST", "sync-people/", apiV1SyncPeople}, | {"POST", "sync-people/", apiV1SyncPeople}, | ||||
| {"POST", "payIn/", apiV1PayInPost}, | {"POST", "payIn/", apiV1PayInPost}, | ||||
| {"DELETE", "payIn/", apiV1PayInDelete}, | {"DELETE", "payIn/", apiV1PayInDelete}, | ||||
| {"POST", "pay-in-list/", apiV1PayInList}, | |||||
| {"GET", "user-reward/", apiV1UserReward}, | {"GET", "user-reward/", apiV1UserReward}, | ||||
| {"GET", "login-available/", apiV1LoginAvailable}, | {"GET", "login-available/", apiV1LoginAvailable}, | ||||
| {"GET", "people-list", apiV1PeopleList}, | {"GET", "people-list", apiV1PeopleList}, | ||||
| {"GET", "broker-list/", apiV1BrokerList}, | {"GET", "broker-list/", apiV1BrokerList}, | ||||
| {"POST", "sync-people/", apiV1SyncPeople}, | {"POST", "sync-people/", apiV1SyncPeople}, | ||||
| {"POST", "payIn/", apiV1PayInPost}, | {"POST", "payIn/", apiV1PayInPost}, | ||||
| {"DELETE", "payIn/", apiV1PayInDelete}, | {"DELETE", "payIn/", apiV1PayInDelete}, | ||||
| {"POST", "pay-in-list/", apiV1PayInList}, | |||||
| {"GET", "user-reward/", apiV1UserReward}, | {"GET", "user-reward/", apiV1UserReward}, | ||||
| {"GET", "login-available/", apiV1LoginAvailable}, | {"GET", "login-available/", apiV1LoginAvailable}, | ||||
| <p id="socketOutPut"></p> | <p id="socketOutPut"></p> | ||||
| </div> | </div> | ||||
| <!--<script type="text/javascript">--> | |||||
| <!-- setTimeout(function(){--> | |||||
| <!-- location.reload();--> | |||||
| <!-- },1000)--> | |||||
| <!--</script>--> | |||||
| </body> | </body> | ||||
| m.AAA = make([]PayInAAAPeriod, 0, 10) | m.AAA = make([]PayInAAAPeriod, 0, 10) | ||||
| lines := strings.Split(raw, "\n") | lines := strings.Split(raw, "\n") | ||||
| currentDecoder := PayInAAAPeriod{} | |||||
| var currentDecoder *PayInAAAPeriod = nil | |||||
| state := "start" | state := "start" | ||||
| for _, l := range lines { // DFA, wow, finally it's used. after years of learning | for _, l := range lines { // DFA, wow, finally it's used. after years of learning | ||||
| switch state { | switch state { | ||||
| case "LookingForPeriod": | case "LookingForPeriod": | ||||
| state = currentDecoder.processPeriod(l) | state = currentDecoder.processPeriod(l) | ||||
| if state == "LookingForRows" { | if state == "LookingForRows" { | ||||
| currentDecoder.Period, e = currentDecoder.getPeriod(l) | |||||
| currentDecoder.Rows = make([]PayInAAARow, 0, 10) | |||||
| Period, e := currentDecoder.getPeriod(l) | |||||
| if e != nil { | if e != nil { | ||||
| log.Warn("cannot find period", l, e) | log.Warn("cannot find period", l, e) | ||||
| state = "LookingForPeriod" | state = "LookingForPeriod" | ||||
| } else { | } else { | ||||
| m.AAA = append(m.AAA, currentDecoder) | |||||
| m.AAA = append(m.AAA, PayInAAAPeriod{}) | |||||
| idx := len(m.AAA) - 1 | |||||
| currentDecoder = &(m.AAA[idx]) | |||||
| currentDecoder.Rows = make([]PayInAAARow, 0, 10) | |||||
| currentDecoder.Period = Period | |||||
| } | } | ||||
| } | } | ||||
| break | break | ||||
| case "LookingForRows", "LookingForRowsSkipCurrent": | case "LookingForRows", "LookingForRowsSkipCurrent": | ||||
| nextState, row, valid := currentDecoder.processRow(l) | nextState, row, valid := currentDecoder.processRow(l) | ||||
| if valid { | |||||
| if valid && currentDecoder != nil { | |||||
| currentDecoder.Rows = append(currentDecoder.Rows, row) | currentDecoder.Rows = append(currentDecoder.Rows, row) | ||||
| } | } | ||||
| state = nextState | state = nextState | ||||
| if nextState == "start" { | if nextState == "start" { | ||||
| currentDecoder = PayInAAAPeriod{} //renew to a empty state | |||||
| currentDecoder = nil //renew to a empty state | |||||
| } | } | ||||
| break | break | ||||
| } | } |