-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (69 loc) · 1.4 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/otiai10/gosseract"
)
func getText(image []byte) string {
client := gosseract.NewClient()
defer client.Close()
client.SetImageFromBytes(image)
client.Languages = []string{"eng", "fra"}
text, _ := client.Text()
return text
}
func getVersion() string {
versionFile := "version.txt"
version := "dev"
if _, err := os.Stat(versionFile); err == nil {
b, err := ioutil.ReadFile(versionFile)
if err != nil {
fmt.Print(err)
}
version = string(b)
version = strings.TrimSuffix(version, "\n")
}
return version
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
r := gin.Default()
host := flag.String("h", "0.0.0.0", "Set port for listening")
port := flag.Int("p", 8000, "Set port for listening")
flag.Parse()
r.POST("/text", func(c *gin.Context) {
file, _, err := c.Request.FormFile("file")
check(err)
defer file.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
check(err)
return
}
text := getText(buf.Bytes())
c.JSON(200, gin.H{
"status": "success",
"text": text,
})
})
r.GET("/version", func(c *gin.Context) {
version := getVersion()
c.JSON(200, gin.H{
"status": "success",
"version": version,
})
})
bindString := *host + ":" + strconv.Itoa(*port)
r.Run(bindString)
}