-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
75 lines (62 loc) · 2.44 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
from flask import Flask, request, jsonify
from flask import send_file
from flask_restful import reqparse, abort, Api, Resource
from generator import generate_new_tombstone
import os
import re
app = Flask(__name__)
api = Api(app)
url = string(os.getenv('server_url', "http://127.0.0.1"))
parser = reqparse.RequestParser()
parser.add_argument('name', type=str, required=True)
parser.add_argument('inscription', type=str, required=True)
class GenerateTombstone(Resource):
def post(self):
"""
Generate and return the png tombstone file
"""
args = parser.parse_args()
out_file_name = generate_new_tombstone(args.name, args.inscription)
return send_file("created_tombstones/{}.png".format(out_file_name), mimetype='image/png')
class Tombstone(Resource):
def get(self, tomb_name):
"""
Return a previously generated tombstone
"""
file_name = "/site/OTTGaaS/created_tombstones/{}.png".format(tomb_name)
if os.path.isfile(file_name):
return send_file(file_name, mimetype='image/png')
else:
return "Unable to find tombstone {}".format(tomb_name), 404
class SlackTombstone(Resource):
def post(self):
"""
Accepts a request from a slack slash command to create and return a tombstone
"""
print request.form
question_match = re.search("name (.+) inscription (.+)", request.form["text"])
if question_match:
name = question_match.group(1)
inscription = question_match.group(2)
out_file_name = generate_new_tombstone(name, inscription)
else:
return "Malformed Request. Your text needs `name` and `inscription` in it."
data = {
"text": "Here's your tombstone",
"response_type": "in_channel",
"attachments": [
{
"Title:": "Your Tombstone",
"fallback": "Something broke",
"image_url": "{}/tombstone/{}".format(url, out_file_name)
}
]
}
return jsonify(**data)
api.add_resource(GenerateTombstone, '/generate_tombstone')
api.add_resource(SlackTombstone, '/slack_generate_tombstone')
api.add_resource(Tombstone, '/tombstone/<string:tomb_name>')
if __name__ == '__main__':
# Support Bluemix CF app port
port = int(os.getenv('VCAP_APP_PORT', 5000))
app.run(host="0.0.0.0", port=port, debug=True)