|
16 | 16 | from oslo_config import cfg |
17 | 17 |
|
18 | 18 | from st2api import config as st2api_config |
19 | | -from st2common import log as logging |
20 | | -from st2common.middleware.streaming import StreamingMiddleware |
21 | | -from st2common.middleware.error_handling import ErrorHandlingMiddleware |
22 | | -from st2common.middleware.cors import CorsMiddleware |
23 | | -from st2common.middleware.request_id import RequestIDMiddleware |
24 | | -from st2common.middleware.logging import LoggingMiddleware |
25 | | -from st2common.middleware.instrumentation import RequestInstrumentationMiddleware |
26 | | -from st2common.middleware.instrumentation import ResponseInstrumentationMiddleware |
27 | | -from st2common.router import Router |
28 | | -from st2common.constants.system import VERSION_STRING |
29 | | -from st2common.service_setup import setup as common_setup |
30 | | -from st2common.util import spec_loader |
| 19 | +from st2common.openapi import app |
31 | 20 | from st2api.validation import validate_auth_cookie_is_correctly_configured |
32 | 21 | from st2api.validation import validate_rbac_is_correctly_configured |
33 | 22 |
|
34 | | -LOG = logging.getLogger(__name__) |
35 | 23 |
|
| 24 | +def setup_app(config={}): |
| 25 | + common_setup = { |
| 26 | + "register_mq_exchanges": True, |
| 27 | + "register_internal_trigger_types": True, |
| 28 | + "run_migrations": True, |
| 29 | + } |
36 | 30 |
|
37 | | -def setup_app(config=None): |
38 | | - config = config or {} |
39 | | - |
40 | | - LOG.info("Creating st2api: %s as OpenAPI app.", VERSION_STRING) |
41 | | - |
42 | | - is_gunicorn = config.get("is_gunicorn", False) |
43 | | - if is_gunicorn: |
44 | | - # NOTE: We only want to perform this logic in the WSGI worker |
45 | | - st2api_config.register_opts(ignore_errors=True) |
46 | | - capabilities = { |
47 | | - "name": "api", |
48 | | - "listen_host": cfg.CONF.api.host, |
49 | | - "listen_port": cfg.CONF.api.port, |
50 | | - "listen_ssl": cfg.CONF.api.use_ssl, |
51 | | - "type": "active", |
52 | | - } |
53 | | - |
54 | | - # This should be called in gunicorn case because we only want |
55 | | - # workers to connect to db, rabbbitmq etc. In standalone HTTP |
56 | | - # server case, this setup would have already occurred. |
57 | | - common_setup( |
58 | | - service="api", |
59 | | - config=st2api_config, |
60 | | - setup_db=True, |
61 | | - register_mq_exchanges=True, |
62 | | - register_signal_handlers=True, |
63 | | - register_internal_trigger_types=True, |
64 | | - run_migrations=True, |
65 | | - service_registry=True, |
66 | | - capabilities=capabilities, |
67 | | - config_args=config.get("config_args", None), |
68 | | - ) |
69 | | - |
70 | | - # Additional pre-run time checks |
71 | | - validate_auth_cookie_is_correctly_configured() |
72 | | - validate_rbac_is_correctly_configured() |
73 | | - |
74 | | - router = Router( |
75 | | - debug=cfg.CONF.api.debug, auth=cfg.CONF.auth.enable, is_gunicorn=is_gunicorn |
76 | | - ) |
| 31 | + pre_run_checks = [ |
| 32 | + validate_auth_cookie_is_correctly_configured, |
| 33 | + validate_rbac_is_correctly_configured, |
| 34 | + ] |
77 | 35 |
|
78 | | - spec = spec_loader.load_spec("st2common", "openapi.yaml.j2") |
79 | 36 | transforms = { |
80 | 37 | "^/api/v1/$": ["/v1"], |
81 | 38 | "^/api/v1/": ["/", "/v1/"], |
82 | 39 | "^/api/v1/executions": ["/actionexecutions", "/v1/actionexecutions"], |
83 | 40 | "^/api/exp/": ["/exp/"], |
84 | 41 | } |
85 | | - router.add_spec(spec, transforms=transforms) |
86 | | - |
87 | | - app = router.as_wsgi |
88 | 42 |
|
89 | | - # Order is important. Check middleware for detailed explanation. |
90 | | - app = StreamingMiddleware(app, path_whitelist=["/v1/executions/*/output*"]) |
91 | | - app = ErrorHandlingMiddleware(app) |
92 | | - app = CorsMiddleware(app) |
93 | | - app = LoggingMiddleware(app, router) |
94 | | - app = ResponseInstrumentationMiddleware(app, router, service_name="api") |
95 | | - app = RequestIDMiddleware(app) |
96 | | - app = RequestInstrumentationMiddleware(app, router, service_name="api") |
97 | | - |
98 | | - return app |
| 43 | + path_whitelist = ["/v1/executions/*/output*"] |
| 44 | + |
| 45 | + return app.setup_app( |
| 46 | + service_name="api", |
| 47 | + app_config=st2api_config, |
| 48 | + oslo_cfg=cfg.CONF.api, |
| 49 | + pre_run_checks=pre_run_checks, |
| 50 | + transforms=transforms, |
| 51 | + common_setup_kwargs=common_setup, |
| 52 | + path_whitelist=path_whitelist, |
| 53 | + config=config, |
| 54 | + ) |
0 commit comments