-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
81 lines (69 loc) · 1.86 KB
/
start.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
from flask import Flask
from flask_cors import cross_origin
from flask_restful import Api
from sqlalchemy import desc
from services.postgres import init_postgres
from models.data_models import Comment, Source, Story
from flask_restful import fields, marshal_with
allowed_origins = [
"https://slacker-news-frontend.onrender.com",
r"(https:\/\/slacker-news-frontend-pr-)[0-9]+.onrender.com",
"http://localhost*",
]
session = init_postgres()
app = Flask(__name__)
api = Api(app)
@app.route("/stories")
@cross_origin(origins=allowed_origins)
@marshal_with(
{
"id": fields.String,
"createdDate": fields.String,
"sourceId": fields.String,
"title": fields.String,
"summary": fields.String,
"sourceUri": fields.String,
"externalId": fields.Integer,
}
)
def get_stories():
try:
items = session.query(Story).order_by(desc("createdDate")).all()
except:
session.rollback()
return items
@app.route("/sources")
@cross_origin(origins=allowed_origins)
@marshal_with(
{
"id": fields.String,
"source": fields.String,
"createdDate": fields.String,
"sourceMethod": fields.String,
"sourceUri": fields.String,
"content": fields.String,
"externalId": fields.Integer,
}
)
def get_sources():
items = session.query(Source).order_by(desc("createdDate")).all()
return items
@app.route("/comments")
@cross_origin(origins=allowed_origins)
@marshal_with(
{
"id": fields.String,
"sourceId": fields.String,
"createdDate": fields.String,
"summary": fields.String,
"externalId": fields.Integer,
}
)
def get_comments():
items = session.query(Comment).order_by(desc("createdDate")).all()
return items
@app.route("/")
def root():
return "OK"
if __name__ == "__main__":
app.run(debug=False)