/
c_dict.go
49 lines (41 loc) · 987 Bytes
/
c_dict.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
package reading
import (
"fmt"
"net/http"
"os/exec"
"regexp"
"github.com/gin-gonic/gin"
)
const dictROOT = "tmp/reading/dict"
var dictExp = regexp.MustCompile(`^[\p{Han}\w]{1,32}$`)
func (p *Engine) getDict(c *gin.Context) {
if out, err := exec.
Command("sdcv", "--data-dir", dictROOT, "-l").
Output(); err == nil {
c.String(http.StatusOK, string(out))
} else {
c.String(http.StatusInternalServerError, err.Error())
}
}
//DictFm form for notice
type DictFm struct {
Keyword string `form:"keyword" binding:"required"`
}
func (p *Engine) postDict(c *gin.Context) {
var fm DictFm
err := c.Bind(&fm)
if err == nil {
if dictExp.MatchString(fm.Keyword) {
var out []byte
if out, err = exec.
Command("sdcv", "--data-dir", dictROOT, fm.Keyword).
Output(); err == nil {
c.String(http.StatusOK, string(out))
return
}
} else {
err = fmt.Errorf("bad keyword: %s", fm.Keyword)
}
}
c.String(http.StatusInternalServerError, err.Error())
}