-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
103 lines (96 loc) · 3.38 KB
/
api.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"strings"
"regexp"
"github.com/kataras/iris"
"github.com/legion-zver/shield"
)
var rxStripTags = regexp.MustCompile("<[a-zA-Z/][^>]*>")
// LearnRequest - learn request
type LearnRequest struct {
Class string `form:"class" json:"class"`
Text string `form:"text" json:"text"`
}
// Learn - обучение
func Learn(c *iris.Context) {
var sh shield.Shield
if c.Get("shield") != nil {
sh = c.Get("shield").(shield.Shield)
}
if sh != nil {
var request LearnRequest
err := c.ReadJSON(&request)
if err != nil {
err = c.ReadForm(&request)
if err != nil {
c.JSON(iris.StatusBadRequest, iris.Map{"error":NewAPIError(400,[]APIErrorCause{APIErrorCause{Target:"request",Сause:err.Error()}})})
return
}
}
request.Class = strings.TrimSpace(strings.ToLower(request.Class))
request.Text = strings.TrimSpace(rxStripTags.ReplaceAllString(request.Text, " "))
if len(request.Text) > 0 {
err = sh.Learn(request.Class, request.Text)
if err != nil {
c.JSON(iris.StatusInternalServerError, iris.Map{"error":NewAPIError(500,[]APIErrorCause{APIErrorCause{Target:"learn",Сause:err.Error()}})})
return
}
// ok
c.JSON(iris.StatusOK, iris.Map{"result":"success"})
return
}
c.JSON(iris.StatusBadRequest, iris.Map{"error":NewAPIError(400,[]APIErrorCause{APIErrorCause{Target:"request.text",Сause:"Empty"}})})
return
}
c.JSON(iris.StatusServiceUnavailable, iris.Map{"error":NewSimpleAPIError(503)})
}
// ScoreRequest - score request
type ScoreRequest struct {
Text string `form:"text" json:"text"`
}
// Score - обучение
func Score(c *iris.Context) {
var sh shield.Shield
if c.Get("shield") != nil {
sh = c.Get("shield").(shield.Shield)
}
if sh != nil {
text := strings.TrimSpace(c.URLParam("text"))
if len(text) < 1 {
var request ScoreRequest
err := c.ReadJSON(&request)
if err != nil {
err = c.ReadForm(&request)
if err != nil {
c.JSON(iris.StatusBadRequest, iris.Map{"error":NewAPIError(400,[]APIErrorCause{APIErrorCause{Target:"request",Сause:err.Error()}})})
return
}
}
text = strings.TrimSpace(request.Text)
}
if len(text) > 0 {
m, err := sh.Score(text)
if err != nil {
c.JSON(iris.StatusInternalServerError, iris.Map{"error":NewAPIError(500,[]APIErrorCause{APIErrorCause{Target:"learn",Сause:err.Error()}})})
return
}
// ok
c.JSON(iris.StatusOK, iris.Map{"result":m})
return
}
c.JSON(iris.StatusBadRequest, iris.Map{"error":NewAPIError(400,[]APIErrorCause{APIErrorCause{Target:"request.text",Сause:"Empty"}})})
return
}
c.JSON(iris.StatusServiceUnavailable, iris.Map{"error":NewSimpleAPIError(503)})
}
// InitAPI - init API
func InitAPI(api iris.MuxAPI) {
if api != nil {
// Learn
api.Get("/learn", Learn)
api.Post("/learn", Learn)
// Score
api.Get("/score", Score)
api.Post("/score", Score)
}
}