<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>node &#8211; Pauls Blog</title>
	<atom:link href="https://sterl.org/tag/node/feed/" rel="self" type="application/rss+xml" />
	<link>https://sterl.org</link>
	<description></description>
	<lastBuildDate>Fri, 31 Mar 2023 15:33:15 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Azure Node Functions Apps and OpenAPI Swagger</title>
		<link>https://sterl.org/2023/03/azure-node-functions-apps-and-openapi-swagger/</link>
					<comments>https://sterl.org/2023/03/azure-node-functions-apps-and-openapi-swagger/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sun, 12 Mar 2023 09:15:26 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[OpenAPI]]></category>
		<category><![CDATA[swagger-ui]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=875</guid>

					<description><![CDATA[Sometimes we face the problem that we want to host a very simple web-service including a swagger-ui / OpenApi-UI. The question now is, what is the simplest solution, which works everywhere, even in a azure function app? Where are of course some libs etc. which on the other hand require us to run the web-server.&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Sometimes we face the problem that we want to host a very simple web-service including a swagger-ui / <a rel="noreferrer noopener" href="https://swagger.io/docs/open-source-tools/swagger-ui/usage/installation/" data-type="URL" data-id="https://swagger.io/docs/open-source-tools/swagger-ui/usage/installation/" target="_blank">OpenApi-UI</a>. The question now is, what is the simplest solution, which works everywhere, even in a azure function app?</p>



<p>Where are of course some libs etc. which on the other hand require us to run the web-server. If we look into the doc using the simple <strong>unpkg </strong>HTML files looks like to be the simplest solution.</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot; /&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;
  &lt;meta
    name=&quot;description&quot;
    content=&quot;SwaggerUI&quot;
  /&gt;
  &lt;title&gt;SwaggerUI&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;swagger-ui&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui-bundle.js&quot; crossorigin&gt;&lt;/script&gt;
&lt;script&gt;
  window.onload = () =&gt; {
    window.ui = SwaggerUIBundle({
      url: 'https://petstore3.swagger.io/api/v3/openapi.json',
      dom_id: '#swagger-ui',
    });
  };
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div>



<h3 class="wp-block-heading">What needs to be done</h3>



<ul class="wp-block-list">
<li>Get the HTML OpenAPI HTML file</li>



<li>Provide an OpenAPI YML or JSON</li>



<li>Create an endpoint which should host the doc</li>
</ul>



<p>This solution works also for a spring web app or a AWS function. We will go ahead and look into a Node JavaScript example to see how it may look like:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://sterl.org/wp-content/uploads/2023/03/azure-function-app-open-api-doc.png"><img fetchpriority="high" decoding="async" width="271" height="200" src="https://sterl.org/wp-content/uploads/2023/03/azure-function-app-open-api-doc.png" alt="" class="wp-image-876"/></a></figure></div>


<p>Which means we have to create a new azure function endpoint where we want to host the OpenApi-UI:</p>



<h3 class="wp-block-heading">function.json</h3>



<p>We can set the function to &#8222;anonymous&#8220; if we like and only get is required:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application/json&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JSON&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;json&quot;}">{
  &quot;bindings&quot;: [
    {
      &quot;authLevel&quot;: &quot;anonymous&quot;,
      &quot;type&quot;: &quot;httpTrigger&quot;,
      &quot;direction&quot;: &quot;in&quot;,
      &quot;name&quot;: &quot;req&quot;,
      &quot;methods&quot;: [
        &quot;get&quot;
      ]
    },
    {
      &quot;type&quot;: &quot;http&quot;,
      &quot;direction&quot;: &quot;out&quot;,
      &quot;name&quot;: &quot;res&quot;
    }
  ]
}</pre></div>



<h3 class="wp-block-heading">openApi.html</h3>



<p>The only adjustment to the original HTML file is the source of the <code>yml</code> or <code>json</code> file. You could also adjust the JS and CSS url:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot; /&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;
  &lt;meta name=&quot;description&quot; content=&quot;My SwaggerUI&quot; /&gt;
  &lt;title&gt;SwaggerUI&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;swagger-ui&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui-bundle.js&quot; crossorigin&gt;&lt;/script&gt;
&lt;script&gt;
  window.onload = () =&gt; {
    window.ui = SwaggerUIBundle({
      url: './doc?file=openApi.yml',
      dom_id: '#swagger-ui',
    });
  };
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div>



<h3 class="wp-block-heading">openApi.yml</h3>



<p>Just as an example here. Note here the <code>URL</code> which has a funny value of <code>_server.url_</code>, we will replace this value later.</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;yaml&quot;,&quot;mime&quot;:&quot;text/x-yaml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;YAML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;yaml&quot;}">openapi: 3.0.2
info:
  title: My OpenAPI
  version: v1
servers:
  - url: &quot;_server.url_&quot;
paths:
  /foo:
    get:
      summary: Som cool function
      responses:
        '200':
          description: Retrieved entities
          content:
            application/json:
              schema:
                type: string</pre></div>



<h3 class="wp-block-heading">index.js</h3>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}">&quot;use strict&quot;;
const fs = require('fs');
const util = require('util');
const readFileAsync = util.promisify(fs.readFile);

module.exports = async function (context, req) {
    switch(req.query.file) {
        case 'openApi.yml':
            // get the url and remove the doc suffix, azure specific
            const url = req.headers.referer.replace('/doc', '');
            let yml = await readFileAsync('doc/openApi.yml', 'UTF-8');
            yml = yml.replace('_server.url_', url);
            context.res = {
                body: yml,
                headers: {
                    &quot;Content-Type&quot;: &quot;application/yml; charset=utf-8&quot;
                }
            };
            break;
        default:
            context.res = {
                body: await readFileAsync('doc/openApi.html', 'UTF-8'),
                headers: {
                    &quot;Cache-Control&quot;: &quot;max-age=600&quot;, // optional cache header
                    &quot;Content-Type&quot;: &quot;text/html; charset=utf-8&quot;
                }
            };
            break;
    }
}</pre></div>



<p>The switch case is where to ensure we load only files we want to publish, as so you have to extend the case block if you want to host the JS files too. Of course they have to be added to your directory.</p>



<h2 class="wp-block-heading">Links</h2>



<ul class="wp-block-list">
<li><a href="https://swagger.io/docs/open-source-tools/swagger-ui/usage/installation/">https://swagger.io/docs/open-source-tools/swagger-ui/usage/installation/</a></li>



<li><a href="https://learn.microsoft.com/de-de/azure/azure-functions/functions-reference-node?t#use-async-and-await">https://learn.microsoft.com/de-de/azure/azure-functions/functions-reference-node?t#use-async-and-await</a></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2023/03/azure-node-functions-apps-and-openapi-swagger/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
