package main import ( "biukop.com/sfm/loan" log "github.com/sirupsen/logrus" "net/http" "strconv" "sync" "time" ) var analysisMutex sync.Mutex // make sure only one analysis run at a time. func apiV1UploadAnalysis(w http.ResponseWriter, r *http.Request, ss *loan.Session) { strId := r.URL.Path[len(apiV1Prefix+"upload-analysis/"):] //remove prefix Id, e := strconv.Atoi(strId) if e != nil { log.Error("Invalid uploads Id cannot convert to integer", Id, e) apiV1Client403Error(w, r, ss) return } ul := loan.Uploads{} e = ul.Read(int64(Id)) if e != nil { log.Error("Upload not found or read error from db", Id, e) apiV1Client404Error(w, r, ss) return } analysisMutex.Lock() ai := AiDecodeIncome{} e = ai.decodeUploadToPayIn(ul, true) analysisMutex.Unlock() if e != nil { log.Error("Invalid uploads Id cannot convert to integer", Id, e) apiV1Server500Error(w, r) return } apiV1SendJson(ai, w, r, ss) } func apiV1UploadCreateAnalysis(w http.ResponseWriter, r *http.Request, ss *loan.Session) { time.Sleep(1 * time.Second) strId := r.URL.Path[len(apiV1Prefix+"upload-analysis/"):] //remove prefix Id, e := strconv.Atoi(strId) if e != nil { log.Error("Invalid uploads Id cannot convert to integer", Id, e) apiV1Client403Error(w, r, ss) return } ul := loan.Uploads{} e = ul.Read(int64(Id)) if e != nil { log.Error("Upload not found or read error from db", Id, e) apiV1Client404Error(w, r, ss) return } ai := AiDecodeIncome{} e = ai.decodeUploadToPayIn(ul, false) if e != nil { log.Error("Cannot decode upload", Id, e) apiV1Server500Error(w, r) return } apiV1SendJson(ai, w, r, ss) }