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
|
|
|
import os |
17
|
|
|
|
18
|
|
|
import pecan |
19
|
|
|
from oslo_config import cfg |
20
|
|
|
from pecan.middleware.static import StaticFileMiddleware |
21
|
|
|
|
22
|
|
|
from st2api import config as st2api_config |
23
|
|
|
from st2common import hooks |
24
|
|
|
from st2common import log as logging |
25
|
|
|
from st2common.constants.system import VERSION_STRING |
26
|
|
|
from st2common.service_setup import setup as common_setup |
27
|
|
|
|
28
|
|
|
LOG = logging.getLogger(__name__) |
29
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def _get_pecan_config(): |
33
|
|
|
opts = cfg.CONF.api_pecan |
34
|
|
|
|
35
|
|
|
cfg_dict = { |
36
|
|
|
'app': { |
37
|
|
|
'root': opts.root, |
38
|
|
|
'template_path': opts.template_path, |
39
|
|
|
'modules': opts.modules, |
40
|
|
|
'debug': opts.debug, |
41
|
|
|
'auth_enable': opts.auth_enable, |
42
|
|
|
'errors': opts.errors, |
43
|
|
|
'guess_content_type_from_ext': False |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return pecan.configuration.conf_from_dict(cfg_dict) |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def setup_app(config=None): |
51
|
|
|
LOG.info('Creating st2api: %s as Pecan app.', VERSION_STRING) |
52
|
|
|
|
53
|
|
|
is_gunicorn = getattr(config, 'is_gunicorn', False) |
54
|
|
|
if is_gunicorn: |
55
|
|
|
st2api_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='api', config=st2api_config, setup_db=True, |
60
|
|
|
register_mq_exchanges=True, |
61
|
|
|
register_signal_handlers=True, |
62
|
|
|
register_internal_trigger_types=True, |
63
|
|
|
run_migrations=True, |
64
|
|
|
config_args=config.config_args) |
65
|
|
|
|
66
|
|
|
if not config: |
67
|
|
|
# standalone HTTP server case |
68
|
|
|
config = _get_pecan_config() |
69
|
|
|
else: |
70
|
|
|
# gunicorn case |
71
|
|
|
if is_gunicorn: |
72
|
|
|
config.app = _get_pecan_config().app |
73
|
|
|
|
74
|
|
|
app_conf = dict(config.app) |
75
|
|
|
|
76
|
|
|
active_hooks = [hooks.RequestIDHook(), hooks.JSONErrorResponseHook(), |
77
|
|
|
hooks.LoggingHook()] |
78
|
|
|
|
79
|
|
|
if cfg.CONF.auth.enable: |
80
|
|
|
active_hooks.append(hooks.AuthHook()) |
81
|
|
|
|
82
|
|
|
active_hooks.append(hooks.CorsHook()) |
83
|
|
|
|
84
|
|
|
app = pecan.make_app(app_conf.pop('root'), |
85
|
|
|
logging=getattr(config, 'logging', {}), |
86
|
|
|
hooks=active_hooks, |
87
|
|
|
**app_conf |
88
|
|
|
) |
89
|
|
|
|
90
|
|
|
# Static middleware which servers common static assets such as logos |
91
|
|
|
static_root = os.path.join(BASE_DIR, 'public') |
92
|
|
|
app = StaticFileMiddleware(app=app, directory=static_root) |
93
|
|
|
|
94
|
|
|
LOG.info('%s app created.' % __name__) |
95
|
|
|
|
96
|
|
|
return app |
97
|
|
|
|