-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
90 lines (69 loc) · 2.35 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
85
86
87
88
89
90
import os
from flask import Flask, request, jsonify, render_template, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import DateTime, Column, String, Integer
from sqlalchemy.sql import func
from service.util import DB_URL
app = Flask(__name__)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = DB_URL or "sqlite:////app/database/db"
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 30
db = SQLAlchemy(app)
class Message(db.Model):
__tablename__ = "messages"
id = Column(Integer, primary_key=True)
message = Column(String, nullable=False)
author = Column(String, nullable=True)
created = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
@app.route("/favicon.ico")
def favicon():
return send_from_directory(
os.path.join(app.root_path, "static"),
"favicon.ico",
mimetype="image/vnd.microsoft.icon",
)
@app.route("/")
def home():
return render_template("index.html")
# TODO: In the future, we could set up "persistent" as a variable
# so that we match url_for in the template with the route here.
# For now: We know this is correct, and we can limit it to just
# the one path that we care about.
@app.route("/persistent/display.jpg")
def persistent_jpg():
return send_from_directory(
os.path.join(app.root_path, "persistent"),
"display.jpg",
mimetype="image/jpeg",
)
@app.route("/matrix/api/message", methods=["POST"])
def create_message():
data = request.get_json()
if "message" not in data:
return jsonify({"error": "bad payload"}), 400
message = data.get("message")
author = data.get("author")
msg = Message(message=message, author=author)
db.session.add(msg)
db.session.commit()
db.session.flush()
return (
jsonify(dict(message=message, author=author)),
200,
)
@app.route("/matrix/api/message", methods=["GET"])
def get_messages():
messages = Message.query.all()
messages = [
dict(
id=str(m.id),
created=m.created.strftime("%Y-%m-%d %H:%M:%S"),
author=m.author,
message=str(m.message),
)
for m in messages
]
return jsonify(dict(messages=messages)), 200
if __name__ == "__main__":
app.run(host="0.0.0.0")