# -*- coding: utf-8 -*-

import redis
from flask import Flask, render_template

from .api import api
from .admin import admin
from .website import website

from .utils import INSTANCE_FOLDER_PATH
from .config import DefaultConfig
from .logger import logger

# For import *
__all__ = ['init_app']

DEFAULT_BLUEPRINTS = (
    api,
    admin,
    website
)


def init_app(config=None, app_name=None, blueprints=None):
    """Initiate the peekpy app"""
    if app_name is None:
        app_name = DefaultConfig.PROJECT
    if blueprints is None:
        blueprints = DEFAULT_BLUEPRINTS

    app = Flask(app_name,
                instance_path=INSTANCE_FOLDER_PATH,
                instance_relative_config=True)

    #Load the Flask app config
    init_config(app, config)

    #Register the blueprints
    register_blueprints(app, blueprints)

    #Initiate the error handlers
    init_error_handlers(app)

    #Init and test the redis database
    init_redis_db(app, config)

    return app


def init_config(app, config):
    """Load the Flask app config"""
    #Load the default configuration
    app.config.from_object(DefaultConfig)

    #Load from a cfg file in the instance folder
    app.config.from_pyfile('production.cfg', silent=True)

    #Load from the config file from an envar
    #app.config.from_envvar(app.name.upper() + '_SETTINGS')

    if config:
        app.config.from_object(config)


def init_error_handlers(app):
    """Init the various error handlers"""

    @app.errorhandler(403)
    def forbidden_page(error):
        return render_template("errors/forbidden_page.html"), 403

    @app.errorhandler(404)
    def page_not_found(error):
        return render_template("errors/page_not_found.html"), 404

    @app.errorhandler(500)
    def server_error_page(error):
        return render_template("errors/server_error.html"), 500


def register_blueprints(app, blueprints):
    """Register the blueprints in the Flask app object"""

    for blueprint in blueprints:
        app.register_blueprint(blueprint)


def init_logger(app):
    """Init the logger"""
    app.logger = logger


def init_redis_db(app, config):
    """Load and test the redis database"""
    try:
        #Try to connect to redis
        rs = redis.StrictRedis(host='localhost', port=6379, db=0)
        rs.client_list()

        #TODO: replace the redis server by the redis service
        #If succeed the redis server is store in the app config dict
        app.redis = rs

    except redis.ConnectionError:
        logger.fatal("Redis server connection failed")
@@ -438,24 +438,78 @@
 ekpy app%22%22%22%0A
+    if config is None:%0A        config = DefaultConfig%0A
     if app_n
@@ -540,24 +540,17 @@
 _name = 
-DefaultC
+c
 onfig.PR
@@ -1160,24 +1160,17 @@
 _object(
-DefaultC
+c
 onfig)%0A%0A
@@ -2258,16 +2258,95 @@
 o redis%0A
+        redis_host = config.REDIS_HOST%0A        redis_port = config.REDIS_PORT%0A%0A
         
@@ -2377,30 +2377,35 @@
 ost=
-'local
+redis_
 host
-'
 , port=
-6379
+redis_port
 , db

Change redis load