-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
84 lines (76 loc) · 2.42 KB
/
server.py
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
# -*- coding: utf-8 -*-
from bottle import *
import json
from predict import predict
import os
import logging
logging.basicConfig(level=logging.WARNING,
filename=os.path.join(os.path.abspath('./'), 'log', 'log.txt'),
filemode='w',
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
# 跨域
@route('/<:re:.*>', method='OPTIONS')
def enable_cors_generic_route():
add_cors_headers()
@hook('after_request')
def enable_cors_after_request_hook():
"""
This executes after every route. We use it to attach CORS headers when applicable.
"""
add_cors_headers()
def add_cors_headers():
response.set_header('Access-Control-Allow-Origin', '*')
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
# summary
@route('/comments', method=['POST'])
def comment():
text = request.json['text']
arr = predict(text)
response.headers['Content-type'] = 'application/json'
res = {
'good': [],
'middle': [],
'bad': []
}
for i in arr:
for comment in i[2]:
if comment[0] == 0 and comment[1] is not None:
res['good'].append({
'type': i[0],
'comment': comment[1]
})
elif comment[0] == 1 and comment[1] is not None:
res['middle'].append({
'type': i[0],
'comment': comment[1]
})
elif comment[0] == 2 and comment[1] is not None:
res['bad'].append({
'type': i[0],
'comment': comment[1]
})
return json.dumps(res)
# 静态文件
@route('/static/img/<filename>')
def send_image(filename):
return static_file(filename, root='./dist/static/img/')
@route('/static/css/<filename>')
def send_css(filename):
return static_file(filename, root='./dist/static/css/')
@route('/static/js/<filename>')
def send_js(filename):
return static_file(filename, root='./dist/static/js/')
@route('/static/<filename>')
def send_urlconfig(filename):
return static_file(filename, root='./dist/static/')
@route('/static/fonts/<filename>')
def send_fonts(filename):
return static_file(filename, root='./dist/static/fonts/')
@route('/favicon.ico')
def send_ico():
return static_file('favicon.ico', root='./')
@route('/')
def index():
return template('./dist/index.html')
run(host='0.0.0.0', port=1234, debug=True)