<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gareth Erskine-Jones on Medium]]></title>
        <description><![CDATA[Stories by Gareth Erskine-Jones on Medium]]></description>
        <link>https://medium.com/@gsej?source=rss-cc3df6c928fb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VC01uJdfet5lpEMaiY8ugA.jpeg</url>
            <title>Stories by Gareth Erskine-Jones on Medium</title>
            <link>https://medium.com/@gsej?source=rss-cc3df6c928fb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:35:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gsej/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a simple kernel module on Linux]]></title>
            <link>https://faun.pub/building-the-simple-kernel-module-from-linux-device-drivers-on-arch-linux-9a8374a502a3?source=rss-cc3df6c928fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a8374a502a3</guid>
            <dc:creator><![CDATA[Gareth Erskine-Jones]]></dc:creator>
            <pubDate>Tue, 11 May 2021 19:52:25 GMT</pubDate>
            <atom:updated>2024-10-26T17:17:53.067Z</atom:updated>
            <content:encoded><![CDATA[<h4>This article is about building the first kernel module is presented in the book <a href="https://lwn.net/Kernel/LDD3/">Linux Device Drivers</a> on a machine running a modern kernel</h4><p>(Note: I have published an updated version of this article here:<br> <a href="https://blog.gsej.co.uk/linux-kernel-module/">https://blog.gsej.co.uk/linux-kernel-module/</a>)</p><p>I’ve been using Linux for a long time and currently using the Arch distribution. Only the command to install kernel headers is Arch specific here — the rest should apply to other distros.</p><p>Wanting to learn about Linux kernel programming, I started reading the book <a href="https://lwn.net/Kernel/LDD3/">Linux Device Drivers</a>. This is quite old (in computing terms). It was written in 2005, and the examples are based on the 2.6 Linux kernel.</p><p>I’m writing this in case anyone else is thinking of trying this first example in the book, and (1) doesn’t even know if the code sample from the book will work with a recent kernel (it does), or (2) encounters some of the issues I did. If you are trying to follow this, please refer to the errors below — there are errors around building the module, and errors about loading it. These are distinct and need to be resolved separately.</p><h3>The challenge</h3><p>I wanted to run the initial “hello, world” example, given in the book. This is a tiny kernel module, which, when loaded into the kernel, writes a message to a log.</p><h3>First problem — I can’t build the 2.6 kernel</h3><p>The book is based on the 2.6 kernels, and I downloaded the source and tried to build it. No success. It seems that in 15 years, the latest version of the compiler shipped with most Linux systems won’t build it. Or at least, won’t build it easily. I found many hints online, but nothing conclusive. So I gave up on this track.</p><h3>Next challenge — building the sample against an up-to-date kernel.</h3><p>Next, I tried to build the “hello, world” module against a modern (5.6 in my case) kernel. This took me a while to figure out — the MakeFilein the book specifically refers to the header versions, and in the end, I used another MakefileI found online. I also didn’t know where to put the source files. Eventually, I got this to build, details are below.</p><h3>Big Challenge — actually loading the compiled module</h3><p>After this step, I could build the module, but when I tried to load it with:</p><pre><br>sudo insmod ./hello.ko</pre><p>I got an error saying:</p><pre>insmod: ERROR: could not insert module ./hello.ko: Invalid module format</pre><p>I didn’t know what this meant but eventually figured out it was because my module wasn’t based on the same headers as my running kernel. To get my running kernel version I used:</p><pre>uname -r</pre><p>To get the version of the headers the module was compiled against, I used:</p><pre><br>modinfo hello.ko</pre><p>The result was different versions — so I was trying to load a module intended for one kernel version while running a different version.</p><p>To clean this up, I removed the custom kernel I had built and was running and updated my Arch system so I had the latest kernel version. I’m not sure what had gone wrong when I built my custom kernel — but I had somehow ended up with headers located in incorrect folders.</p><h3>Missing folder!</h3><p>After I’d cleared up my kernel situation, so my system, at boot, reported just one available kernel (the latest one for Arch), I tried the build again, and it failed as it was missing a folder called /lib/modules/{version}/ build where {version} is the kernel version.</p><p>To fix this I installed the kernel headers:</p><pre>$sudo pacman -S linux-headers</pre><h3>Success</h3><p>Then, it all worked. And I was glad.</p><h3>Solution — so, the instructions are:</h3><p>Create a folder in your home directory called, hello-module (or whatever). In that directory, add a file called hello.c, containing:</p><pre>#include &lt;linux/init.h&gt;<br>#include &lt;linux/module.h&gt;</pre><pre>MODULE_LICENSE(“Dual BSD/GPL”);</pre><pre>static int hello_init(void)<br>{<br>    printk(KERN_ALERT “Hello, world\n”);<br>    return 0;<br>}</pre><pre>static void hello_exit(void)<br>{<br>    printk(KERN_ALERT “Goodbye, cruel world\n”);<br>}</pre><pre>module_init(hello_init);<br>module_exit(hello_exit);</pre><p>Also, add a file called Makefile. Use this one:</p><pre>obj-m := hello.o<br> <br>KERNELDIR ?= /lib/modules/$(shell uname -r)/build<br>PWD := $(shell pwd)<br> <br>all: default<br> <br>default:<br> $(MAKE) -C $(KERNELDIR) M=$(PWD) modules<br> <br>clean:<br> rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions</pre><p>Open a terminal in that folder and run:</p><pre>make</pre><p>It should succeed, and you should have a file (along with some others) called hello.ko.</p><p>Run:</p><pre>moduleinfo hello.ko</pre><p>This should show the same version as your running kernel (which you can get from uname -r.</p><p>If so, all is good. Then run:</p><pre>sudo insmod ./hello.ko</pre><p>This should succeed silently, but by running:</p><pre>sudo dmesg</pre><p>You should see in the log, the message emitted from the loading module! Yay. Your code is running in the kernel.</p><figure><a href="https://faun.to/i9Pt9"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t_MOgMTr_f1N4qHN_R7KVA.png" /></a></figure><p>Join FAUN: <a href="https://faun.to/i9Pt9"><strong>Website</strong></a><strong> </strong>💻<strong>|</strong><a href="https://faun.dev/podcast"><strong>Podcast</strong></a><strong> </strong>🎙️<strong>|</strong><a href="https://twitter.com/joinfaun"><strong>Twitter</strong></a><strong> </strong>🐦<strong>|</strong><a href="https://www.facebook.com/faun.dev/"><strong>Facebook</strong></a><strong> </strong>👥<strong>|</strong><a href="https://instagram.com/fauncommunity/"><strong>Instagram</strong></a><strong> </strong>📷|<a href="https://www.facebook.com/groups/364904580892967/"><strong>Facebook Group</strong></a><strong> </strong>🗣️<strong>|</strong><a href="https://www.linkedin.com/company/faundev"><strong>Linkedin Group</strong></a><strong> </strong>💬<strong>|</strong> <a href="https://faun.dev/chat"><strong>Slack</strong></a> 📱<strong>|</strong><a href="https://thechief.io/"><strong>Cloud Native</strong> <strong>News</strong></a><strong> </strong>📰<strong>|</strong><a href="https://linktr.ee/faun.dev/"><strong>More</strong></a><strong>.</strong></p><p><strong>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a8374a502a3" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/building-the-simple-kernel-module-from-linux-device-drivers-on-arch-linux-9a8374a502a3">Building a simple kernel module on Linux</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Nginx configuration for Angular]]></title>
            <link>https://faun.pub/my-nginx-configuration-for-angular-6f748a4ff683?source=rss-cc3df6c928fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f748a4ff683</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[caching]]></category>
            <category><![CDATA[content-security-policy]]></category>
            <dc:creator><![CDATA[Gareth Erskine-Jones]]></dc:creator>
            <pubDate>Mon, 13 Apr 2020 14:56:16 GMT</pubDate>
            <atom:updated>2023-02-13T05:25:30.482Z</atom:updated>
            <content:encoded><![CDATA[<p>(Note: edited in May 2021 to include details for the new Permissions-Policy header).</p><p>An Angular app, when compiled and deployed, is served as a static website — it’s just an HTML file, a bunch of javascript and CSS files, and perhaps some additional assets like images and fonts.</p><p>Simple though this is, the web-server configuration still needs some attention —there are <strong>security headers</strong> to be set, <strong>caching policies for different files</strong>, and the server needs to be set up to serve the index file when a deep-linked route is requested.</p><p>This is a quick walk-through of the relevant parts of my Nginx configuration, which works well with a simple, out-of-the-box Angular application, and gets an <strong><em>A</em></strong> score on <a href="https://securityheaders.com/">securityheaders.com</a>. The complete configuration files can be found at the bottom of the article.</p><p>My server block starts like this:</p><pre>server {<br>    listen       80;<br>    server_name  localhost;<br>    root   /usr/share/nginx/html;</pre><p>I host my sites in docker containers on Azure, so they’re configured to listen on port 80. Https is handled outside of the container by the hosting environment.</p><pre>server_tokens off;</pre><p>This prevents the Nginx version from being sent in the server header, which can be considered a security risk.</p><pre>location ~ /index.html|.*\.json$ {<br>    expires -1;<br>    add_header Cache-Control &#39;no-store, no-cache, must-revalidate,<br>        proxy-revalidate, max-age=0&#39;;</pre><p>This location block will match the index.html file, and any JSON file, and it instructs the browser not to cache these files.</p><p>If the index.html file were to be cached by the browser, then when a new version of the application was deployed, users would still see the old version, unless the cache expired or was explicitly cleared.</p><p>I include JSON files here, as I use them to ship application settings (which I’ve written about <a href="https://medium.com/faun/angular-on-docker-environment-specific-settings-1e92c3ad01e6">here</a>, and want changes to those settings to take effect as quickly as possible.</p><p>The block ends by including a file that sets some security headers. I’ll look at the security-headers.conf file below.</p><pre>location ~ .*\.css$|.*\.js$ {<br>    add_header Cache-Control &#39;max-age=31449600&#39;; # one year<br>    include /etc/nginx/security-headers.conf;<br>}</pre><p>The next block matches CSS and JavaScript files and instructs the browser to cache them for a year. These often-large files, so benefit from prolonged caching, and because Angular employs cache-busting in its production builds, there’s no danger of stale files being used by the browser, provided the index.html file is not cached.</p><p>Once again, the block also includes the security headers.</p><pre>location / {<br>    try_files $uri$args $uri$args/ /index.html;<br>    add_header Cache-Control &#39;max-age=86400&#39;; # one day<br>    include /etc/nginx/security-headers.conf;<br>}</pre><p>The final location block matches all requests that aren’t matched by one of the other more specific blocks.</p><p>The try_files instruction attempts to match the request to a file or directory — and so this is the block that will match image or font files, for example.</p><p>If there are no matching files or directories, it uses the /index.html URI. This means that a user who tries to use a deep-linked URL, e.g. <a href="https://myangularapp.example.com/my/deep/link,">https://myangularapp.example.com/my/deep/link,</a> will be served the index.html file. Without this, the request would result in a 404.</p><p>Files that are matched by this block are cached for a day, which means that users benefit from caching of image or font files, but if these files are updated, the users will still get the new versions quite soon.</p><p>As ever, the security headers are also included.</p><h3>Security Headers</h3><p>Each location block includes the file security-headers.conf which causes the server to set a number of HTTP response headers.</p><p>It might be thought that these headers could just be set once in the server block, but unfortunately, that doesn’t work. If we add just one header in a location block, any headers set in the enclosing block are ignored — that’s why we need to set them in each location block, and so use a separate file to reduce repetition.</p><p>Each of the headers tells the consuming browser that it can apply certain security restrictions to the page, with a view to mitigating particular threats.</p><p>The full details of what each header does are beyond the scope of this article but you can check the security headers on your site, and read more about what each header does, at the excellent <a href="https://securityheaders.com/">https://securityheaders.com/</a>.</p><pre>add_header Strict-Transport-Security &quot;max-age=31449600; <br>   includeSubDomains&quot; always;</pre><p>This header tells the browser that in the future (for the period specified by max-age), it should only connect to your site using https. This reduces the threat from certain kinds of <em>man-in-the-middle</em> attacks. One year (31449600) is a typical value for max-age.</p><pre>add_header Content-Security-Policy &quot;object-src &#39;none&#39;; <br>   script-src &#39;self&#39;; <br>   script-src-elem &#39;self&#39;; <br>   base-uri &#39;self&#39;; <br>   require-trusted-types-for &#39;script&#39;&quot; always;</pre><p><em>Content-Security-Policy </em>is the most complex of the security headers, and depending on your site, it can take time to find a secure configuration that works. The rule given here works for a basic Angular site that doesn’t use any external scripts — but if you do load external resources (e.g. a google analytics script or a CDN hosted javascript library), you’ll need to make some changes.</p><p>The header instructs the browser in detail about which sources are valid for various types of content — for example, you can forbid the use of inline-scripts, restrict the locations that scripts can be loaded from, ban the use of embedded objects, etc. This can greatly help in defending against XSS attacks.</p><p>Google has some great resources on adopting CSP (<a href="https://csp.withgoogle.com/docs/strict-csp.html">https://csp.withgoogle.com/docs/strict-csp.html</a>), along with a tool to test your configuration (<a href="https://csp-evaluator.withgoogle.com/">https://csp-evaluator.withgoogle.com/</a>).</p><p>When you add the Content-Security-Policy header, any content loaded from sources which are not permitted will be blocked, and a detailed message logged in the browser console.</p><p>It can be useful when starting to use Content-Security-Policy to add the Content-Security-Policy-Report-Only header instead. This header displays the same message about forbidden content but doesn’t actually block it.</p><pre>add_header X-Frame-Options &quot;DENY&quot; always;</pre><p>This simple header prevents your site from being rendered in a <em>frame</em> or <em>iframe</em>. The intention is to block <a href="https://en.wikipedia.org/wiki/Clickjacking">clickjacking</a> attacks.</p><pre>add_header X-Content-Type-Options &quot;nosniff&quot; always;</pre><p>This header instructs the browser to use the content-type set by the server for a response rather than trying to figure it out for itself.</p><pre>add_header Referrer-Policy &quot;strict-origin&quot; always;</pre><p>This controls how much information is included in the Referrer header when you click on a link to another site. strict-origin truncates the value to just the origin (e.g. <a href="https://myangularapp.example.com)/">https://myangularapp.example.com)</a> rather than including the full path. There are a few other options, and which <a href="https://scotthelme.co.uk/a-new-security-header-referrer-policy/">Scott Helme covers comprehensively</a>.</p><pre>add_header Permissions-Policy &quot;microphone=(), geolocation=(), camera=()&quot; always;</pre><p>This setting adds a permissions-policy header equivalent to the feature-policy header above. At the moment I’m including both of these headers in the configs for my own sites.</p><p>My complete configuration is given below. This works with a basic Angular app created with the Angular CLI in production-mode and with no external scripts. It provides a decent basic caching regime, and it gets an <strong>A</strong> score on <a href="https://securityheaders.com/">securityheaders.com</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c0f9454c390909c2d834028ff8968f1/href">https://medium.com/media/6c0f9454c390909c2d834028ff8968f1/href</a></iframe><p>and:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81d9cc74f485548f2ef4586c1d7bbea5/href">https://medium.com/media/81d9cc74f485548f2ef4586c1d7bbea5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*Piks8Tu6xUYpF4DU" /></figure><p><strong>Follow us on </strong><a href="https://twitter.com/joinfaun"><strong>Twitter</strong></a><strong> </strong>🐦<strong> and </strong><a href="https://www.facebook.com/faun.dev/"><strong>Facebook</strong></a><strong> </strong>👥<strong> and </strong><a href="https://instagram.com/fauncommunity/"><strong>Instagram</strong></a><strong> </strong>📷 <strong>and join our </strong><a href="https://www.facebook.com/groups/364904580892967/"><strong>Facebook</strong></a><strong> and </strong><a href="https://www.linkedin.com/company/faundev"><strong>Linkedin</strong></a><strong> Groups </strong>💬<strong>.</strong></p><p><strong>To join our community Slack team chat </strong>🗣️ <strong>read our weekly Faun topics </strong>🗞️,<strong> and connect with the community </strong>📣<strong> click here⬇</strong></p><figure><a href="https://www.faun.dev/join/?utm_source=medium.com%2Ffaun&amp;utm_medium=medium&amp;utm_campaign=faunmediumbanner"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6P3WpLjGv5v1ucm5dgkucg.png" /></a></figure><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f748a4ff683" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/my-nginx-configuration-for-angular-6f748a4ff683">My Nginx configuration for Angular</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run a .NET Core app in a Docker container with SSH access]]></title>
            <link>https://faun.pub/run-a-net-core-app-in-a-docker-container-with-ssh-access-ab7f4f1f3d7?source=rss-cc3df6c928fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab7f4f1f3d7</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[supervisor]]></category>
            <dc:creator><![CDATA[Gareth Erskine-Jones]]></dc:creator>
            <pubDate>Fri, 10 Apr 2020 14:08:28 GMT</pubDate>
            <atom:updated>2020-04-10T16:35:05.323Z</atom:updated>
            <content:encoded><![CDATA[<h3>Run a .NET Core app in a Docker container with SSH access</h3><p>It’s often useful to be able to ssh into the environment where your application is running — especially during development or when troubleshooting an issue.</p><p>If you’re running a .NET Core app in a Docker container, you’ll need to specifically add ssh support to the container. Here I’m going to explain how to do that.</p><h3>Prerequisites and assumptions</h3><p>If you want to follow the steps below, you’ll need to have <a href="https://www.docker.com/products/docker-desktop">Docker</a> installed, and the <a href="https://dotnet.microsoft.com/download">.NET Core SDK</a>.</p><p>The code for this article can be found at <a href="https://github.com/gsej-demos/dotnetcoresshdocker">https://github.com/gsej-demos/dotnetcoresshdocker</a>.</p><p>The base images used here are based on Debian. If you change the examples to use different base images, the commands to install packages into the image may need to be changed.</p><h3>Creating the .NET Core .NET Core App</h3><p>To start, create a new .NET Core Api project:</p><pre>dotnet new api --name dotnetcoressh</pre><h4>Remove https</h4><p>Edit Startup.cs and remove the line:</p><pre>app.UseHttpsRedirection();</pre><p>Similarly, remove the https entry from launch.json — the file should look something like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3dd089d4753168161fe166c52ac018f/href">https://medium.com/media/d3dd089d4753168161fe166c52ac018f/href</a></iframe><p>We’re removing these because, it’s more common to terminate SSL outside of the container.</p><p>Now when we start the application with dotnet run we should see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*hBnaqNKn1LeWPljWNBwc8w.png" /></figure><p>The app is listening on the local port 5000, so we can navigate to the example endpoint provided by the project template, and we should see some output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*FLcvY2_v-A0ZP88CRWU5uw.png" /></figure><h3>Add Dockerfile</h3><p>So the app is running, but not in a container. Let’s add a Dockerfile to the project folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/748e84bce4b3382512977c7790eb824f/href">https://medium.com/media/748e84bce4b3382512977c7790eb824f/href</a></iframe><p>This builds our app in one container, and then copies the built binaries to our final image.</p><p>We should be able to now build the container, and run it.</p><pre>docker build -t dotnetcoressh .<br>docker run --rm --name api -d -p 8080:80 dotnetcoressh</pre><p>Microsoft’s .NET core images run default to running the app on port 80, so here we map that to 8080 on the local machine.</p><p>We can verify that the app is running correctly by navigating to <a href="http://localhost:8080/weatherforecast.">http://localhost:8080/weatherforecast.</a></p><p>Then we can cleanup by killing the container with docker stop api.</p><h3>Add Supervisord</h3><p>A Dockerfile can only contain one ENTRYPOINT, but we need to run two processes in our container — the .NET Core application and the sshd server. To do this, we can use <a href="http://supervisord.org/">supervisord</a>, which will start with the container, and can be configured to launch multiple processes.</p><p>Add to the project a file called supervisord.conf</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b9260368b78bfbfe53f35625222d837/href">https://medium.com/media/0b9260368b78bfbfe53f35625222d837/href</a></iframe><p>And modify the Dockerfile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/294a1c9cbcaf676417e1643e71d42c56/href">https://medium.com/media/294a1c9cbcaf676417e1643e71d42c56/href</a></iframe><p>Here we install supervisor in our image, create a directory that it needs to run, and copy the configuration file to the appropriate location. Finally we that it needs to run, and coping over the configuration file created above. Finally, we set the ENTRYPOINT to run supervisord — which, when it starts up, will run our .NET Core app.</p><p>Once again, build and run the container with:</p><pre>docker build -t dotnetcoressh .<br>docker run --rm --name api -d -p 8080:80 dotnetcoressh</pre><p>And verify that it’s still working by browsing to</p><p><a href="http://localhost:8080/weatherforecase%60">http://localhost:8080/weatherforecas</a>t</p><p>And cleanup with docker stop api.</p><h3>Adding sshd</h3><p>Now, with supervisord in place, we can add sshd.</p><p>Create an configuration file for the ssh daemon. This should be called sshd_config and it in the project folder. Note that we are running sshd on the non-standard port of 2222.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e4cd7df6ffdccd11bd2b2b24d13b5f2/href">https://medium.com/media/8e4cd7df6ffdccd11bd2b2b24d13b5f2/href</a></iframe><p>And modify the docker file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7697f1263057b07838f0be2d2e7c448/href">https://medium.com/media/f7697f1263057b07838f0be2d2e7c448/href</a></iframe><p>Here we install openssh-server,copy in the config file, and set the root password.</p><p>Finally, we update the supervisord.conf so that it starts sshd as well as the .NET Core app</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8d7562d03c7c3beab12f7eec7b8c4e1/href">https://medium.com/media/b8d7562d03c7c3beab12f7eec7b8c4e1/href</a></iframe><p>Once again, rebuild and run the container. This time the run command includes a port mapping for the ssh server.</p><pre>docker build -t dotnetcoressh .<br>docker run --rm --name api -d -p 8080:80 -p 2222:2222 dotnetcoressh</pre><p>The api endpoint should be available as before, but in addition, sshd will be running, and we can connect with ssh root@localhost -p 2222 and the password Docker! (as configured in the Dockerfile).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*EeWZwI4IwpFDeqxPqmA5tQ.png" /></figure><h3>Security</h3><p>It’s worth pointing out that this method involves a hard-coded password which may end up in your source control repository. Whether this is an issue depends on how you are going to use your image.</p><p>I run images configured like this on Azure’s App Service offering — in this environment port 2222 is not accessible externally, except through an authenticated proxy connection (<a href="https://docs.microsoft.com/en-us/azure/app-service/containers/app-service-linux-ssh-support">https://docs.microsoft.com/en-us/azure/app-service/containers/app-service-linux-ssh-support</a>). If you’re hosting in different circumstances, you should check to ensure you’re not introducing a security issue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*Piks8Tu6xUYpF4DU" /></figure><p><strong>Follow us on </strong><a href="https://twitter.com/joinfaun"><strong>Twitter</strong></a><strong> </strong>🐦<strong> and </strong><a href="https://www.facebook.com/faun.dev/"><strong>Facebook</strong></a><strong> </strong>👥<strong> and </strong><a href="https://instagram.com/fauncommunity/"><strong>Instagram</strong></a><strong> </strong>📷 <strong>and join our </strong><a href="https://www.facebook.com/groups/364904580892967/"><strong>Facebook</strong></a><strong> and </strong><a href="https://www.linkedin.com/company/faundev"><strong>Linkedin</strong></a><strong> Groups </strong>💬<strong>.</strong></p><p><strong>To join our community Slack team chat </strong>🗣️ <strong>read our weekly Faun topics </strong>🗞️,<strong> and connect with the community </strong>📣<strong> click here⬇</strong></p><figure><a href="https://www.faun.dev/join/?utm_source=medium.com%2Ffaun&amp;utm_medium=medium&amp;utm_campaign=faunmediumbanner"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6P3WpLjGv5v1ucm5dgkucg.png" /></a></figure><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab7f4f1f3d7" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/run-a-net-core-app-in-a-docker-container-with-ssh-access-ab7f4f1f3d7">Run a .NET Core app in a Docker container with SSH access</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memoizing http requests in Angular]]></title>
            <link>https://faun.pub/memoizing-http-requests-in-angular-6b9022b99ee3?source=rss-cc3df6c928fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b9022b99ee3</guid>
            <category><![CDATA[rxjs]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[memoization]]></category>
            <category><![CDATA[caching]]></category>
            <dc:creator><![CDATA[Gareth Erskine-Jones]]></dc:creator>
            <pubDate>Fri, 03 Apr 2020 17:19:06 GMT</pubDate>
            <atom:updated>2020-04-06T13:00:25.589Z</atom:updated>
            <content:encoded><![CDATA[<h3>Memoizing to reduce HTTP requests in Angular</h3><p>Many Single Page Applications make multiple ajax calls to fetch small sets of data that rarely change — for example, values to populate drop-down menus, lists of navigation items, or product categories</p><p>It’s not uncommon for complex pages to have a dozen of these small look-ups, and as a user navigates through different sections of the site, the same data can be fetched multiple times. Obviously this is inefficient, and you can end up with pages that take too long to become usable.</p><p>There are many ways to deal with this, but here I’m going to outline a very simple technique that allows data to be cached in the browser, without requiring major changes to the code-base. The technique is called memoizing and involves caching the result of a function call when you know that subsequent calls to that function with the same arguments ought to return the same results.</p><p>First, let’s looks a First I’m going to look at what happens with normal HTTP requests in an Angular application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d13ac14c920857b5db66c76df820320/href">https://medium.com/media/0d13ac14c920857b5db66c76df820320/href</a></iframe><p>The fetchResource method returns an observable, and when the caller subscribes to that observable, an HTTP request is made, and the results are returned. I’ve included a few tests to show that if the fetchResource method is called twice, perhaps from two different components, two HTTP requests are made.</p><p>In the second version of the code, the original fetchResourcemethod is renamed to _fetchResource, and made private. In the constructor of the service, we call the memoizefunction, passing in _fetchResource. The result of this call is another function, which we expose on a public property. None of the code which calls fetchResource needs to be changed — the interface of our services remains the same as before.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0af3e79e74cdc73253247b512cca84b/href">https://medium.com/media/d0af3e79e74cdc73253247b512cca84b/href</a></iframe><p>Again, I’ve included some test, and they demonstrate that multiple calls and subscriptions to fetchResource will return the correct result, but will only trigger a single HTTP request.</p><p>The memoizefunction is defined as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b92fc1519b162c1d7505dfc37211493/href">https://medium.com/media/3b92fc1519b162c1d7505dfc37211493/href</a></iframe><p>When memoize is called, with fetchResource as an argument, it creates an empty object to serve as a cache and then creates another function. This function takes some arguments, serializes them, and checks the cache to see if it already has a corresponding result. If it does, it returns it, otherwise it calls fetchResource, and caches the resulting observable (after piping it through shareReplay which ensures any subsequent subscribers will not trigger another HTTP request).</p><p>This is a a bit dense, I know, but the result is that calling the function multiple times, will result in the same observable being returned, and that observable, when subscribed to, will return the same result.</p><p>The values cached in this way will remain in the cache until the page is refreshed. For data which doesn’t change very often, or at all, this is fine. If you’re concerned however that a user might keep their browser open for a long time, and they’ll miss updates, then it’s simple to add a timeout:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01c64cddb202b95e34cb07dbd24b2d3d/href">https://medium.com/media/01c64cddb202b95e34cb07dbd24b2d3d/href</a></iframe><p>Here, the function works exactly as it did before, except that it recreates the cache object if it is older than 60 minutes.</p><p>I’ve found this technique useful in multiple projects — it can dramatically reduce the number of HTTP requests needed on pages with complex forms and multiple lookups. I hope you find it useful too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*Piks8Tu6xUYpF4DU" /></figure><p><strong>Follow us on </strong><a href="https://twitter.com/joinfaun"><strong>Twitter</strong></a><strong> </strong>🐦<strong> and </strong><a href="https://www.facebook.com/faun.dev/"><strong>Facebook</strong></a><strong> </strong>👥<strong> and </strong><a href="https://instagram.com/fauncommunity/"><strong>Instagram</strong></a><strong> </strong>📷 <strong>and join our </strong><a href="https://www.facebook.com/groups/364904580892967/"><strong>Facebook</strong></a><strong> and </strong><a href="https://www.linkedin.com/company/faundev"><strong>Linkedin</strong></a><strong> Groups </strong>💬<strong>.</strong></p><p><strong>To join our community Slack team chat </strong>🗣️ <strong>read our weekly Faun topics </strong>🗞️,<strong> and connect with the community </strong>📣<strong> click here⬇</strong></p><figure><a href="https://www.faun.dev/join/?utm_source=medium.com%2Ffaun&amp;utm_medium=medium&amp;utm_campaign=faunmediumbanner"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6P3WpLjGv5v1ucm5dgkucg.png" /></a></figure><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b9022b99ee3" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/memoizing-http-requests-in-angular-6b9022b99ee3">Memoizing http requests in Angular</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Starting with Terraform and Azure]]></title>
            <link>https://faun.pub/starting-with-terraform-and-azure-d5a974bddfc2?source=rss-cc3df6c928fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5a974bddfc2</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Gareth Erskine-Jones]]></dc:creator>
            <pubDate>Mon, 13 May 2019 06:36:34 GMT</pubDate>
            <atom:updated>2019-07-21T10:57:30.265Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*-j6iaRIXj9DKItaJW7giMg.jpeg" /><figcaption>Photo by Lorenzo Cafaro from Pexels</figcaption></figure><p>Creating infrastructure using the Azure Portal, one click at a time, can be a great way to explore, and to learn and see what’s available. When it comes to creating real environments for development, testing, and production however, this approach has major limitations.</p><ul><li>It’s time consuming to create environments in this way.</li><li>They are difficult to document.</li><li>It’s almost impossible to recreate them with complete accuracy.</li></ul><p>So ideally you should automate your creation of cloud infrastructure. This gives the ability to create environments by running a single command. The scripts or configuration files can be checked into source control and serve as documentation. Anyone with access to these files can create identical environments — eliminating the sorts of problems when something works in one environment but not another. Finally, when an environment is no longer needed, it can be destroyed within minutes by running another command.</p><p>There are a number of ways to do this, including writing scripts using <a href="https://docs.microsoft.com/en-us/powershell/azure/overview?view=azps-2.0.0">Azure Powershell</a> or <a href="https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli?view=azure-cli-latest">Azure CLI</a>, or by using <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-quickstart-create-templates-use-the-portal">Azure Resource Manager Templates</a>.</p><p>In this article I’m going to show how to start creating Azure resources with <a href="https://www.terraform.io/">Terraform</a>. We’ll create an app service, which will launch a docker container hosting a static website. In future articles, we’ll look at creating more complex resources, and at making our terraform configuration more flexible and reusable.</p><p>To follow along, you will of course, need to have some sort of Azure account. If you don’t have one, you can sign up for a free trial <a href="https://azure.microsoft.com/free/">here</a>.</p><h4>Why Terraform?</h4><p>With Terraform, you don’t write scripts which detail how to create your resources — rather you create a file, or set of files, called a configuration which describes how your resources should look once created. Terraform does the work of determining how to get to your desired state. This means you can apply a terraform configuration, make a small adjustment to it, and then apply it again, much more easily than you can adjust and re-run a powershell script — you don’t have to worry about what the current state of your infrastructure is, Terraform keeps track of that, and only makes the changes that are needed to achieve your desired state.</p><h4>Installing the pre-requisites</h4><p>The first thing to do is to download Terraform from <a href="https://www.terraform.io/downloads.html">here</a>. The download is a zip file containing a single executable (these instructions assume we are using Windows, but if you’re using Linux, most of this should still work). You need to extract the zip file, drop it in a folder, and add that folder to your path. Note that the configurations in this article assume you have at least version 0.12 of Terraform.</p><p>Next up, you’ll need to install the Azure CLI, which can be downloaded <a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest">here</a> for Windows users. Download links for other platforms are <a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest">available</a>. Download and follow the install instructions.</p><p>Open up your favouite command-prompt, and verify that everything is available</p><pre>&gt; terraform --version<br>Terraform v0.12.1</pre><pre>&gt; az --version<br>azure-cli                         2.0.62</pre><pre>(lots more output)</pre><h4>Creating the configuration file</h4><p>We are going to start with a single configuration file, so create a folder, open it with your code editor (I’m using Visual Studio Code), and create a file called main.tf, containing a single line.</p><pre>provider &quot;azurerm&quot; {}</pre><p>Save the file, open a command prompt and switch to the folder created earlier, and run:</p><p>Here we are telling Terraform which provider we want to us. Providers are the bridge between Terraform and the various kinds of infrastrure that can be controlled. There’s an extensive list of providers on <a href="https://www.terraform.io/docs/providers/">hashicorp’s site</a>. As we’re creating Azure resources, we’re using the azurerm provider.</p><p>Terraform will read the file, and download the azurerm provider which it saves in a folder called .terraform</p><h4>Creating our first resource</h4><p>Resources in Azure are created in resource groups, so we’ll now edit main.tf and add the following block:</p><pre>resource “azurerm_resource_group” “my-group” {<br> name = “my-resource-group”<br> location = “westus”<br>}</pre><p>Here, the word resource is part of the terraform configuration syntax, azurerm_resource_group is the type of resource we want to create, and my-group is the name of the resource. Note that this is the name of the resource in the terraform configuration — it is not the same thing as the name of the azure resource group, which is my-resource-group and appears inside the block.<br>Once you’ve added this to the file, you can run</p><pre>&gt; terraform plan</pre><p>Terraform will analyse the configuration and output what would need to be done to bring your infrastructure in line with what you’ve requested, in this case we’ll see something like this:</p><pre>An execution plan has been generated and is shown below.<br>Resource actions are indicated with the following symbols:<br> + create</pre><pre>Terraform will perform the following actions:<br>+ azurerm_resource_group.my-group<br> id: &lt;computed&gt;<br> location: “westus”<br> name: “my-resource-group”<br> tags.%: &lt;computed&gt;</pre><pre>Plan: 1 to add, 0 to change, 0 to destroy.</pre><p>If you’re not logged into Azure you’ll get an error like this:</p><pre>* provider.azurerm: Error building AzureRM Client: Error retrieving the Profile from the Azure CLI: No Subscription was Marked as Default in the Azure Profile. Please re-authenticate using `az login`.</pre><p>In this case you need to do as suggested, and run the az login command to authenticate, and then go back and run terraform plan again.</p><p>Once you’ve successfully run the plan command, you can go ahead and create the resource group with:</p><pre>&gt; terraform apply</pre><p>You’ll be prompted for confirmation, and then the resource group will be created. You can use the Azure portal to verify that the group does now exist.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*FZgfFaNaO8buwIVQPvgzQQ.png" /></figure><h4>State file</h4><p>If you look at your configuration folder, you’ll now see that it contains an additional file — terraform.tfstate. You shouldn’t edit this file, but feel free to take a look at its contents — it’s a description of the current state of your infrastructure. When Terraform processes the configuration and determines what needs to be done to bring your infrastructure into line with it, it is comparing the configuration with the state described in this file, rather than looking at the actual resources you’ve created. This implies two things —</p><ol><li>You shouldn’t alter your Terraform created infrastructure, except through Terraform. If you were to alter your resources using the Azure Portal for instance, then those changes wouldn’t be reflected in your terraform state file.</li><li>The state file is important, and having it in your configuration folder isn’t ideal. A co-worker who also needs to work with your Terraform configuration will need access to the file. You could commit it to source control along with your configuration, but if the state file contains secrets, then that again isn’t a good idea. I’ll explore a better solution in a future article.</li></ol><h4>Creating a website</h4><p>Let’s add a couple more resources to our configuration.</p><pre>resource &quot;azurerm_app_service_plan&quot; &quot;appserviceplan&quot; {<br>  name                = &quot;my-service-plan&quot;<br>  location            = &quot;westus&quot;<br>  resource_group_name = azurerm_resource_group.my-group.name<br>  reserved = true # Mandatory for Linux plans<br>  kind = &quot;Linux&quot;</pre><pre>  sku {<br>    tier = &quot;Basic&quot;<br>    size = &quot;B1&quot;<br>  }<br>}</pre><pre>resource &quot;azurerm_app_service&quot; &quot;web&quot; {<br>  name                = &quot;my-site&quot;<br>  location            = &quot;westus&quot;<br>  resource_group_name = azurerm_resource_group.my-group.name<br>  app_service_plan_id = azurerm_app_service_plan.appserviceplan.id</pre><pre># Configure Docker Image to load on start</pre><pre>  site_config {<br>    linux_fx_version = &quot;DOCKER|nginx&quot;<br>  }<br>}</pre><p>So here we are adding to our resource group a service plan, and an app service. The app service is configured to start up a container using the default nginx image, which displays a simple static web page.</p><p>There are a number of things worth noting here.</p><p>Firstly, the name of the appservice will be used to create the url for the website. The url in this case would be my-site.azurewebsites.net. Unsurprisingly, this has to be globally unique, so you’ll need to change the name to something else. I usually use a fixed prefix which I think will be unique, followed by a suffix which reflects the purpose of the site — e.g. myclient-web-test-1.</p><p>Secondly, we can see the syntax used to refer to properties of previously defined resources (strictly speaking, just other resources — as the order in the file doesn’t matter). I could just have hardcoded the resource group name rather than using this syntax, but by writing it this way, am informing Terraform that the app service plan depends on the resource group. This helps Terraform to construct a dependency graph of the resources it’s to create. Without this information, it may attempt to create them in the wrong order.</p><p>Once the file’s been updated, we can run terraform apply again, and then we can see that in the portal that, our website exists, and that we can access it through the configured URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TlP_25Jy3CX8k7QP09EXqg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bGlzRsIjvbMDleUkUUL6Lg.png" /></figure><p>The final step, when we’ve finished experimenting, is to tear down the environment we’ve created. This is as simple as running</p><pre>&gt; terraform destroy</pre><p>After a confirmation step, all of the resources we’ve created will be deleted, and we’re back to a clean slate.</p><p>In the next article I’ll build on the configuration we used here, and show how we can build more complex environments, and how we can create multiple environments which are identical apart from their names, so we can have test and development environments which are truly production-like.</p><p>Our final configuration file, at this stage looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0051d2b0129fa98cb6dfe5ac04665fea/href">https://medium.com/media/0051d2b0129fa98cb6dfe5ac04665fea/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*Piks8Tu6xUYpF4DU" /></figure><p><strong>Follow us on </strong><a href="https://twitter.com/joinfaun"><strong>Twitter</strong></a><strong> </strong>🐦<strong> and </strong><a href="https://www.facebook.com/faun.dev/"><strong>Facebook</strong></a><strong> </strong>👥<strong> and join our </strong><a href="https://www.facebook.com/groups/364904580892967/"><strong>Facebook Group</strong></a><strong> </strong>💬<strong>.</strong></p><p><strong>To join our community Slack </strong>🗣️ <strong>and read our weekly Faun topics </strong>🗞️,<strong> click here⬇</strong></p><figure><a href="https://www.faun.dev/join/?utm_source=medium.com%2Ffaun&amp;utm_medium=medium&amp;utm_campaign=faunmediumbanner"><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oSdFkACJxs5iy1oR" /></a></figure><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5a974bddfc2" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/starting-with-terraform-and-azure-d5a974bddfc2">Starting with Terraform and Azure</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tell users when you’ve updated your Angular app]]></title>
            <link>https://gsej.medium.com/tell-users-when-youve-updated-your-angular-app-71287d730806?source=rss-cc3df6c928fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/71287d730806</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[continuous-deployment]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <dc:creator><![CDATA[Gareth Erskine-Jones]]></dc:creator>
            <pubDate>Mon, 31 Dec 2018 19:42:24 GMT</pubDate>
            <atom:updated>2018-12-31T19:42:24.096Z</atom:updated>
            <content:encoded><![CDATA[<h3>Let users know when you’ve updated your Angular app</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*-j6iaRIXj9DKItaJW7giMg.jpeg" /><figcaption>Photo by Lorenzo Cafaro from Pexels</figcaption></figure><p>It’s fantastic when a user loves your Angular web site so much they never close it; but what happens if you deploy a new version? Maybe you’ve fixed a bug, or added new features, but some users are still working with an old version in the browser.</p><p>I’ve seen this situation with both testers and with end users, and it can waste a lot of time, because although the user just needs to hit F5 to refresh the page, they don’t necessarily know that they need to do that.</p><p>It would be great if the Angular application itself could detect when a new version had been deployed, so it could advise the user to refresh.</p><p>The approach here is to include in the deployed code a file containing something that changes with each release. This could be a build number, a timestamp, or a hash of the released files.</p><h4>Get your build number on startup</h4><p>I’m using the build number generated by my CI build pipeline. I put this in a JSON file, which I add to the assets folder, which is copied when Angular Cli builds the application. How you populate this file depends on how you choose to build and deploy your app. I use a step in my build pipeline to populate the file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f1c6e9a820a9ea1ba499838f3a2367c/href">https://medium.com/media/9f1c6e9a820a9ea1ba499838f3a2367c/href</a></iframe><p>Next, I add a class to my app to represent the data in the JSON file, and a small service (the BuildDetailsService) which I can inject anywhere, giving access to the data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fd813773077906d41b2a2b194d9bc07/href">https://medium.com/media/4fd813773077906d41b2a2b194d9bc07/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11983714b04cf2cf6f5e2861e03cd9ed/href">https://medium.com/media/11983714b04cf2cf6f5e2861e03cd9ed/href</a></iframe><p>Finally I add a service which requests the build-details.json file, and stores the data in the BuildDetailsService.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56f37d8f3e55cb8664c3998df506e751/href">https://medium.com/media/56f37d8f3e55cb8664c3998df506e751/href</a></iframe><p>This service needs to be registered in the AppModule as an APP_INITIALIZER which ensures that it’ll run before the application is started up.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eeebf9451d657410693b02a923def16b/href">https://medium.com/media/eeebf9451d657410693b02a923def16b/href</a></iframe><p>This all means that when the application is loaded into the browser, it will load and store the build number.</p><h4>Check for updated releases periodically</h4><p>The next step is to have the app occasionally phone home to see if a new version has been deployed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5e6d12f0ee65955451a8daf2f359ea7/href">https://medium.com/media/b5e6d12f0ee65955451a8daf2f359ea7/href</a></iframe><p>The UpToDateBuildService, once started, fetches the build-details.json file from the server at regular intervals (here, I’ve set it to once per minute, but a longer interval might be appropriate. It compares the build number it gets, with the one that was loaded when the page was loaded in the browser. If the numbers are different, then a new version of the application has been deployed, and the verion in the browser is out of date.</p><p>We can use this information to suggest to the user that they need to refresh the page.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0538ce569d852ee086f04854998cedba/href">https://medium.com/media/0538ce569d852ee086f04854998cedba/href</a></iframe><p>A more sophisticated component could suggest to the user that they need to save their work, and could provide a refresh button.</p><p>Hopefully someone will find this useful!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71287d730806" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular on Docker — environment specific settings]]></title>
            <link>https://faun.pub/angular-on-docker-environment-specific-settings-1e92c3ad01e6?source=rss-cc3df6c928fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e92c3ad01e6</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Gareth Erskine-Jones]]></dc:creator>
            <pubDate>Sun, 16 Dec 2018 18:25:50 GMT</pubDate>
            <atom:updated>2024-10-25T07:00:11.476Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Ia2JL_X2ZDIzaZRFa5P9w.jpeg" /></figure><h3>Angular on Docker — environment specific settings</h3><p>(Note: this story was written for an old version of Angular. I have published an updated version here: <a href="https://blog.gsej.co.uk/angular-on-docker/">https://blog.gsej.co.uk/angular-on-docker/</a>)</p><p>Angular applications often have settings which vary depending on which environment they are running in. For example, when running it in production, an application may need to use a different URL to connect to a backend api, compared with a staging environment, or locally on a developer’s machine.</p><p>One solution is to put the settings in environment.ts file, and include an additional environment.{env-name}.ts files for each environment we need. This means we need to specify the environment when we build our application, which isn’t great when we want to deploy an image.</p><p>When the Angular application is deployed as a docker image, we really want the same image be used, regardless of which environment we’re deploying to, so a different solution is needed.</p><p>Firstly, we need to put the settings we need for local development into a json file. I put this file in the assets folder, which gets copied to the dist folder when the project is built (assuming you’re using angular cli).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17e3758b6eb5a55ed23a745e5e9e56f4/href">https://medium.com/media/17e3758b6eb5a55ed23a745e5e9e56f4/href</a></iframe><p>Then we create a class to represent the settings in our Angular application:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5132695556815f7ad55fa44a519ff70a/href">https://medium.com/media/5132695556815f7ad55fa44a519ff70a/href</a></iframe><p>a service which can be injected into any class which needs access to the settings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/946f0f40c4a3ffeb789d6015ecbcab88/href">https://medium.com/media/946f0f40c4a3ffeb789d6015ecbcab88/href</a></iframe><p>and another service to request the settings on application startup:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d19cb889e6665577eadd930a012743f/href">https://medium.com/media/4d19cb889e6665577eadd930a012743f/href</a></iframe><p>This service needs to be registered as an APP_INITIALIZER in the app.module.ts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04e8df73fd7f45edaffe39f2ecd27c02/href">https://medium.com/media/04e8df73fd7f45edaffe39f2ecd27c02/href</a></iframe><p>With this in place, the application should load the settings provided in the settings.json file on startup.</p><p>To vary the settings based on environment, I add an additional file in the assets folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/297e8bbb1744ae4a4ceac0571cdb3f65/href">https://medium.com/media/297e8bbb1744ae4a4ceac0571cdb3f65/href</a></iframe><p>This has the same structure as the settings.json file, but specifies the name of an environment variable, instead of the setting value.</p><p>Then I modify my Dockerfile, so that when the container starts up, it replaces the environment variable names in the template file, with the values of those variables, and overwrites the settings.json file with the result, before starting the webserver.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab417cc8a28af51f8e812d52b4524cb1/href">https://medium.com/media/ab417cc8a28af51f8e812d52b4524cb1/href</a></iframe><p>In this case I’m using the standard nginx image — if you’re using something different you’ll need to make the appropriate changes.</p><p>I use envsubst to perform the variable substitution, and then I startup nginx.</p><p>Now when I start up the container, I can pass in my settings as environment variables.</p><p>So now a single image can be deployed to any number of environments, and the settings varied accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*Piks8Tu6xUYpF4DU" /></figure><p><strong>Join our community Slack and read our weekly Faun topics ⬇</strong></p><figure><a href="https://www.faun.dev/join/?utm_source=medium.com%2Ffaun&amp;utm_medium=medium&amp;utm_campaign=faunmediumbanner"><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oSdFkACJxs5iy1oR" /></a></figure><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e92c3ad01e6" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/angular-on-docker-environment-specific-settings-1e92c3ad01e6">Angular on Docker — environment specific settings</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>