1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
""" |
17
|
|
|
Stream WSGI application. |
18
|
|
|
|
19
|
|
|
This application listens for events on the RabbitMQ message bus and delivers them to all the |
20
|
|
|
clients which are connected to the stream HTTP endpoint (fan out approach). |
21
|
|
|
|
22
|
|
|
Note: This app doesn't need access to MongoDB, just RabbitMQ. |
23
|
|
|
""" |
24
|
|
|
|
25
|
|
|
import os |
26
|
|
|
|
27
|
|
|
import pecan |
28
|
|
|
from oslo_config import cfg |
29
|
|
|
|
30
|
|
|
from st2stream import config as st2stream_config |
31
|
|
|
from st2common import hooks |
32
|
|
|
from st2common import log as logging |
33
|
|
|
from st2common.constants.system import VERSION_STRING |
34
|
|
|
from st2common.service_setup import setup as common_setup |
35
|
|
|
|
36
|
|
|
LOG = logging.getLogger(__name__) |
37
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def _get_pecan_config(): |
41
|
|
|
config = { |
42
|
|
|
'app': { |
43
|
|
|
'root': 'st2stream.controllers.root.RootController', |
44
|
|
|
'modules': ['st2auth'], |
45
|
|
|
'debug': cfg.CONF.stream.debug, |
46
|
|
|
'errors': {'__force_dict__': True}, |
47
|
|
|
'guess_content_type_from_ext': False |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return pecan.configuration.conf_from_dict(config) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def setup_app(config=None): |
55
|
|
|
LOG.info('Creating st2stream: %s as Pecan app.', VERSION_STRING) |
56
|
|
|
|
57
|
|
|
is_gunicorn = getattr(config, 'is_gunicorn', False) |
58
|
|
|
if is_gunicorn: |
59
|
|
|
st2stream_config.register_opts() |
60
|
|
|
# This should be called in gunicorn case because we only want |
61
|
|
|
# workers to connect to db, rabbbitmq etc. In standalone HTTP |
62
|
|
|
# server case, this setup would have already occurred. |
63
|
|
|
common_setup(service='stream', config=st2stream_config, setup_db=True, |
64
|
|
|
register_mq_exchanges=True, |
65
|
|
|
register_signal_handlers=True, |
66
|
|
|
register_internal_trigger_types=False, |
67
|
|
|
run_migrations=False, |
68
|
|
|
config_args=config.config_args) |
69
|
|
|
|
70
|
|
|
if not config: |
71
|
|
|
# standalone HTTP server case |
72
|
|
|
config = _get_pecan_config() |
73
|
|
|
else: |
74
|
|
|
# gunicorn case |
75
|
|
|
if is_gunicorn: |
76
|
|
|
config.app = _get_pecan_config().app |
77
|
|
|
|
78
|
|
|
app_conf = dict(config.app) |
79
|
|
|
|
80
|
|
|
active_hooks = [hooks.RequestIDHook(), hooks.JSONErrorResponseHook(), |
81
|
|
|
hooks.LoggingHook()] |
82
|
|
|
|
83
|
|
|
if cfg.CONF.auth.enable: |
84
|
|
|
active_hooks.append(hooks.AuthHook()) |
85
|
|
|
|
86
|
|
|
active_hooks.append(hooks.CorsHook()) |
87
|
|
|
|
88
|
|
|
app = pecan.make_app(app_conf.pop('root'), |
89
|
|
|
logging=getattr(config, 'logging', {}), |
90
|
|
|
hooks=active_hooks, |
91
|
|
|
**app_conf |
92
|
|
|
) |
93
|
|
|
|
94
|
|
|
LOG.info('%s app created.' % __name__) |
95
|
|
|
|
96
|
|
|
return app |
97
|
|
|
|