Skip to content

Commit a384f4d

Browse files
committed
Move app creation out of init module
1 parent bdbf3ac commit a384f4d

4 files changed

Lines changed: 37 additions & 36 deletions

File tree

lso/__init__.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,3 @@
1414
"""LSO, an API for remotely running Ansible playbooks."""
1515

1616
__version__ = "3.0.0"
17-
18-
import logging
19-
20-
from fastapi import FastAPI
21-
from fastapi.middleware.cors import CORSMiddleware
22-
23-
from lso import environment
24-
from lso.routes.default import router as default_router
25-
from lso.routes.execute import router as executable_router
26-
from lso.routes.playbook import router as playbook_router
27-
28-
logger = logging.getLogger(__name__)
29-
30-
31-
def create_app() -> FastAPI:
32-
"""Initialise the LSO app."""
33-
app = FastAPI(docs_url="/api/doc", redoc_url="/api/redoc", openapi_url="/api/openapi.json")
34-
35-
app.add_middleware(
36-
CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]
37-
)
38-
39-
app.include_router(default_router, prefix="/api")
40-
app.include_router(playbook_router, prefix="/api/playbook")
41-
app.include_router(executable_router, prefix="/api/execute")
42-
43-
environment.setup_logging()
44-
45-
logger.info("FastAPI app initialized")
46-
47-
return app

lso/app.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,39 @@
1313

1414
"""Default app creation."""
1515

16-
import lso
16+
import logging
1717

18-
app = lso.create_app()
18+
from fastapi import FastAPI
19+
from fastapi.middleware.cors import CORSMiddleware
20+
21+
from lso import environment
22+
from lso.routes.default import router as default_router
23+
from lso.routes.execute import router as executable_router
24+
from lso.routes.playbook import router as playbook_router
25+
26+
logger = logging.getLogger(__name__)
27+
28+
29+
def create_app() -> FastAPI:
30+
"""Initialise the LSO app."""
31+
app = FastAPI(docs_url="/api/doc", redoc_url="/api/redoc", openapi_url="/api/openapi.json")
32+
33+
app.add_middleware(
34+
CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]
35+
)
36+
37+
app.include_router(default_router, prefix="/api")
38+
app.include_router(playbook_router, prefix="/api/playbook")
39+
app.include_router(executable_router, prefix="/api/execute")
40+
41+
environment.setup_logging()
42+
43+
logger.info("FastAPI app initialized")
44+
45+
return app
46+
47+
48+
app = create_app()
1949

2050
if __name__ == "__main__":
2151
import uvicorn

test/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def run(*args: Any, **kwargs: Any) -> Runner: # noqa: ARG001
5959
@pytest.fixture(scope="session")
6060
def client() -> TestClient:
6161
"""Return a client that can be used to test the server."""
62-
from lso import create_app # noqa: PLC0415
62+
from lso.app import create_app # noqa: PLC0415
6363

6464
app = create_app()
6565
return TestClient(app) # wait here until calling context ends

test/routes/test_playbook.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ def test_playbook_endpoint_invalid_host_vars(client: TestClient, mocked_ansible_
9595

9696
assert isinstance(response, dict)
9797
assert response["detail"] == [
98-
'[WARNING]: Skipping unexpected key (host_vars) in group (_meta), only "vars", '
99-
'"children" and "hosts" are valid\n',
98+
(
99+
'[WARNING]: Skipping unexpected key (host_vars) in group (_meta), only "vars", '
100+
'"children" and "hosts" are valid\n'
101+
),
100102
]
101103
responses.assert_call_count(TEST_CALLBACK_URL, 0)
102104

0 commit comments

Comments
 (0)