-
Notifications
You must be signed in to change notification settings - Fork 12
/
settings.py
130 lines (115 loc) · 4.53 KB
/
settings.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
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
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Instantiates the Python Eve REST API Server.
Instantiates the Python Eve REST API Server for both local
and cloud (IBM Bluemix) execution. Provides a default catch-all
routing to provide API consumers with intentional responses
for all routes. Provides a redis cloud caching instance for
session management where desired.
"""
import os
import json
import re
import redis
from models import (mac)
__author__ = "Sanjay Joshi"
__copyright__ = "IBM Copyright 2015"
__credits__ = ["Sanjay Joshi"]
__license__ = "Apache 2.0"
__version__ = "1.0"
__maintainer__ = "Sanjay Joshi"
__email__ = "joshisa@us.ibm.com"
__status__ = "Prototype"
# Initialize Objects
# capture current working directory
PWD = os.getenv("PWD")
# set root folder path
ROOT_PATH = os.path.join(PWD, "macreduce")
# set default host and ports (change 5000 to avoid airplay collision)
APP_HOST = '0.0.0.0'
APP_PORT = os.getenv('PORT', '5005')
VCAP_CONFIG = os.getenv('VCAP_SERVICES')
VCAP_APPLICATION = os.getenv('VCAP_APPLICATION')
REDIS_INSTANCE = None
APP_URI = 'http://0.0.0.0:5005'
# Detect if we are deployed within Bluemix or not and act accordingly
if VCAP_CONFIG:
# We're hosted on Bluemix! Use the MongoLabs sandbox as our backend.
# Read the VCAP_APPLICATION environment variable to get its route
decoded_application = json.loads(VCAP_APPLICATION)
APP_URI = 'http://' + decoded_application['application_uris'][0]
SERVER_NAME = decoded_application['application_uris'][0]
# Read the VCAP_SERVICES environment variable
decoded_config = json.loads(VCAP_CONFIG)
# Loop through the service instances to capture connection info
for key, value in decoded_config.iteritems():
# Looking for an instance of a Mongo Bluemix Service
if key.startswith('mongo'):
mongo_creds = decoded_config[key][0]['credentials']
seq = (r'^mongodb\:\/\/(?P<username>[\W\w]+):(?P<password>[\W\w]+)'
'@'
'(?P<host>[\.\w]+):(?P<port>\d+)/(?P<database>[\W\w]+).*?$')
regex = re.compile(seq)
match = regex.search(mongo_creds['url'])
# Deconstruct MongoURL connection information
parseURI = match.groupdict()
MONGO_HOST = parseURI['host']
MONGO_PORT = int(parseURI['port'])
MONGO_USERNAME = parseURI['username']
MONGO_PASSWORD = parseURI['password']
MONGO_DBNAME = parseURI['database']
continue
# Looking for an instance of Redis
elif key.startswith('redis'):
redis_creds = decoded_config[key][0]['credentials']
REDIS_HOSTNAME = redis_creds['hostname']
REDIS_PASSWORD = redis_creds['password']
REDIS_PORT = int(redis_creds['port'])
REDIS_INSTANCE = (redis.Redis(host=REDIS_HOSTNAME,
password=REDIS_PASSWORD, port=REDIS_PORT))
continue
else:
# We're on my favorite Mac Laptop
MONGO_HOST = 'localhost'
MONGO_PORT = 27017
MONGO_USERNAME = 'user'
MONGO_PASSWORD = 'user'
MONGO_DBNAME = 'apitest'
# Enable URL_PREFIX. Used in conjunction with API_VERSION to build
# API Endpoints of the form <base_route>/<url_prefix>/<api_version>/
URL_PREFIX = 'api'
# Enable API Versioning. This will force API Calls to follow a form of
# <base_route>/<api_version>/<resource_title>/...
API_VERSION = 'v1'
# Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
# Enable reads (GET), edits (PATCH) and deletes of individual items
# (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'DELETE']
DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
MONGO_QUERY_BLACKLIST = ['$where']
# We enable standard client cache directives for all resources exposed by the
# API. We can always override these global settings later.
CACHE_CONTROL = 'max-age=20'
CACHE_EXPIRES = 20
# Accept-Language request headers
LANGUAGE_DEFAULT = 'en'
LANGUAGES = {
'en': 'English',
'es': 'Espanol',
'fr': 'French',
'pt': 'Portuguese',
'ar': 'Arabic'
}
# We enable Cross Origin Resource Sharing (CORS) to facilitate swagger.io
X_DOMAINS = '*'
X_HEADERS = ['Origin', 'X-Requested-With', 'Content-Type', 'Accept']
# Our API will expose the following resources (MongoDB collections):
# 'mac', ...
# In order to allow for proper data validation, we define behaviour
# and structure.
DOMAIN = {
'mac': mac.schema,
}