-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (166 loc) · 6 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"github.com/wuyangdut/godetector/common"
"github.com/wuyangdut/godetector/service"
)
var config *common.WebConfig
var parsedTemplate *template.Template
var adminTemplate *template.Template
var filterHandler *service.FilterHandler
var serviceHandler *service.ServiceHandler
var conf = flag.String("f", "./conf.yaml", "Input Your Config Yaml Path")
var testTsAddr = flag.String("addr", "http://127.0.0.1:8080", "Input The TorchServe Address For Test")
func TestHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
result := service.TsResponse{IsIllegal: false}
err := parsedTemplate.Execute(w, result)
if err != nil {
log.Println("Error executing template :", err)
}
}
func AdminHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
err := adminTemplate.Execute(w, nil)
if err != nil {
log.Println("Error executing template :", err)
}
}
// 翻译:curl http://127.0.0.1:8080/predictions/nsfw_model -T ./test/test_img/porn.jpg
func NsfwImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
data := common.ParseFileBytes(r)
result := service.NsfwImageDetect(*testTsAddr, data)
err := parsedTemplate.Execute(w, result.MarshalIndent())
if err != nil {
log.Println("Error executing template :", err)
}
}
//翻译; curl http://127.0.0.1:8080/predictions/protest_model -T ./test/test_img/protest_sign.jpg
func ProtestImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
data := common.ParseFileBytes(r)
result := service.ProtestImageDetect(*testTsAddr, data)
err := parsedTemplate.Execute(w, result.MarshalIndent())
if err != nil {
log.Println("Error executing template :", err)
}
}
func FilterTextHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
result := filterHandler.FilterTextCheat(r.PostFormValue("text"))
err := parsedTemplate.Execute(w, result.MarshalIndent())
if err != nil {
log.Println("Error executing template :", err)
}
}
// curl -X POST http://127.0.0.1:8080/predictions/cn_text_model -T ./test/cn_text_test.txt
func ChineseTextHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
result := service.ChineseTextDetect(*testTsAddr, r.PostFormValue("text"))
err := parsedTemplate.Execute(w, result.MarshalIndent())
if err != nil {
log.Println("Error executing template :", err)
}
}
// curl -X POST http://127.0.0.1:8080/predictions/en_text_model -T ./test/en_text_test.txt
func EnglishTextHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
result := service.EnglishTextDetect(*testTsAddr, r.PostFormValue("text"))
err := parsedTemplate.Execute(w, result.MarshalIndent())
if err != nil {
log.Println("Error executing template :", err)
}
}
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
result := service.CheckHealth(*testTsAddr)
_, err := fmt.Fprint(w, result)
if err != nil {
log.Println("Error checking health :", err)
}
}
func NsqImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
image := common.ParseFileBytes(r)
serviceResponse := serviceHandler.NsqImageSend(image)
resBody := serviceResponse.MarshalIndent()
fmt.Fprint(w, resBody)
}
func NsqTextHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
text := r.PostFormValue("text")
serviceResponse := serviceHandler.NsqTextSend(text)
resBody := serviceResponse.MarshalIndent()
fmt.Fprint(w, resBody)
}
func StrategyHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "405", http.StatusMethodNotAllowed)
}
raw := make(map[string]string)
raw["nsfw"] = r.PostFormValue("nsfw")
raw["protest"] = r.PostFormValue("protest")
raw["cntext"] = r.PostFormValue("cntext")
raw["entext"] = r.PostFormValue("entext")
raw["numtext"] = r.PostFormValue("numtext")
serviceHandler.MathHandler = service.NewMathAnalyzer(raw)
_, err := fmt.Fprint(w, "Updating strategy is finished.")
if err != nil {
log.Println("Error updating strategy :", err)
}
}
func main() {
flag.Parse()
fmt.Println("hello, godetector web server!")
config = common.GetWebConfig(*conf)
//监听Http请求,用于管理员测试本地推理端后台
http.HandleFunc("/index/test", TestHandler) //测试主页
http.HandleFunc("/image/nsfw", NsfwImageHandler)
http.HandleFunc("/image/protest", ProtestImageHandler)
http.HandleFunc("/text/filter", FilterTextHandler)
http.HandleFunc("/text/cn", ChineseTextHandler)
http.HandleFunc("/text/en", EnglishTextHandler)
http.HandleFunc("/test/health", HealthCheckHandler)
//监听Http请求,用于管理员测试违规图片检测、违规文本检测、自定义阻止策略
http.HandleFunc("/index/admin", AdminHandler) //Admin主页
http.HandleFunc("/nsq/image", NsqImageHandler)
http.HandleFunc("/nsq/text", NsqTextHandler)
http.HandleFunc("/admin/strategy", StrategyHandler)
parsedTemplate, _ = template.ParseFiles("./template/test.html")
adminTemplate, _ = template.ParseFiles("./template/admin.html")
filterHandler = service.NewFilterHandler()
serviceHandler = service.NewServiceHandler(config.NsqdAddr, config.EtcdAddr, config.Strategy)
defer serviceHandler.Stop()
fmt.Printf("admin index serve on: http://%s/index/admin\n", config.WebAddr)
fmt.Printf("test index serve on: http://%s/index/test\n", config.WebAddr)
err := http.ListenAndServe(config.WebAddr, nil)
if err != nil {
log.Fatal("Error Starting the HTTP Server :", err)
return
}
}