Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2stream/st2stream/app.py (1 issue)

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
from oslo_config import cfg
26
27
from st2stream import config as st2stream_config
28
from st2common import log as logging
29
from st2common.middleware.streaming import StreamingMiddleware
30
from st2common.middleware.error_handling import ErrorHandlingMiddleware
31
from st2common.middleware.cors import CorsMiddleware
32
from st2common.middleware.request_id import RequestIDMiddleware
33
from st2common.middleware.logging import LoggingMiddleware
34
from st2common.middleware.instrumentation import RequestInstrumentationMiddleware
35
from st2common.middleware.instrumentation import ResponseInstrumentationMiddleware
36
from st2common.router import Router
37
from st2common.util.monkey_patch import monkey_patch
38
from st2common.constants.system import VERSION_STRING
39
from st2common.service_setup import setup as common_setup
40
from st2common.util import spec_loader
41
42
LOG = logging.getLogger(__name__)
43
44
45
def setup_app(config={}):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
46
    LOG.info('Creating st2stream: %s as OpenAPI app.', VERSION_STRING)
47
48
    is_gunicorn = config.get('is_gunicorn', False)
49
    if is_gunicorn:
50
        # Note: We need to perform monkey patching in the worker. If we do it in
51
        # the master process (gunicorn_config.py), it breaks tons of things
52
        # including shutdown
53
        monkey_patch()
54
55
        st2stream_config.register_opts()
56
        # This should be called in gunicorn case because we only want
57
        # workers to connect to db, rabbbitmq etc. In standalone HTTP
58
        # server case, this setup would have already occurred.
59
        common_setup(service='stream', config=st2stream_config, setup_db=True,
60
                     register_mq_exchanges=True,
61
                     register_signal_handlers=True,
62
                     register_internal_trigger_types=False,
63
                     run_migrations=False,
64
                     config_args=config.get('config_args', None))
65
66
    router = Router(debug=cfg.CONF.stream.debug, auth=cfg.CONF.auth.enable,
67
                    is_gunicorn=is_gunicorn)
68
69
    spec = spec_loader.load_spec('st2common', 'openapi.yaml.j2')
70
    transforms = {
71
        '^/stream/v1/': ['/', '/v1/']
72
    }
73
    router.add_spec(spec, transforms=transforms)
74
75
    app = router.as_wsgi
76
77
    # Order is important. Check middleware for detailed explanation.
78
    app = StreamingMiddleware(app)
79
    app = ErrorHandlingMiddleware(app)
80
    app = CorsMiddleware(app)
81
    app = LoggingMiddleware(app, router)
82
    app = ResponseInstrumentationMiddleware(app, service_name='stream')
83
    app = RequestIDMiddleware(app)
84
    app = RequestInstrumentationMiddleware(app, service_name='stream')
85
86
    return app
87