<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Roy Hoeymans</title>
    <description>The latest articles on DEV Community by Roy Hoeymans (@royhoey).</description>
    <link>https://dev.to/royhoey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F230642%2F3ff86e77-cf1b-4877-ab27-7539bf321c6a.png</url>
      <title>DEV Community: Roy Hoeymans</title>
      <link>https://dev.to/royhoey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/royhoey"/>
    <language>en</language>
    <item>
      <title>Hosting an Angular App as a static website with Azure Function Proxies</title>
      <dc:creator>Roy Hoeymans</dc:creator>
      <pubDate>Tue, 21 Jan 2020 09:28:10 +0000</pubDate>
      <link>https://dev.to/effectory/hosting-an-angular-app-as-a-static-website-with-azure-function-proxies-3m44</link>
      <guid>https://dev.to/effectory/hosting-an-angular-app-as-a-static-website-with-azure-function-proxies-3m44</guid>
      <description>&lt;p&gt;Azure static websites are a great way to host your web applications. Static websites are much &lt;a href="https://medium.com/medialesson/best-way-to-host-a-single-page-application-spa-in-microsoft-azure-3e70cbd075c3" rel="noopener noreferrer"&gt;cheaper and faster&lt;/a&gt; than app services. This makes them perfect for simple websites with static information, but you will run into two problems when trying to host an Angular app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 1: Custom headers
&lt;/h3&gt;

&lt;p&gt;One limitation of static websites is the inability to add custom headers to requests. Being able to add security headers such as CSP is an essential feature for modern websites. Although this feature is highly requested, it’s not available yet. You can upvote it on the &lt;a href="https://feedback.azure.com/forums/217298-storage/suggestions/34959124-allow-adding-headers-to-static-website-hosting-in" rel="noopener noreferrer"&gt;Azure feedback forum&lt;/a&gt; if you want to see this implemented!&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 2: Angular Routing
&lt;/h3&gt;

&lt;p&gt;Angular apps are a great fit for static websites, because routing happens on the client. However, a problem occurs when making a request to a ‘deep link’, such as &lt;code&gt;https://mysite.com/heroes/42&lt;/code&gt;. The request will return a 404 because it can not find the resource on the server. You can configure a static website to redirect failed requests to the &lt;code&gt;index.html&lt;/code&gt;, but the 404 will still show up in your network tab. This becomes a problem when using monitoring services such as Application Insights, or when writing integration tests with Selenium or Cypress.&lt;/p&gt;

&lt;p&gt;In this post I will describe how these problems can be overcome with the help of Azure Function proxies.&lt;/p&gt;




&lt;h2&gt;
  
  
  Setting up a static website
&lt;/h2&gt;

&lt;p&gt;Setting up a static website in Azure is very simple. Create an Azure storage account and enable the static website setting. You can find a step by step guide in the &lt;a href="https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website-host" rel="noopener noreferrer"&gt;Azure documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F2348%2F1%2AKGOMrBP4Bnn1UMZZkWaJcQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F2348%2F1%2AKGOMrBP4Bnn1UMZZkWaJcQ.png" alt="Static website screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enabling the static website setting will create a container called &lt;code&gt;$web&lt;/code&gt; in the blob storage. This is where you can upload static HTML, CSS, and Javascript files. In the case of an Angular app, this is where you would put the contents of your dist folder. To try it out, you can use the classic Angular Tour of Heroes app, which you can find on Github:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/johnpapa/angular-tour-of-heroes" rel="noopener noreferrer"&gt;https://github.com/johnpapa/angular-tour-of-heroes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clone the repository, build the app and copy the contents of the generated dist folder to the &lt;code&gt;$web&lt;/code&gt; container in the storage account. Of course you can also use your own Angular app with routing.&lt;/p&gt;

&lt;p&gt;When you enter the primary endpoint of your static website in your browser (e.g. &lt;code&gt;https://tourofheroes.z6.web.core.windows.net&lt;/code&gt;) you will see that your app is up and running!&lt;/p&gt;

&lt;p&gt;A useful tool to find out if you’re missing any security headers on a website is &lt;a href="https://securityheaders.com/" rel="noopener noreferrer"&gt;https://securityheaders.com/&lt;/a&gt;. Let’s scan our static website and see how we’re doing!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1915%2F1%2AYopqt4eI87vw8WxfqC4-vw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1915%2F1%2AYopqt4eI87vw8WxfqC4-vw.png" alt="Security report summary gives an F rating"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ouch, that doesn’t look good… Unfortunately it’s not possible to add custom headers to a static website in Azure (yet). This is where Azure Functions Proxies come in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding custom headers
&lt;/h2&gt;

&lt;p&gt;Azure Functions Proxies were introduced as a tool for serverless API management. They allow you to modify requests and responses of your APIs. We’re going to use them to add custom headers to requests to our static website, and to make requests to deep links work.&lt;/p&gt;

&lt;p&gt;Start by &lt;a href="https://docs.microsoft.com/nl-nl/azure/azure-functions/functions-create-first-azure-function#create-a-function-app" rel="noopener noreferrer"&gt;creating a new Function App&lt;/a&gt; in the Azure portal. Make sure to select the consumption plan to keep costs down. If you want to, you can select the same storage account as your static website. We’re only interested in adding proxies so there is no need to create functions inside the app.&lt;/p&gt;

&lt;p&gt;Once you’ve created the Function App, you are ready to start configuring proxy settings. Start by creating the first proxy named &lt;code&gt;root&lt;/code&gt;. This proxy will route the Function App URL to the &lt;code&gt;index.html&lt;/code&gt; of the static website. In the Route template field, enter ‘/’. Select the GET and HEAD headers in the Allowed HTTP methods. In the Backend URL field, enter the URL of your static website.&lt;/p&gt;

&lt;p&gt;The Response override section is where you can add custom headers! Let’s add the following security headers as recommended by securityheaders.com:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Header&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Strict-Transport-Security&lt;/td&gt;
&lt;td&gt;max-age=31536000; includeSubDomains&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;X-Content-Type-Options&lt;/td&gt;
&lt;td&gt;nosniff&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;X-XSS-Protection&lt;/td&gt;
&lt;td&gt;1; mode=block&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;x-frame-options&lt;/td&gt;
&lt;td&gt;SAMEORIGIN&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Content-Security-Policy&lt;/td&gt;
&lt;td&gt;default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline';&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Referrer-Policy&lt;/td&gt;
&lt;td&gt;same-origin&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Feature-Policy&lt;/td&gt;
&lt;td&gt;payment 'self'; geolocation 'self';&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Once you’re done, your proxy should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1024%2F1%2AxKh7sbudCpJYEx4U8cQ3Eg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1024%2F1%2AxKh7sbudCpJYEx4U8cQ3Eg.png" alt="Function proxies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Requests to the URL of the Azure function proxy will now be routed to the static website URL, with the added security headers! Entering the proxy URL in your browser will load the Angular app you uploaded to the static website. You will also notice that requests to the Javascript files needed to run the Angular app (e.g. &lt;code&gt;https://tour-of-heroes.azurewebsites.net/main.js&lt;/code&gt;) return 404 errors. This is because it will look for the file on the Azure Function website. We need to create another proxy that routes the request to the right file on the static website. Add another proxy called ‘files’ with the matching condition &lt;code&gt;/{filename}.{ext}&lt;/code&gt; that routes to &lt;code&gt;https://tourofheroes.z6.web.core.windows.net/{filename}/{ext}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F774%2F1%2Alx0RY1e9LxpfkJF5AfMgJQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F774%2F1%2Alx0RY1e9LxpfkJF5AfMgJQ.png" alt="files proxy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In case your Angular app uses assets, add another similar proxy. It should use the matching condition &lt;code&gt;/assets/{file}&lt;/code&gt; to route requests to &lt;code&gt;https://tourofheroes.z6.web.core.windows.net/assets/{file}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When visiting your site now, using the proxy URL, it should be able to find the Javascript files and load the full Angular app. Scan the proxy URL on securityheaders.com and you will see a result that’s a lot better!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F767%2F1%2AfeERMiSsbZfdl7F7YYPR2Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F767%2F1%2AfeERMiSsbZfdl7F7YYPR2Q.png" alt="Security header scan result A"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Making requests to Angular routes work
&lt;/h2&gt;

&lt;p&gt;If your app uses Angular routing you still have work to do. Navigating to a route starting from the root of the app causes no issues, because the Angular router interprets the URL and routes to that page. But entering a deep link in the browser address bar or refreshing the page while on a route does. An example of a deep link is something like &lt;code&gt;https://tour-of-heroes.azurewebsites.net/detail/14&lt;/code&gt;. The browser will make a direct request to the server for that URL, bypassing the Angular router, and return a 404.&lt;/p&gt;

&lt;p&gt;You’ll need to configure the server to route those requests to the &lt;code&gt;index.html&lt;/code&gt;. With a static website, you can solve this with another proxy.&lt;/p&gt;

&lt;p&gt;Use the matching condition &lt;code&gt;/{*restOfPath}&lt;/code&gt; to route all the requests to &lt;code&gt;https://tourofheroes.z6.web.core.windows.net/index.html&lt;/code&gt;. You can copy the response overrides from the root proxy using the UI, or use the advanced editor in the top bar to edit the &lt;code&gt;proxies.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F778%2F1%2AcVXeeCbOi1cWJGZrzzgwIQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F778%2F1%2AcVXeeCbOi1cWJGZrzzgwIQ.png" alt="routes proxy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you should be able to go straight to any Angular route by entering it in the browser address bar without encountering 404s!&lt;/p&gt;

&lt;p&gt;Below you can find an example of a complete &lt;code&gt;proxies.json&lt;/code&gt; file for an Angular web app:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The next possible step is to configure SSL and custom domains. You can do this in the Azure Function app itself or by creating an &lt;a href="https://docs.microsoft.com/en-us/azure/frontdoor/" rel="noopener noreferrer"&gt;Azure Front Door&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cost breakdown
&lt;/h2&gt;

&lt;p&gt;Since one of the advantages of using static websites is how cheap they are, we don’t want this solution to ramp up the costs.&lt;/p&gt;

&lt;p&gt;A request to an Azure Function proxy is billed as one execution. Consumption plan has a monthly free grant of 1 million executions, after that it will cost you €0,169 per million executions. There is also a charge for execution time, measured in gigabyte seconds (GB-s), rounded up to the nearest 128 MB. Memory used by a proxy is less than 128 MB.&lt;/p&gt;

&lt;p&gt;This means that 1 million requests will cost you nothing(!), and 10 million requests will cost you €1,61. Get an estimate for your use case with the &lt;a href="https://azure.microsoft.com/nl-nl/pricing/calculator/" rel="noopener noreferrer"&gt;Azure pricing calculator&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other considerations
&lt;/h2&gt;

&lt;p&gt;An alternative solution is using the rule engine in Azure CDN to add custom headers and re-write requests. You have to pick the Premium Verizon tier to be able to use it. The first 10 TB per month costs you €0,1333 per GB for requests in Zone 1 (Europe / US). I’m not sure how to compare this to the Azure Function costs, but the possibility of not having to pay anything for a month on a consumption plan appeals to me!&lt;/p&gt;

&lt;p&gt;What I like about Azure Function proxies is that changes made in the &lt;code&gt;proxies.json&lt;/code&gt; will apply immediately. New routing rules on Azure CDN can take up to a couple of hours to take effect, which can be quite annoying when setting it up.&lt;/p&gt;

&lt;p&gt;Another thing to keep in mind is the cold start of Azure Functions. Because of dynamic scaling, apps that haven’t executed in a while take longer to start up. There are workarounds for this though, like creating a timer triggered function that runs every couple of minutes. Or, if you are using Front Door, you can create a HTTP-triggered function for health checks to keep the app warm.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;By using Azure Function proxies you can overcome important limitations of static websites. Use them to add security headers and make requests to routes of your Angular app work!&lt;/p&gt;

&lt;p&gt;This way you can enjoy the low costs and high performance of a static website, without compromising on security.&lt;/p&gt;

&lt;p&gt;Happy hosting!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>azure</category>
      <category>proxies</category>
      <category>staticwebsite</category>
    </item>
  </channel>
</rss>
