from cloudant import Cloudant from flask import Flask, render_template, request, jsonify, session, redirect, url_for, escape import atexit import cf_deployment_tracker import os import json import games.level # Emit Bluemix deployment event cf_deployment_tracker.track() app = Flask(__name__) app.secret_key = 'YouThinkYouCanGetThisSoEasilyThenYouAreWrong' default_rows = "15" default_cols = "60" default_Code = """#include <iostream> using namespace std; int main() { return 0; } """ if 'VCAP_SERVICES' in os.environ: vcap = json.loads(os.getenv('VCAP_SERVICES')) print('Found VCAP_SERVICES') if 'secretkey' in vcap: app.secretkey = vcap['secretkey']
def create_app(): """ Create the api as it's own app so that it's easier to scale it out on it's own in the future. :return: A flask object/wsgi callable. """ import cf_deployment_tracker from server.config import Config from os import environ as env from server.exceptions import APIException from server.web.utils import request_wants_json from server.web.rest.root import root_v1_blueprint from server.web.rest.demos import demos_v1_blueprint, setup_auth_from_request from server.web.rest.shipments import shipments_v1_blueprint from server.web.rest.distribution_centers import distribution_centers_v1_blueprint from server.web.rest.retailers import retailers_v1_blueprint from server.web.rest.products import products_v1_blueprint from server.web.rest.weather import weather_v1_blueprint # Emit Bluemix deployment event cf_deployment_tracker.track() # Create the app logistics_wizard = Flask('logistics_wizard', static_folder=None) CORS(logistics_wizard, origins=[re.compile('.*')], supports_credentials=True) if Config.ENVIRONMENT == 'DEV': logistics_wizard.debug = True # Register the blueprints for each component logistics_wizard.register_blueprint(root_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(demos_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(shipments_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(distribution_centers_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(retailers_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(products_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(weather_v1_blueprint, url_prefix='/api/v1') logistics_wizard.before_request(setup_auth_from_request) def exception_handler(e): """ Handle any exception thrown in the interface layer and return a JSON response with the error details. Wraps python exceptions with a generic exception message. :param e: The raised exception. :return: A Flask response object. """ if not isinstance(e, APIException): exc = APIException(u'Server Error', internal_details=unicode(e)) else: exc = e current_app.logger.error(exc) return Response(json.dumps(compose_error(exc, e)), status=exc.status_code, mimetype='application/json') def not_found_handler(e): current_app.logger.exception(e) if request_wants_json(): status_code = 404 return Response(json.dumps({ 'code': status_code, 'message': 'Resource not found.' }), status=status_code, mimetype='application/json') else: # TODO: Default to the root web page # return index() pass def bad_request_handler(e): current_app.logger.exception(e) status_code = 400 return Response(json.dumps({ 'code': status_code, 'message': 'Bad request.' }), status=status_code, mimetype='application/json') # Register error handlers logistics_wizard.errorhandler(Exception)(exception_handler) logistics_wizard.errorhandler(400)(bad_request_handler) logistics_wizard.errorhandler(404)(not_found_handler) return logistics_wizard
def deploy_event_test(self): """Send a sample test event to the dev deployment tracker""" dev_deployment_tracker_url = 'https://deployment-tracker-dev.mybluemix.net/api/v1/track' self.assertTrue(track(dev_deployment_tracker_url) is None)
from flask import Flask, render_template, request, redirect from templates.emontio_main_web import * from templates.emontio_main_web import main import cf_deployment_tracker import os cf_deployment_tracker.track() app = Flask(__name__) port = int(os.getenv('VCAP_APP_PORT', 8080)) @app.route("/") def main_web(): return render_template('index.html') @app.route("/emontio_main_web.py", methods = ['POST']) def run_emontio(): stock_id = request.form['stock_id'] stock_name = request.form['stock_name'] web_url = request.form['web_url'] if not stock_id or not stock_name or not web_url: return render_template('index.html' , stock_id=stock_id, stock_name=stock_name, web_url=web_url) else: if "www." not in web_url: web_url ="www." + web_url if not web_url.endswith(".com") == True: web_url = web_url + ".com" if not web_url.startswith("http://") == True:
def create_app(): """ Create the api as it's own app so that it's easier to scale it out on it's own in the future. :return: A flask object/wsgi callable. """ import cf_deployment_tracker from bluemix_service_discovery.service_publisher import ServicePublisher from server.config import Config from os import environ as env from server.exceptions import APIException from server.web.utils import request_wants_json from server.web.rest.root import root_v1_blueprint from server.web.rest.demos import demos_v1_blueprint, setup_auth_from_request from server.web.rest.shipments import shipments_v1_blueprint from server.web.rest.distribution_centers import distribution_centers_v1_blueprint from server.web.rest.retailers import retailers_v1_blueprint from server.web.rest.products import products_v1_blueprint # Emit Bluemix deployment event cf_deployment_tracker.track() # Create the app logistics_wizard = Flask('logistics_wizard', static_folder=None) CORS(logistics_wizard, origins=[re.compile('.*')], supports_credentials=True) if Config.ENVIRONMENT == 'DEV': logistics_wizard.debug = True # Register the blueprints for each component logistics_wizard.register_blueprint(root_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(demos_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(shipments_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(distribution_centers_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(retailers_v1_blueprint, url_prefix='/api/v1') logistics_wizard.register_blueprint(products_v1_blueprint, url_prefix='/api/v1') logistics_wizard.before_request(setup_auth_from_request) def exception_handler(e): """ Handle any exception thrown in the interface layer and return a JSON response with the error details. Wraps python exceptions with a generic exception message. :param e: The raised exception. :return: A Flask response object. """ if not isinstance(e, APIException): exc = APIException(u'Server Error', internal_details=unicode(e)) else: exc = e current_app.logger.error(exc) return Response(json.dumps(compose_error(exc, e)), status=exc.status_code, mimetype='application/json') def not_found_handler(e): current_app.logger.exception(e) if request_wants_json(): status_code = 404 return Response(json.dumps({ 'code': status_code, 'message': 'Resource not found.' }), status=status_code, mimetype='application/json') else: # TODO: Default to the root web page # return index() pass def bad_request_handler(e): current_app.logger.exception(e) status_code = 400 return Response(json.dumps({ 'code': status_code, 'message': 'Bad request.' }), status=status_code, mimetype='application/json') # Register error handlers logistics_wizard.errorhandler(Exception)(exception_handler) logistics_wizard.errorhandler(400)(bad_request_handler) logistics_wizard.errorhandler(404)(not_found_handler) # Register app with Service Discovery and initiate heartbeat cycle if running in PROD if Config.SD_STATUS == 'ON' and env.get('VCAP_APPLICATION') is not None: from signal import signal, SIGINT, SIGTERM from sys import exit # Create service publisher and register service creds = json.loads( env['VCAP_SERVICES'])['service_discovery'][0]['credentials'] publisher = ServicePublisher( 'lw-controller', 300, 'UP', json.loads(env['VCAP_APPLICATION'])['application_uris'][0], 'http', tags=[ 'logistics-wizard', 'front-end', env['LOGISTICS_WIZARD_ENV'] ], url=creds['url'], auth_token=creds['auth_token']) publisher.register_service(True) # Set up exit handlers for gracefully killing heartbeat thread def exit_app(*args): deregister_app(publisher) exit(0) signal(SIGTERM, exit_app) signal(SIGINT, exit_app) atexit.register(destroy_app, publisher) return logistics_wizard