<s> import os <EOL> import pecan <EOL> from oslo_config import cfg <EOL> from pecan . middleware . static import StaticFileMiddleware <EOL> from st2api import config as st2api_config <EOL> from st2common import hooks <EOL> from st2common import log as logging <EOL> from st2common . util . monkey_patch import monkey_patch <EOL> from st2common . constants . system import VERSION_STRING <EOL> from st2common . service_setup import setup as common_setup <EOL> LOG = logging . getLogger ( __name__ ) <EOL> BASE_DIR = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> def _get_pecan_config ( ) : <EOL> opts = cfg . CONF . api_pecan <EOL> cfg_dict = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:root>' : opts . root , <EOL> '<STR_LIT>' : opts . template_path , <EOL> '<STR_LIT>' : opts . modules , <EOL> '<STR_LIT>' : opts . debug , <EOL> '<STR_LIT>' : opts . auth_enable , <EOL> '<STR_LIT>' : opts . errors , <EOL> '<STR_LIT>' : False <EOL> } <EOL> } <EOL> return pecan . configuration . conf_from_dict ( cfg_dict ) <EOL> def setup_app ( config = None ) : <EOL> LOG . info ( '<STR_LIT>' , VERSION_STRING ) <EOL> is_gunicorn = getattr ( config , '<STR_LIT>' , False ) <EOL> if is_gunicorn : <EOL> monkey_patch ( ) <EOL> st2api_config . register_opts ( ) <EOL> common_setup ( service = '<STR_LIT>' , config = st2api_config , setup_db = True , <EOL> register_mq_exchanges = True , <EOL> register_signal_handlers = True , <EOL> register_internal_trigger_types = True , <EOL> run_migrations = True , <EOL> config_args = config . config_args ) <EOL> if not config : <EOL> config = _get_pecan_config ( ) <EOL> else : <EOL> if is_gunicorn : <EOL> config . app = _get_pecan_config ( ) . app <EOL> app_conf = dict ( config . app ) <EOL> active_hooks = [ hooks . RequestIDHook ( ) , hooks . JSONErrorResponseHook ( ) , <EOL> hooks . LoggingHook ( ) ] <EOL> if cfg . CONF . auth . enable : <EOL> active_hooks . append ( hooks . AuthHook ( ) ) <EOL> active_hooks . append ( hooks . CorsHook ( ) ) <EOL> app = pecan . make_app ( app_conf . pop ( '<STR_LIT:root>' ) , <EOL> logging = getattr ( config , '<STR_LIT>' , { } ) , <EOL> hooks = active_hooks , <EOL> ** app_conf <EOL> ) <EOL> static_root = os . path . join ( BASE_DIR , '<STR_LIT>' ) <EOL> app = StaticFileMiddleware ( app = app , directory = static_root ) <EOL> LOG . info ( '<STR_LIT>' % __name__ ) <EOL> return app </s>
