Basic commit to add get pages showing

- bootstrap script
- run script
- couple of views
- basic config
This commit is contained in:
Martyn Inglis
2015-12-10 10:56:59 +00:00
parent 761e7b4444
commit e25ca0e434
11 changed files with 169 additions and 6 deletions

View File

@@ -7,3 +7,4 @@ Application for the notification api.
Read and write notifications/status queue. Read and write notifications/status queue.
Get and update notification status. Get and update notification status.
mkvirtualenv -p /usr/local/bin/python3 notify-api

View File

@@ -0,0 +1,70 @@
import os
from flask._compat import string_types
from flask import Flask
from config import configs
def create_app(config_name):
application = Flask(__name__)
application.config['NOTIFY_API_ENVIRONMENT'] = config_name
application.config.from_object(configs[config_name])
init_app(application)
from .main import main as main_blueprint
application.register_blueprint(main_blueprint)
return application
def init_app(app):
for key, value in app.config.items():
if key in os.environ:
app.config[key] = convert_to_boolean(os.environ[key])
def convert_to_boolean(value):
"""Turn strings to bools if they look like them
Truthy things should be True
>>> for truthy in ['true', 'on', 'yes', '1']:
... assert convert_to_boolean(truthy) == True
Falsey things should be False
>>> for falsey in ['false', 'off', 'no', '0']:
... assert convert_to_boolean(falsey) == False
Other things should be unchanged
>>> for value in ['falsey', 'other', True, 0]:
... assert convert_to_boolean(value) == value
"""
if isinstance(value, string_types):
if value.lower() in ['t', 'true', 'on', 'yes', '1']:
return True
elif value.lower() in ['f', 'false', 'off', 'no', '0']:
return False
return value
def convert_to_number(value):
"""Turns numeric looking things into floats or ints
Integery things should be integers
>>> for inty in ['0', '1', '2', '99999']:
... assert isinstance(convert_to_number(inty), int)
Floaty things should be floats
>>> for floaty in ['0.99', '1.1', '1000.0000001']:
... assert isinstance(convert_to_number(floaty), float)
Other things should be unchanged
>>> for value in [0, 'other', True, 123]:
... assert convert_to_number(value) == value
"""
try:
return float(value) if "." in value else int(value)
except (TypeError, ValueError):
return value

7
app/main/__init__.py Normal file
View File

@@ -0,0 +1,7 @@
from flask import Blueprint
main = Blueprint('main', __name__)
from .views import notifications, index

7
app/main/views/index.py Normal file
View File

@@ -0,0 +1,7 @@
from flask import jsonify
from .. import main
@main.route('/', methods=['GET'])
def index():
return jsonify(result="hello world"), 200

View File

@@ -0,0 +1,7 @@
from flask import jsonify
from .. import main
@main.route('/notification', methods=['POST'])
def create_notification():
return jsonify(result="created"), 201

View File

@@ -1,11 +1,24 @@
from flask import Flask #!/usr/bin/env python
app = Flask(__name__) from __future__ import print_function
import os
from flask.ext.script import Manager, Server
from app import create_app
application = create_app(os.getenv('NOTIFY_API_ENVIRONMENT') or 'development')
manager = Manager(application)
port = int(os.environ.get('PORT', 6011))
manager.add_command("runserver", Server(host='0.0.0.0', port=port))
@app.route('/') @manager.command
def index(): def list_routes():
return 'Hello from notify-notifications-api' """List URLs of all application routes."""
for rule in sorted(application.url_map.iter_rules(), key=lambda r: r.rule):
print("{:10} {}".format(", ".join(rule.methods - set(['OPTIONS', 'HEAD'])), rule.rule))
if __name__ == '__main__': if __name__ == '__main__':
app.run(port=6011) manager.run()

21
config.py Normal file
View File

@@ -0,0 +1,21 @@
class Config(object):
DEBUG = False
class Development(Config):
DEBUG = True
class Test(Config):
DEBUG = True
class Live(Config):
pass
configs = {
'development': Development,
'test': Test,
'live': Live,
}

30
scripts/bootstrap.sh Executable file
View File

@@ -0,0 +1,30 @@
#!/bin/bash
#
# Bootstrap virtualenv environment and postgres databases locally.
#
# NOTE: This script expects to be run from the project root with
# ./scripts/bootstrap.sh
set -o pipefail
function display_result {
RESULT=$1
EXIT_STATUS=$2
TEST=$3
if [ $RESULT -ne 0 ]; then
echo -e "\033[31m$TEST failed\033[0m"
exit $EXIT_STATUS
else
echo -e "\033[32m$TEST passed\033[0m"
fi
}
if [ ! $VIRTUAL_ENV ]; then
virtualenv ./venv
. ./venv/bin/activate
fi
# Install Python development dependencies
pip3 install -r requirements_for_test.txt

3
scripts/run_app.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
python application.py runserver

4
setup.cfg Normal file
View File

@@ -0,0 +1,4 @@
[pep8]
max-line-length = 120
exclude = ./migrations,./venv,./venv3