Skip to content

Commit

Permalink
Feat: Django Deployable Swagger (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamputraintan authored Jan 3, 2025
1 parent efb00d8 commit 62809ba
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"app",
"aws_xray_sdk.ext.django",
'simple_history',
"drf_spectacular",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -146,6 +147,9 @@
"JSON_UNDERSCOREIZE": {
'no_underscore_before_number': True,
},
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
}

CORS_ORIGIN_ALLOW_ALL = True
Expand All @@ -164,3 +168,35 @@

# turn off xray more generally and, you can overwrite with env var AWS_XRAY_SDK_ENABLED=true at runtime
aws_xray_sdk.global_sdk_config.set_sdk_enabled(False)

REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] = 'drf_spectacular.openapi.AutoSchema'
SPECTACULAR_SETTINGS = {
'TITLE': 'Metadata Manager API',
'DESCRIPTION': 'The Metadata Manager API for UMCCR.',
'VERSION': '0.0.1',
'SERVE_INCLUDE_SCHEMA': False,
'SECURITY': [
{
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT",
}
],
'CONTACT': {
'name': 'UMCCR',
'email': '[email protected]'
},
"LICENSE": {
"name": "MIT License",
},
"EXTERNAL_DOCS": {
"description": "Terms of service",
"url": "https://umccr.org/",
},
'CAMELIZE_NAMES': True,
'POSTPROCESSING_HOOKS': [
'drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields',
'drf_spectacular.hooks.postprocess_schema_enums'
],
'SCHEMA_PATH_PREFIX': f'/api/{API_VERSION}/',
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,12 @@

INSTALLED_APPS += (
"django_extensions",
"drf_spectacular",
)

ROOT_URLCONF = "app.urls.local"

RUNSERVER_PLUS_PRINT_SQL_TRUNCATE = sys.maxsize

REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] = 'drf_spectacular.openapi.AutoSchema'

SPECTACULAR_SETTINGS = {
'TITLE': 'Metadata Manager API',
'DESCRIPTION': 'The Metadata Manager API for UMCCR.',
'VERSION': '0.0.1',
'SERVE_INCLUDE_SCHEMA': False,
'SECURITY': [
{
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT",
}
],
'CONTACT': {
'name': 'UMCCR',
'email': '[email protected]'
},
"LICENSE": {
"name": "MIT License",
},
"EXTERNAL_DOCS": {
"description": "Terms of service",
"url": "https://umccr.org/",
},
'CAMELIZE_NAMES': True,
'POSTPROCESSING_HOOKS': [
'drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields',
'drf_spectacular.hooks.postprocess_schema_enums'
],
'SCHEMA_PATH_PREFIX': f'/api/{API_VERSION}/',
}

REDOC_SETTINGS = {
"LAZY_RENDERING": False,
}

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.urls import path, include
from django.urls import path
from drf_spectacular.views import SpectacularJSONAPIView, SpectacularSwaggerView

from app.routers import OptionalSlashDefaultRouter
from app.viewsets import LibraryViewSet, SubjectViewSet, SampleViewSet, ProjectViewSet, ContactViewSet, \
Expand All @@ -20,6 +22,9 @@

urlpatterns = [
path(f"{api_base}", include(router.urls)),
path('schema/openapi.json', SpectacularJSONAPIView.as_view(), name='schema'),
path('schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'),
name='swagger-ui'),
]

handler500 = "rest_framework.exceptions.server_error"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from django.urls import path
from drf_spectacular.views import SpectacularJSONAPIView, SpectacularSwaggerView

from .base import urlpatterns as base_urlpatterns

urlpatterns = base_urlpatterns + [
path('schema/openapi.json', SpectacularJSONAPIView.as_view(), name='schema'),
path('swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'),
name='swagger-ui'),
# Some URL that only exists in the local environment
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Construct } from 'constructs';
import { Duration } from 'aws-cdk-lib';
import { Function } from 'aws-cdk-lib/aws-lambda';
import { HttpMethod, HttpRoute, HttpRouteKey } from 'aws-cdk-lib/aws-apigatewayv2';
import {
HttpMethod,
HttpNoneAuthorizer,
HttpRoute,
HttpRouteKey,
} from 'aws-cdk-lib/aws-apigatewayv2';
import { PythonFunction, PythonFunctionProps } from '@aws-cdk/aws-lambda-python-alpha';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
Expand Down Expand Up @@ -60,6 +65,14 @@ export class LambdaAPIConstruct extends Construct {
// add some integration to the http api gw
const apiIntegration = new HttpLambdaIntegration('ApiLambdaIntegration', this.lambda);

// Routes for API schemas
new HttpRoute(this, 'GetSchemaHttpRoute', {
httpApi: apiGW.httpApi,
integration: apiIntegration,
authorizer: new HttpNoneAuthorizer(), // No auth needed for schema
routeKey: HttpRouteKey.with(`/schema/{PROXY+}`, HttpMethod.GET),
});

new HttpRoute(this, 'GetHttpRoute', {
httpApi: apiGW.httpApi,
integration: apiIntegration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { EventBus, IEventBus, Rule } from 'aws-cdk-lib/aws-events';
import { LambdaFunction } from 'aws-cdk-lib/aws-events-targets';
import { PythonFunction, PythonLayerVersion } from '@aws-cdk/aws-lambda-python-alpha';
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import { HttpMethod, HttpRoute, HttpRouteKey } from 'aws-cdk-lib/aws-apigatewayv2';
import {
HttpMethod,
HttpNoneAuthorizer,
HttpRoute,
HttpRouteKey,
} from 'aws-cdk-lib/aws-apigatewayv2';
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { ApiGatewayConstruct, ApiGatewayConstructProps } from '../../../../components/api-gateway';
import { Architecture } from 'aws-cdk-lib/aws-lambda';
Expand Down Expand Up @@ -118,6 +123,14 @@ export class SequenceRunManagerStack extends Stack {

const apiIntegration = new HttpLambdaIntegration('ApiIntegration', apiFn);

// Routes for API schemas
new HttpRoute(this, 'GetSchemaHttpRoute', {
httpApi: srmApi.httpApi,
integration: apiIntegration,
authorizer: new HttpNoneAuthorizer(), // No auth needed for schema
routeKey: HttpRouteKey.with(`/schema/{PROXY+}`, HttpMethod.GET),
});

new HttpRoute(this, 'GetHttpRoute', {
httpApi: httpApi,
integration: apiIntegration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"rest_framework",
"sequence_run_manager",
"aws_xray_sdk.ext.django",
"drf_spectacular",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -143,6 +144,9 @@
"JSON_UNDERSCOREIZE": {
'no_underscore_before_number': True,
},
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
}

CORS_ORIGIN_ALLOW_ALL = True
Expand All @@ -161,3 +165,37 @@

# turn off xray more generally and, you can overwrite with env var AWS_XRAY_SDK_ENABLED=true at runtime
aws_xray_sdk.global_sdk_config.set_sdk_enabled(False)

# --- drf-spectacular settings

REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] = 'drf_spectacular.openapi.AutoSchema'

SPECTACULAR_SETTINGS = {
'TITLE': 'UMCCR OrcaBus sequence_run_manager API',
'DESCRIPTION': 'UMCCR OrcaBus sequence_run_manager API',
'VERSION': API_VERSION,
'SERVE_INCLUDE_SCHEMA': False,
'SECURITY': [
{
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT",
}
],
'CONTACT': {
'name': 'UMCCR',
'email': '[email protected]'
},
"LICENSE": {
"name": "MIT License",
},
"EXTERNAL_DOCS": {
"description": "Terms of service",
"url": "https://umccr.org/",
},
'CAMELIZE_NAMES': True,
'POSTPROCESSING_HOOKS': [
'drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields',
'drf_spectacular.hooks.postprocess_schema_enums'
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,9 @@

INSTALLED_APPS += (
"django_extensions",
"drf_spectacular",
)

ROOT_URLCONF = "sequence_run_manager.urls.local"

RUNSERVER_PLUS_PRINT_SQL_TRUNCATE = sys.maxsize

# --- drf-spectacular settings

REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] = 'drf_spectacular.openapi.AutoSchema'

SPECTACULAR_SETTINGS = {
'TITLE': 'UMCCR OrcaBus sequence_run_manager API',
'DESCRIPTION': 'UMCCR OrcaBus sequence_run_manager API',
'VERSION': API_VERSION,
'SERVE_INCLUDE_SCHEMA': False,
'SECURITY': [
{
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT",
}
],
'CONTACT': {
'name': 'UMCCR',
'email': '[email protected]'
},
"LICENSE": {
"name": "MIT License",
},
"EXTERNAL_DOCS": {
"description": "Terms of service",
"url": "https://umccr.org/",
},
'CAMELIZE_NAMES': True,
'POSTPROCESSING_HOOKS': [
'drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields',
'drf_spectacular.hooks.postprocess_schema_enums'
],
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.urls import path, include
from django.urls import path
from drf_spectacular.views import SpectacularJSONAPIView, SpectacularSwaggerView

from sequence_run_manager.routers import OptionalSlashDefaultRouter
from sequence_run_manager.viewsets.sequence import SequenceViewSet
Expand All @@ -18,6 +20,9 @@

urlpatterns = [
path(f"{api_base}", include(router.urls)),
path('schema/openapi.json', SpectacularJSONAPIView.as_view(), name='schema'),
path('schema/swagger-ui/',
SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
]

handler500 = "rest_framework.exceptions.server_error"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from django.urls import path
from drf_spectacular.views import SpectacularJSONAPIView, SpectacularSwaggerView

from .base import urlpatterns as base_urlpatterns

urlpatterns = base_urlpatterns + [
path('schema/openapi.json', SpectacularJSONAPIView.as_view(), name='schema'),
path('swagger-ui/',
SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
# Some URL that only exists in the local environment
]
15 changes: 14 additions & 1 deletion lib/workload/stateless/stacks/workflow-manager/deploy/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import {
} from 'aws-cdk-lib';
import { PythonFunction, PythonLayerVersion } from '@aws-cdk/aws-lambda-python-alpha';
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import { HttpMethod, HttpRoute, HttpRouteKey } from 'aws-cdk-lib/aws-apigatewayv2';
import {
HttpMethod,
HttpNoneAuthorizer,
HttpRoute,
HttpRouteKey,
} from 'aws-cdk-lib/aws-apigatewayv2';
import { PostgresManagerStack } from '../../../../stateful/stacks/postgres-manager/deploy/stack';
import { ManagedPolicy, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { ApiGatewayConstruct, ApiGatewayConstructProps } from '../../../../components/api-gateway';
Expand Down Expand Up @@ -124,6 +129,14 @@ export class WorkflowManagerStack extends Stack {

const apiIntegration = new HttpLambdaIntegration('ApiIntegration', apiFn);

// Routes for API schemas
new HttpRoute(this, 'GetSchemaHttpRoute', {
httpApi: wfmApi.httpApi,
integration: apiIntegration,
authorizer: new HttpNoneAuthorizer(), // No auth needed for schema
routeKey: HttpRouteKey.with(`/schema/{PROXY+}`, HttpMethod.GET),
});

new HttpRoute(this, 'GetHttpRoute', {
httpApi: httpApi,
integration: apiIntegration,
Expand Down
Loading

0 comments on commit 62809ba

Please sign in to comment.