Skip to content

Commit

Permalink
Format code with gofmt and gofumpt (#52)
Browse files Browse the repository at this point in the history
This commit fixes the style issues introduced in a0ec5c1 according to the output
from gofmt and gofumpt.

Details: https://deepsource.io/gh/xf0e/open-ocr/transform/d2bea675-6ef1-45fd-9014-78b413628865/

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
deepsource-autofix[bot] authored Aug 4, 2022
1 parent a0ec5c1 commit d4a4f7d
Show file tree
Hide file tree
Showing 31 changed files with 36 additions and 151 deletions.
5 changes: 2 additions & 3 deletions cli-httpd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func makeHTTPServer(rabbitConfig *ocrworker.RabbitConfig, ocrChain http.Handler)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

return makeServerFromMux(mux)

}

func main() {
Expand Down Expand Up @@ -183,7 +182,7 @@ func main() {
if certFile == "" || keyFile == "" {
log.Fatal().Msg("usehttps flag only makes sense if both the private key and a certificate are available")
}
var httpsSrv = makeHTTPServer(&rabbitConfig, ocrChain)
httpsSrv := makeHTTPServer(&rabbitConfig, ocrChain)
httpsSrv.Addr = listenAddr

// crypto settings
Expand All @@ -204,7 +203,7 @@ func main() {
log.Fatal().Err(err).Str("component", "CLI_HTTP").Caller().Msg("cli_https has failed to start")
}
} else {
var httpSrv = makeHTTPServer(&rabbitConfig, ocrChain)
httpSrv := makeHTTPServer(&rabbitConfig, ocrChain)
httpSrv.Addr = listenAddr
if err := httpSrv.ListenAndServe(); err != nil {
log.Fatal().Err(err).Str("component", "CLI_HTTP").Caller().Msg("cli_http has failed to start")
Expand Down
6 changes: 2 additions & 4 deletions cli-preprocessor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"flag"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"time"

"github.com/xf0e/open-ocr"
)
Expand All @@ -17,7 +18,6 @@ func init() {
}

func main() {

var preprocessor string
flagFunc := func() {
flag.StringVar(
Expand All @@ -26,7 +26,6 @@ func main() {
"identity",
"The preprocessor to use, eg, stroke-width-transform",
)

}

rabbitConfig := ocrworker.DefaultConfigFlagsOverride(flagFunc)
Expand All @@ -51,5 +50,4 @@ func main() {
err = <-preprocessorWorker.Done
log.Error().Err(err).Str("component", "MAIN_PREPROSSOR").Msg("preprocessor worker failed")
}

}
1 change: 0 additions & 1 deletion cli-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ func main() {
Str("component", "OCR_WORKER").Err(err).
Msg("OCR Worker failed with error")
}

}
5 changes: 1 addition & 4 deletions convert-pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import (
"github.com/rs/zerolog/log"
)

type ConvertPdf struct {
}
type ConvertPdf struct{}

func (ConvertPdf) preprocess(ocrRequest *OcrRequest) error {

tmpFileNameInput, err := createTempFileName("")
tmpFileNameInput = fmt.Sprintf("%s.pdf", tmpFileNameInput)
if err != nil {
Expand Down Expand Up @@ -72,7 +70,6 @@ func (ConvertPdf) preprocess(ocrRequest *OcrRequest) error {

// read bytes from output file
resultBytes, err := os.ReadFile(tmpFileNameOutput)

if err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions generate_landing_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

func randomMOTD() string {

names := [...]string{
`<p>Nice day to put slinkies on an escalator!</p>
<pre>__ ; '.' :
Expand Down Expand Up @@ -198,5 +197,4 @@ pre {
</section></body> </html>`

return head + buttons + middle + MOTD + tail

}
3 changes: 1 addition & 2 deletions mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package ocrworker

const MockEngineResponse = "mock engine decoder response"

type MockEngine struct {
}
type MockEngine struct{}

// ProcessRequest will process incoming OCR request by routing it through the whole process chain
func (m MockEngine) ProcessRequest(ocrRequest *OcrRequest, workerConfig *WorkerConfig) (OcrResult, error) {
Expand Down
2 changes: 0 additions & 2 deletions ocr_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (e OcrEngineType) String() string {
}

func (e *OcrEngineType) UnmarshalJSON(b []byte) (err error) {

var engineTypeStr string

if err := json.Unmarshal(b, &engineTypeStr); err == nil {
Expand Down Expand Up @@ -78,5 +77,4 @@ func (e *OcrEngineType) UnmarshalJSON(b []byte) (err error) {
} else {
return err
}

}
6 changes: 2 additions & 4 deletions ocr_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ package ocrworker

import (
"encoding/json"
"github.com/rs/zerolog/log"
"testing"

"github.com/rs/zerolog/log"

"github.com/couchbaselabs/go.assert"
)

func TestOcrEngineTypeJson(t *testing.T) {

testJson := `{"img_url":"foo", "engine":"tesseract"}`
ocrRequest := OcrRequest{}
err := json.Unmarshal([]byte(testJson), &ocrRequest)
if err != nil {
log.Error().Str("component", "TEST").Err(err)

}
assert.True(t, err == nil)
assert.Equals(t, ocrRequest.EngineType, EngineTesseract)
log.Error().Str("component", "TEST").Interface("ocrRequest", ocrRequest)

}
9 changes: 3 additions & 6 deletions ocr_http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (

func (s *OcrHTTPStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// _ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
var requestIDRaw = ksuid.New()
requestIDRaw := ksuid.New()
requestID := requestIDRaw.String()
log.Info().Str("component", "OCR_HTTP").Str("RequestID", requestID).
Msg("serveHttp called")
Expand All @@ -45,7 +45,7 @@ func (s *OcrHTTPStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Reques
log.Warn().Err(err).Caller().Str("component", "OCR_HTTP").Msg(req.RequestURI + " request Body could not be removed")
}
}(req.Body)
var httpStatus = 200
httpStatus := 200

ServiceCanAcceptMu.Lock()
serviceCanAcceptLocal := ServiceCanAccept
Expand Down Expand Up @@ -84,7 +84,6 @@ func (s *OcrHTTPStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Reques
}

ocrResult, httpStatus, err := HandleOcrRequest(&ocrRequest, &s.RabbitConfig)

if err != nil {
msg := "Unable to perform OCR decode. Error: %v"
errMsg := fmt.Sprintf(msg, err)
Expand All @@ -108,7 +107,7 @@ func (s *OcrHTTPStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Reques

// HandleOcrRequest will process incoming OCR request by routing it through the whole process chain
func HandleOcrRequest(ocrRequest *OcrRequest, workerConfig *RabbitConfig) (OcrResult, int, error) {
var httpStatus = 200
httpStatus := 200
ocrResult := newOcrResult(ocrRequest.RequestID)
// set the context for zerolog, RequestID will be printed on each logging event
logger := zerolog.New(os.Stdout).With().
Expand All @@ -120,7 +119,6 @@ func HandleOcrRequest(ocrRequest *OcrRequest, workerConfig *RabbitConfig) (OcrRe

workingConfig := WorkerConfig{}
ocrResult, err := ocrEngine.ProcessRequest(ocrRequest, &workingConfig)

if err != nil {
logger.Error().Err(err).Str("component", "OCR_HTTP").Msg("Error processing ocr request")
httpStatus = 500
Expand All @@ -145,5 +143,4 @@ func HandleOcrRequest(ocrRequest *OcrRequest, workerConfig *RabbitConfig) (OcrRe

return ocrResult, httpStatus, nil
}

}
3 changes: 0 additions & 3 deletions ocr_http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

// This test assumes that rabbit mq is running
func DisabledTestOcrHttpHandlerIntegration(t *testing.T) {

rabbitConfig := rabbitConfigForTests()
workerConfig := workerConfigForTests()

Expand Down Expand Up @@ -59,7 +58,6 @@ func DisabledTestOcrHttpHandlerIntegration(t *testing.T) {
}

func spawnOcrWorker(workerConfig *WorkerConfig) error {

// kick off a worker
// this would normally happen on a different machine ..
ocrWorker, err := NewOcrRpcWorker(workerConfig)
Expand All @@ -68,5 +66,4 @@ func spawnOcrWorker(workerConfig *WorkerConfig) error {
}
ocrWorker.Run()
return nil

}
7 changes: 1 addition & 6 deletions ocr_http_multipart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func NewOcrHttpMultipartHandler(r *RabbitConfig) *OcrHttpMultipartHandler {
}

func (*OcrHttpMultipartHandler) extractParts(req *http.Request) (OcrRequest, error) {

log.Info().Str("component", "OCR_HTTP").Msg("request to ocr-file-upload")
ocrReq := OcrRequest{}

Expand Down Expand Up @@ -87,18 +86,16 @@ func (*OcrHttpMultipartHandler) extractParts(req *http.Request) (OcrRequest, err
default:
return ocrReq, fmt.Errorf("this endpoint only accepts POST requests")
}

}

func (s *OcrHttpMultipartHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Warn().Err(err).Caller().Str("component", "OCR_HTTP").Msg(req.RequestURI + " request Body could not be removed")
}
}(req.Body)
var httpStatus = 200
httpStatus := 200
ocrRequest, err := s.extractParts(req)
if err != nil {
log.Error().Err(err).Str("component", "OCR_HTTP")
Expand All @@ -108,7 +105,6 @@ func (s *OcrHttpMultipartHandler) ServeHTTP(w http.ResponseWriter, req *http.Req
}

ocrResult, httpStatus, err := HandleOcrRequest(&ocrRequest, &s.RabbitConfig)

if err != nil {
msg := "Unable to perform OCR decode."
log.Error().Err(err).Str("component", "OCR_HTTP").Msg(msg)
Expand All @@ -117,5 +113,4 @@ func (s *OcrHttpMultipartHandler) ServeHTTP(w http.ResponseWriter, req *http.Req
}

_, _ = fmt.Fprintf(w, ocrResult.Text)

}
4 changes: 1 addition & 3 deletions ocr_http_status_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
"github.com/rs/zerolog/log"
)

type OcrHttpStatusHandler struct {
}
type OcrHttpStatusHandler struct{}

func NewOcrHttpStatusHandler() *OcrHttpStatusHandler {
return &OcrHttpStatusHandler{}
}

func (s *OcrHttpStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

log.Debug().Str("component", "OCR_STATUS").Msg("OcrHttpStatusHandler called")

ocrRequest := OcrRequest{}
Expand Down
3 changes: 1 addition & 2 deletions ocr_postback_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (

var postTimeout = 50 * time.Second

type ocrPostClient struct {
}
type ocrPostClient struct{}

func newOcrPostClient() *ocrPostClient {
return &ocrPostClient{}
Expand Down
9 changes: 4 additions & 5 deletions ocr_request.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ocrworker

import "fmt"
import "encoding/base64"
import (
"encoding/base64"
"fmt"
)

type OcrRequest struct {
ImgUrl string `json:"img_url"`
Expand Down Expand Up @@ -38,7 +40,6 @@ func (ocrRequest *OcrRequest) nextPreprocessor(processorRoutingKey string) strin
}

func (ocrRequest *OcrRequest) decodeBase64() error {

bytes, decodeError := base64.StdEncoding.DecodeString(ocrRequest.ImgBase64)

if decodeError != nil {
Expand All @@ -52,12 +53,10 @@ func (ocrRequest *OcrRequest) decodeBase64() error {
}

func (ocrRequest *OcrRequest) hasBase64() bool {

return ocrRequest.ImgBase64 != ""
}

func (ocrRequest *OcrRequest) downloadImgUrl() error {

bytes, err := url2bytes(ocrRequest.ImgUrl)
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions ocr_res_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var (

// CheckForAcceptRequest will check by reading the RabbitMQ API if resources for incoming request are available
func CheckForAcceptRequest(urlQueue, urlStat string, statusChanged bool) bool {

isAvailable := false
TechnicalErrorResManager = false
jsonQueueStat, err := url2bytes(urlQueue)
Expand Down Expand Up @@ -147,8 +146,8 @@ func SetResManagerState(ampqAPIConfig *RabbitConfig) {
urlStat := ampqAPIConfig.AmqpAPIURI + ampqAPIConfig.APIPathStats
factorForMessageAccept = ampqAPIConfig.FactorForMessageAccept

var boolCurValue = false
var boolOldValue = true
boolCurValue := false
boolOldValue := true
Loop:
for {
if AppStop {
Expand Down
4 changes: 0 additions & 4 deletions ocr_results_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var (

// CheckOcrStatusByID checks status of an ocr request based on origin of request
func CheckOcrStatusByID(requestID string) (OcrResult, bool) {

if _, ok := RequestsTrack.Load(requestID); !ok {
// log.Info().Str("component", "OCR_CLIENT").Str("RequestID", requestID).Msg("no such request found in the queue")
return OcrResult{}, false
Expand Down Expand Up @@ -41,19 +40,16 @@ func CheckOcrStatusByID(requestID string) (OcrResult, bool) {
}

func getQueueLen() uint {

return uint(atomic.LoadUint32(&RequestTrackLength))
}

func deleteRequestFromQueue(requestID string) {

inFlightGauge.Dec()
atomic.AddUint32(&RequestTrackLength, ^uint32(0))
RequestsTrack.Delete(requestID)
}

func addNewOcrResultToQueue(requestID string, rpcResponseChan chan OcrResult) {

inFlightGauge.Inc()
atomic.AddUint32(&RequestTrackLength, 1)
RequestsTrack.Store(requestID, rpcResponseChan)
Expand Down
Loading

0 comments on commit d4a4f7d

Please sign in to comment.