-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
119 lines (96 loc) · 2.86 KB
/
Copy pathserver.py
File metadata and controls
119 lines (96 loc) · 2.86 KB
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
from flask import Flask, request, abort, render_template
from flask_sqlalchemy import SQLAlchemy
import os
import time
cur_dir = os.path.abspath(os.path.dirname(__file__))
version = '0.0.1a'
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(cur_dir, 'app.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
message = db.Column(db.String(255), index=True)
time = db.Column(db.String(100))
user = db.Column(db.Integer, db.ForeignKey('user.id'))
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
user = db.Column(db.String(30), index=True, unique=True)
password = db.Column(db.String(30))
messages = db.relationship('Message', backref='author', lazy='dynamic')
@app.route("/")
def hello():
info = status()
return render_template("index.html", info=info)
@app.route('/status')
def status():
"""
Получаем информацию по сервису
:return: JSON
"""
return {
'status': True,
'name': 'Chatter',
'time': time.strftime("%d-%m-%Y %H:%M:%S"),
'messages': Message.query.count(),
'users': User.query.count(),
'version': version
}
@app.route('/send', methods=['POST'])
def send():
"""
Принимаем JSON
{
"username": str,
"password": str,
"text": str
}
и записывает в базу данных
:return: JSON {"ok": true}
"""
username = request.json['username']
password = request.json['password']
cur_user = User.query.filter_by(user=username).first()
if cur_user:
if cur_user.password != password:
return abort(401)
else:
cur_user = User(user=username, password=password)
db.session.add(cur_user)
db.session.commit()
text = request.json['text']
current_time = time.time()
message = Message(message=text, time=current_time, user=cur_user.id)
db.session.add(message)
db.session.commit()
return {"ok": True}
@app.route('/history')
def history():
"""
История сообщений
:return: JSON
"""
after = float(request.args.get('after'))
messages = Message.query.filter(Message.time > after).all()
filtred = []
for message in messages:
filtred.append({
'text': message.message,
'username': message.author.user,
'time': message.time,
})
return {
'messages': filtred
}
@app.route("/install")
def install():
"""
Создание базы данных и пользователя Joe
:return: JSON {"ok": true}
"""
db.create_all()
joe = User(user='Joe', password='joe')
db.session.add(joe)
db.session.commit()
return {"ok": True}
app.run()