forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr_http_handler.go
55 lines (42 loc) · 1.04 KB
/
ocr_http_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
package ocrworker
import (
"encoding/json"
"fmt"
"net/http"
"github.com/couchbaselabs/logg"
)
type OcrHttpHandler struct {
RabbitConfig RabbitConfig
}
func NewOcrHttpHandler(r RabbitConfig) *OcrHttpHandler {
return &OcrHttpHandler{
RabbitConfig: r,
}
}
func (s *OcrHttpHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
logg.LogTo("OCR_HTTP", "serveHttp called")
defer req.Body.Close()
ocrReq := OcrRequest{}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&ocrReq)
if err != nil {
logg.LogError(err)
http.Error(w, "Unable to unmarshal json", 500)
return
}
ocrClient, err := NewOcrRpcClient(s.RabbitConfig)
if err != nil {
logg.LogError(err)
http.Error(w, "Unable to create rpc client", 500)
return
}
decodeResult, err := ocrClient.DecodeImage(ocrReq)
if err != nil {
logg.LogError(err)
http.Error(w, "Unable to perform OCR decode", 500)
return
}
logg.LogTo("OCR_HTTP", "decodeResult: %v", decodeResult)
logg.LogTo("OCR_HTTP", "ocrReq: %v", ocrReq)
fmt.Fprintf(w, decodeResult.Text)
}