-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (33 loc) · 1.2 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
#!/usr/bin/env python
# coding: utf-8
from flask import Flask, request, jsonify
import logbook
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from extract import fetch_maori_translation
def apply_logging():
from os.path import abspath, exists, dirname, join
server_log_file = join(dirname(abspath(__file__)), "maori.log")
if not exists(server_log_file):
open(server_log_file, "w").close()
logbook.set_datetime_format("local")
local_log = logbook.FileHandler(server_log_file)
local_log.format_string = (
u'[{record.time:%Y-%m-%d %H:%M:%S}] '
u'lineno:{record.lineno} '
u'{record.level_name}:{record.message}')
local_log.push_application()
sentry_sdk.init(
dsn="https://[email protected]/5197511",
integrations=[FlaskIntegration()]
)
apply_logging()
app = Flask(__name__)
@app.route("/", methods=['GET'])
def translate():
text = request.args.get('text')
ip = request.headers.get("X-Real-IP", "")
logbook.info(" {} {}".format(ip, text.encode("utf-8")))
return jsonify(fetch_maori_translation(text))
if __name__ == "__main__":
app.run(host="127.0.0.1", port="8004")