Commit 3220ab80 authored by Kin Jin Ng's avatar Kin Jin Ng
Browse files

review enhancements

parent 2dc6aeb1
Pipeline #43360 failed with stage
in 48 seconds
......@@ -21,13 +21,13 @@
|- <router module name>.py
|- __init__.py
```
3. In `<router module name>.py` include the following global variables.
3. In `<router module name>.py` include:
```python
from fastapi import APIRouter
router = APIRouter()
router_prefix = '<extension router prefix>'
router_tags = ['<extension router tags>']
router.prefix = '<extension router prefix>'
router.tags = ['<extension router tags>']
```
4. Include new service endpoints to `router` in `<router module name>.py`.
- Use `@router.[get|post|delete|put|patch|...]` fastapi decorator to include new methods to the router
......@@ -83,15 +83,15 @@ Loading `app.extensions.<extension package>.routers.<router module>` extension
#### Troubleshooting
##### A. Wrong router module configuration
Check step #3 above
Check step 3 above
```shell
Loading `app.extensions.{}.routers.{}` extension
Failed to load `app.extensions.{}.routers.{}` extension.
Module not configured properly. module 'app.extensions.{}.routers.{}' has no attribute 'router_prefix'
Module not configured properly. module 'app.extensions.{}.routers.{}' has no attribute 'router'
```
##### B. Wrong module name
Review steps #2 and #4 above
Review steps 2 and 4 above
```shell
Loading `app.extensions.{}.routers.{}` extension
Failed to load `app.extensions.{}.routers.{}` extension.
......@@ -100,8 +100,16 @@ Loading `app.extensions.{}.routers.{}` extension
```
##### C. Trailing comma in `EXTENSION_MODULES` list
Review step #4 above, make sure there is no trailing comma in the `EXTENSION_MODULES` list.
Review step 4 above, make sure there is no trailing comma in the `EXTENSION_MODULES` list.
```shell
Loading `` extension
Failed to load `` extension. Empty module name
```
##### D. Empty router prefix or tags
Check step 3 above
```shell
Loading `app.extensions.{}.routers.{}` extension
Failed to load `app.extensions.{}.routers.{}` extension.
Module not configured properly. Router prefix cannot be empty.
```
\ No newline at end of file
# Load extension modules [alpha version]
import importlib
import sys
from app.conf import Config
from app.helper import logger
discovered_routers = []
def get_routers():
if len(discovered_routers) == 0:
load_extensions()
return discovered_routers
def load_extensions():
discovered_extensions = []
extension_modules = Config.extension_modules.value
if extension_modules:
discovered_extensions = extension_modules.split(',')
logger.get_logger().info(f'Discovered extensions: {discovered_extensions}')
for name in discovered_extensions:
load_extension(name)
def load_extension(name):
log = logger.get_logger()
try:
log.info(f'Loading `{name}` extension')
module = importlib.import_module(name)
router = module.router
if not router.prefix:
raise AttributeError('Router prefix cannot be empty')
if not router.tags:
raise AttributeError('Router tags cannot be empty')
discovered_routers.append(router)
log.info(f'Done. `{name}` loaded')
except AttributeError as error:
log.warning(f'Failed to load `{name}` extension. Module not configured properly. {error}')
except ModuleNotFoundError as error:
log.warning(f'Failed to load `{name}` extension. Module not found. {error}')
except ValueError as error:
log.warning(f'Failed to load `{name}` extension. {error}')
except:
log.warning(f'Failed to load `{name}` extension. {sys.exc_info()[0]}')
......@@ -20,6 +20,7 @@ from app import __version__, __build_number__, __app_name__
from app.auth.auth import require_opendes_authorized_user
from app.conf import Config, check_environment
from app.errors.exception_handlers import add_exception_handlers
from app.extensions import discoverer
from app.helper import traces, logger
from app.injector.app_injector import AppInjector
......@@ -35,7 +36,8 @@ from app.routers.ddms_v2 import (
log_ddms_v2,
well_ddms_v2
)
from app.routers.ddms_v3 import wellbore_ddms_v3, well_ddms_v3, welllog_ddms_v3, wellbore_trajectory_ddms_v3, markerset_ddms_v3
from app.routers.ddms_v3 import wellbore_ddms_v3, well_ddms_v3, welllog_ddms_v3, wellbore_trajectory_ddms_v3, \
markerset_ddms_v3
from app.routers.trajectory import trajectory_ddms_v2
from app.routers.dipset import dipset_ddms_v2, dip_ddms_v2
from app.routers.logrecognition import log_recognition
......@@ -43,12 +45,9 @@ from app.routers.search import search, fast_search
from app.clients import StorageRecordServiceClient, SearchServiceClient
from app.utils import get_http_client_session, OpenApiHandler, get_wdms_temp_dir
import importlib
base_app = FastAPI()
# The sub application which contains all the routers
wdms_app = FastAPI(title=__app_name__,
description='build ' + __build_number__,
......@@ -100,6 +99,8 @@ async def startup_event():
if Config.alpha_feature_enabled.value:
enable_alpha_feature()
add_extension_routers()
@base_app.on_event('shutdown')
async def shutdown_event():
......@@ -114,6 +115,7 @@ async def shutdown_event():
await get_http_client_session().close()
wellbore_api_group_prefix = '/ddms/v2'
wdms_app.include_router(probes.router)
......@@ -194,27 +196,22 @@ wdms_app.add_middleware(CreateBasicContextMiddleware, injector=app_injector)
# adding exception handling
add_exception_handlers(wdms_app)
# Load extensions [alpha version]
discovered_extensions = []
extension_modules = Config.extension_modules.value
if extension_modules:
discovered_extensions = extension_modules.split(',')
# Load and add router extensions [alpha version]
def add_extension_routers():
for router in discoverer.get_routers():
add_extension_router(router)
for name in discovered_extensions:
def add_extension_router(router):
log = logger.get_logger()
name = router.prefix
try:
print(f'Loading `{name}` extension')
module = importlib.import_module(name)
wdms_app.include_router(module.router, prefix=module.router_prefix, tags=module.router_tags,
dependencies=[
Depends(require_data_partition_id, use_cache=False),
Depends(require_opendes_authorized_user, use_cache=False)])
print(f'\tDone. `{name}` loaded')
except AttributeError as error:
print(f'\tFailed to load `{name}` extension. Module not configured properly. {error}')
except ModuleNotFoundError as error:
print(f'\tFailed to load `{name}` extension. Module not found. {error}')
log.info(f'Adding router family `{name}`')
wdms_app.include_router(router, dependencies=[Depends(require_data_partition_id, use_cache=False),
Depends(require_opendes_authorized_user, use_cache=False)])
log.info(f'Done. `{name}` added')
except ValueError as error:
print(f'\tFailed to load `{name}` extension. {error}')
log.warning(f'Failed to add `{name}` router. {error}')
except:
print(f'\tFailed to load `{name}` extension. {sys.exc_info()[0]}')
\ No newline at end of file
log.warning(f'Failed to add `{name}` router. {sys.exc_info()[0]}')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment