#!/usr/bin/env python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import logging
from flask import Flask
from flask_socketio import SocketIO
from werkzeug.contrib.fixers import ProxyFix

from dino import environ
from dino.config import ConfigKeys

__author__ = 'Oscar Eriksson <oscar@gmail.com>'

logger = logging.getLogger(__name__)
logging.getLogger('amqp').setLevel(logging.INFO)


def create_app():
    _app = Flask(
            import_name=__name__,
            template_folder='admin/templates/',
            static_folder='admin/static/')

    # used for encrypting cookies for handling sessions
    _app.config['SECRET_KEY'] = 'abc492ee-9739-11e6-a174-07f6b92d4a4b'

    message_queue_type = environ.env.config.get(ConfigKeys.TYPE, domain=ConfigKeys.QUEUE, default=None)
    if message_queue_type is None and not (len(environ.env.config) == 0 or environ.env.config.get(ConfigKeys.TESTING)):
        raise RuntimeError('no message queue type specified')

    message_queue = 'redis://%s' % environ.env.config.get(ConfigKeys.HOST, domain=ConfigKeys.CACHE_SERVICE, default='')
    message_channel = 'dino_%s' % environ.env.config.get(ConfigKeys.ENVIRONMENT, default='test')

    logger.info('message_queue: %s' % message_queue)

    _socketio = SocketIO(
            _app,
            logger=logger,
            engineio_logger=os.environ.get('DINO_DEBUG', '0') == '1',
            async_mode='eventlet',
            message_queue=message_queue,
            channel=message_channel)

    # preferably "emit" should be set during env creation, but the socketio object is not created until after env is
    environ.env.out_of_scope_emit = _socketio.emit

    _app.wsgi_app = ProxyFix(_app.wsgi_app)
    return _app, _socketio

app, socketio = create_app()

import dino.admin.routes

@@ -886,16 +886,1186 @@
 INFO)%0A%0A%0A
+class ReverseProxied(object):%0A    '''Wrap the application in this middleware and configure the%0A    front-end server to add these headers, to let you quietly bind%0A    this to a URL other than / and to an HTTP scheme that is%0A    different than what is used locally.%0A%0A    In nginx:%0A    location /myprefix %7B%0A        proxy_pass http://192.168.0.1:5001;%0A        proxy_set_header Host $host;%0A        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;%0A        proxy_set_header X-Scheme $scheme;%0A        proxy_set_header X-Script-Name /myprefix;%0A        %7D%0A%0A    :param app: the WSGI application%0A    '''%0A    def __init__(self, app):%0A        self.app = app%0A%0A    def __call__(self, environ, start_response):%0A        script_name = environ.get('HTTP_X_SCRIPT_NAME', '')%0A        if script_name:%0A            environ%5B'SCRIPT_NAME'%5D = script_name%0A            path_info = environ%5B'PATH_INFO'%5D%0A            if path_info.startswith(script_name):%0A                environ%5B'PATH_INFO'%5D = path_info%5Blen(script_name):%5D%0A%0A        scheme = environ.get('HTTP_X_SCHEME', '')%0A        if scheme:%0A            environ%5B'wsgi.url_scheme'%5D = scheme%0A        return self.app(environ, start_response)%0A%0A%0A
 def crea
@@ -3349,16 +3349,31 @@
 i_app = 
+ReverseProxied(
 ProxyFix
@@ -3387,16 +3387,17 @@
 sgi_app)
+)
 %0A    ret

allow reverse proy for web interface