forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocr_http_status_handler.go
56 lines (48 loc) · 1.66 KB
/
ocr_http_status_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ocrworker
import (
"encoding/json"
"net/http"
"github.com/rs/zerolog/log"
)
type OcrHttpStatusHandler struct{}
func NewOcrHttpStatusHandler() *OcrHttpStatusHandler {
return &OcrHttpStatusHandler{}
}
func (*OcrHttpStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
log.Debug().Str("component", "OCR_STATUS").Msg("OcrHttpStatusHandler called")
ocrRequest := OcrRequest{}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&ocrRequest)
if err != nil {
log.Error().Err(err).Str("component", "OCR_STATUS")
http.Error(w, "unable to unmarshal json", 400)
return
}
ocrResult, ocrRequestExists := CheckOcrStatusByID(ocrRequest.ImgUrl)
if !ocrRequestExists {
ocrResult.Text = ""
ocrResult.ID = ocrRequest.ImgUrl
ocrResult.Status = "not found"
log.Info().Str("component", "OCR_STATUS").Str("RequestID", ocrRequest.ImgUrl).
Str("RemoteAddr", req.RemoteAddr).
Msg("no such ocr request, processing time limit was probably reached for this request")
}
w.Header().Set("Content-Type", "application/json")
js, err := json.Marshal(ocrResult)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error().Err(err).Str("component", "OCR_STATUS").Str("RequestID", ocrRequest.ImgUrl).Str("RemoteAddr", req.RemoteAddr)
return
}
_, err = w.Write(js)
if err != nil {
log.Error().Err(err).Str("component", "OCR_STATUS").Str("RequestID", ocrRequest.ImgUrl).Str("RemoteAddr", req.RemoteAddr)
}
if ocrRequestExists && err == nil {
log.Info().Str("component", "OCR_STATUS").
Str("RequestID", ocrRequest.ImgUrl).
Str("RemoteAddr", req.RemoteAddr).
Msg("ocr request was claimed")
}
_ = req.Body.Close()
}