Skip to content

Commit

Permalink
SC-16584: SwaggerUi is available for all Glue applications (#468)
Browse files Browse the repository at this point in the history
* SC-16584: Update of swagger-ui image and Glue Infrastructure preparation

* SC-16584: Add optional glue endpoints

* SC-16584: refactored js + templates

---------

Co-authored-by: Filip Sushko <filip.sushko@spryker.com>
  • Loading branch information
fsmeier and zyuzka authored Jan 8, 2024
1 parent e65ab8c commit eaf37e9
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 17 deletions.
27 changes: 24 additions & 3 deletions bin/standalone/cli-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ class Dispatcher {
response.end();
}

_get_glueSchema(request, response) {
const fileLocation = process.env.SPRYKER_REST_API_SCHEMA_PATH || 'src/Generated/Glue/Specification/spryker_rest_api.schema.yml';
_options_glueStorefrontSchema(request, response) {
this._options_glueSchema(request, response)
}

_options_glueBackendSchema(request, response) {
this._options_glueSchema(request, response)
}

_get_schema(request, response, fileLocation) {
const baseUrl = request.headers['x-schema-base-url'] || '';

fs.readFile(process.env.PWD + '/' + fileLocation, 'utf8', function (error,schemaContent) {

response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET');

Expand All @@ -93,6 +99,21 @@ class Dispatcher {
response.end();
});
}

_get_glueSchema(request, response) {
const fileLocation = process.env.SPRYKER_REST_API_SCHEMA_PATH || 'src/Generated/Glue/Specification/spryker_rest_api.schema.yml';
this._get_schema(request, response, fileLocation);
}

_get_glueStorefrontSchema(request, response) {
const fileLocation = process.env.SPRYKER_REST_API_SCHEMA_PATH || 'src/Generated/GlueStorefront/Specification/spryker_storefront_api.schema.yml';
this._get_schema(request, response, fileLocation);
}

_get_glueBackendSchema(request, response) {
const fileLocation = process.env.SPRYKER_REST_API_SCHEMA_PATH || 'src/Generated/GlueBackend/Specification/spryker_backend_api.schema.yml';
this._get_schema(request, response, fileLocation);
}
}

class Server {
Expand Down
79 changes: 79 additions & 0 deletions generator/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ public function setIsActive(bool $isActive): void

$endpointMap = $projectData['_endpointMap'] = mapBackendEndpointsWithFallbackZed($projectData['_endpointMap']);

$projectData = buildSwaggerEnvVariables($projectData);

$projectData['_testing'] = [
'defaultPort' => $defaultPort,
'projectServices' => $projectData['services'],
Expand Down Expand Up @@ -581,6 +583,8 @@ static function ($endpoint) use ($projectData) {
implode(' ', $hosts)
), $output, $returnCode);



if ($returnCode > 0) {
exit($returnCode);
}
Expand All @@ -599,6 +603,7 @@ static function ($endpoint) use ($projectData) {
exit(1);
}


// -------------------------
/**
* @param array $projectData
Expand Down Expand Up @@ -1732,3 +1737,77 @@ function buildProjectData(array $projectData): array
->createProjectDataBuildProcessor()
->run($projectData);
}

function buildSwaggerEnvVariables(array $projectData): array
{
$services = $projectData['services'] ?? [];
$swaggerService = $services['swagger'] ?? [];

if (empty($swaggerService)) {
return $projectData;
}

$swaggerUrls = buildSwaggerUrls($projectData);

if (empty($swaggerUrls)) {
return $projectData;
}

$swaggerService['environment']['URLS'] = json_encode($swaggerUrls);
$projectData['services']['swagger'] = $swaggerService;

return $projectData;
}

function isGlueApplication(string $appName): bool
{
$glueApps = [
GLUE_APP,
GLUE_STOREFRONT,
GLUE_BACKEND,
];

return in_array($appName, $glueApps);
}

function buildGlueSwaggerUrl(string $appName, string $appHost, string $schema): array
{
$appSuffix = 'Api';

$appName = explode('-', $appName);
$appName[] = $appSuffix;

$appName = array_map('ucfirst', $appName);
$appName = implode(' ', $appName);

$schemaUrl = sprintf('%s://%s/%s', $schema, $appHost, 'schema.yml');

return [
'name' => $appName,
'url' => $schemaUrl,
];
}

function buildSwaggerUrls(array $projectData): array
{
$schema = getCurrentScheme($projectData);
$endpoints = $projectData['_endpointMap'] ?? [];

if (empty($endpoints)) {
return [];
}

$urlsEnvVariable = [];

$endpoints = $endpoints[array_key_first($endpoints)] ?? [];

foreach ($endpoints as $applicationName => $applicationHost) {
if (!isGlueApplication($applicationName)) {
continue;
}

$urlsEnvVariable[] = buildGlueSwaggerUrl($applicationName, $applicationHost, $schema);
}

return $urlsEnvVariable;
}
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{% extends "nginx/http/gateway/server.conf.twig" %}
{% set cli_proxy_pass_value = 'glueBackendSchema' %}
{% block locations %}
{{ parent() }}
{{ include('nginx/http/gateway/glue.schema_location.twig', {cli_proxy_pass: cli_proxy_pass_value}) }}
{% endblock locations %}

Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{% include 'nginx/http/gateway/glue.server.conf.twig' with _context %}
{% extends "nginx/http/gateway/server.conf.twig" %}
{% set cli_proxy_pass_value = 'glueStorefrontSchema' %}
{% block locations %}
{{ parent() }}
{{ include('nginx/http/gateway/glue.schema_location.twig', {cli_proxy_pass: cli_proxy_pass_value}) }}
{% endblock locations %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% block location_schema %}
location /schema.yml {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-SCHEMA-BASE-URL $scheme://$host;

resolver 127.0.0.11 valid=10s;
set $cli_proxy_pass http://cli:9000/{{ cli_proxy_pass }}/;
proxy_pass $cli_proxy_pass;
}
{% endblock location_schema %}
15 changes: 3 additions & 12 deletions generator/src/templates/nginx/http/gateway/glue.server.conf.twig
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{% extends "nginx/http/gateway/server.conf.twig" %}
{% set cli_proxy_pass_value = 'glueSchema' %}
{% block locations %}
{{ parent() }}
location /schema.yml {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-SCHEMA-BASE-URL $scheme://$host;

resolver 127.0.0.11 valid=10s;
set $cli_proxy_pass http://cli:9000/glueSchema/;
proxy_pass $cli_proxy_pass;
}
{{ parent() }}
{{ include('nginx/http/gateway/glue.schema_location.twig', {cli_proxy_pass: cli_proxy_pass_value}) }}
{% endblock locations %}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{% include "service/#{engine}/v3.24/#{engine}.yml.twig" with _context only %}
{% include "service/#{engine}/v5.10/#{engine}.yml.twig" with _context only %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{ serviceName }}:
image: swaggerapi/swagger-ui:v5.10.5
networks:
- private
labels:
'spryker.app.name': swagger
'spryker.app.type': hidden
'spryker.project': ${SPRYKER_DOCKER_PREFIX}:${SPRYKER_DOCKER_TAG}
{% if serviceData['environment'] is defined %}
environment:
{% for key, value in serviceData['environment'] %}
- {{ key }}={{ value | raw }}
{% endfor %}
{% endif %}
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "8080" ]
interval: 5s
timeout: 5s
retries: 5

0 comments on commit eaf37e9

Please sign in to comment.