-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
53 lines (43 loc) · 1.32 KB
/
app.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
import os
import sys
from flask import Flask
from pymongo import MongoClient
from flask_restful import Api
from dao import Dao
from resources import *
from flask_cors import CORS
# imports from other files
from errors import NotFoundError, UpdateError
from resources import Home, Story, StoryList
from util import Timeout
app = Flask(__name__)
# enable cross-origin requests
CORS(app)
# initialize the flask-api library
api = Api(app)
# initialize the mongo connection string variables
uri = os.getenv('MONGODB_URI')
username = os.getenv('MONGO_USERNAME')
client = MongoClient(uri)
# attempt to initialize story collection
try:
story_collection = client[username].story
except TypeError:
print ('Must enter Mongo username environment variable MONGO_USERNAME')
print ('Failed to start')
try:
with Timeout(5):
client.admin.command('ismaster')
except Timeout.Timeout:
print ('Connection to MongoDB failed, check MONGODB_URI environment variable')
print ('Failed to start')
dao = Dao(story_collection)
api.add_resource(Home, '/')
api.add_resource(Story, '/story', '/story/<string:name>',
resource_class_kwargs={'dao':dao})
api.add_resource(StoryList, '/stories',
resource_class_kwargs={'dao':dao})
api.add_resource(StoryImport, '/storyImport/<string:name>',
resource_class_kwargs={'dao':dao})
if __name__ == '__main__':
app.run()