<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>The World Aflame</title>
    <subtitle>The ramblings of one developer (flammable)</subtitle>
    <link href="https://www.andrew-thorburn.com/atom.xml" rel="self" type="application/atom+xml"/>
    <link href="https://www.andrew-thorburn.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2023-08-22T19:35:00+00:00</updated>
    <id>https://www.andrew-thorburn.com/atom.xml</id>
    <entry xml:lang="en">
        <title>The Great Blog Migration</title>
        <published>2023-08-22T19:35:00+00:00</published>
        <updated>2023-08-22T19:35:00+00:00</updated>
        <author>
          <name>Andrew</name>
        </author>
        <link rel="alternate" href="https://www.andrew-thorburn.com/the-great-blog-migration/" type="text/html"/>
        <id>https://www.andrew-thorburn.com/the-great-blog-migration/</id>
        
        <content type="html">&lt;p&gt;I spent a little while this weekend moving this blog from &lt;a href=&quot;https:&#x2F;&#x2F;ghost.org&#x2F;&quot;&gt;Ghost&lt;&#x2F;a&gt; to
&lt;a href=&quot;https:&#x2F;&#x2F;www.getzola.org&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt;, and therefore also moving the hosting from &lt;a href=&quot;https:&#x2F;&#x2F;www.digitalocean.com&#x2F;&quot;&gt;Digital Ocean&lt;&#x2F;a&gt; to
&lt;a href=&quot;https:&#x2F;&#x2F;pages.github.com&#x2F;&quot;&gt;GitHub Pages&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Why? It’s pretty simple, really - I don’t need a server-side process rendering
my blog. Ghost seemed like an interesting platform when I first used it, 
five-ish years ago, but it also appears to have pivoted since then and is 
now focused a lot more on “creators” and “engagement” and so on - providing 
tools for people who have a large audience and are trying to earn money from 
their writing. Indeed, their headline is now “Turn your audience into a 
business.”&lt;&#x2F;p&gt;
&lt;p&gt;Given the number of posts I’ve made (&lt;em&gt;cough&lt;&#x2F;em&gt;), that’s pretty clearly not 
something I have a need for right now, and may not &lt;em&gt;ever&lt;&#x2F;em&gt; need, unless I 
take a sharp right turn into writing as a career.&lt;&#x2F;p&gt;
&lt;p&gt;On top of that, because Ghost requires a NodeJS server-side process, I have 
to deal with the following:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Monthly payments (small, but not zero)&lt;&#x2F;li&gt;
&lt;li&gt;Keeping the VPS up-to-date with security patches&#x2F;etc&lt;&#x2F;li&gt;
&lt;li&gt;Keeping Ghost up-to-date&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;It’s not &lt;em&gt;a lot&lt;&#x2F;em&gt; of work, but it also should be done even if I’m not 
actively posting blog posts, and I’d much prefer something where &lt;em&gt;none&lt;&#x2F;em&gt; of 
the above is necessary. Ghost does provide a &lt;a href=&quot;https:&#x2F;&#x2F;ghost.org&#x2F;pricing&#x2F;&quot;&gt;hosted option&lt;&#x2F;a&gt;, for 
example, but even the lowest tier is US$9&#x2F;month.&lt;&#x2F;p&gt;
&lt;p&gt;So why GitHub Pages? It provides a completely free way to serve this blog 
which meets all of my really very simple needs:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Publish some HTML&lt;&#x2F;li&gt;
&lt;li&gt;Let me use my own domain&lt;&#x2F;li&gt;
&lt;li&gt;HTTPS&lt;&#x2F;li&gt;
&lt;li&gt;No server or platform maintenance&lt;&#x2F;li&gt;
&lt;li&gt;Free or &lt;em&gt;extremely&lt;&#x2F;em&gt; cheap&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Am I over the moon about adding another bit of the web that’s reliant on 
GitHub? Well… no. But since I’m migrating off of Ghost as a platform, I 
need to look for another tool to actually, y’know, make a website.&lt;&#x2F;p&gt;
&lt;p&gt;Obviously, anything that needs to actually run code in response to a request 
is out - so no WordPress, nothing like that. GitHub Pages, &lt;a href=&quot;https:&#x2F;&#x2F;docs.github.com&#x2F;en&#x2F;pages&#x2F;setting-up-a-github-pages-site-with-jekyll&#x2F;about-github-pages-and-jekyll&quot;&gt;by default&lt;&#x2F;a&gt;
is, by default, powered by &lt;a href=&quot;https:&#x2F;&#x2F;jekyllrb.com&#x2F;&quot;&gt;Jekyll&lt;&#x2F;a&gt;. However, I’m not a fan of 
Jekyll just because it’s build in Ruby, and &lt;em&gt;that&lt;&#x2F;em&gt; means having to deal with 
installing Ruby, managing Gems, and god forbid I install another Ruby-based 
tool that needs incompatible Gems or something. And yes, there are tools to 
work around all of that but since I’m not &lt;em&gt;already&lt;&#x2F;em&gt; in the Ruby system, I’d 
really prefer something that’s just a single, drop-in binary.&lt;&#x2F;p&gt;
&lt;p&gt;The other advantage of these tools is that I have everything stored in Git, 
in Markdown, and so migrating elsewhere is just a matter of running the 
&lt;code&gt;generate&lt;&#x2F;code&gt; command of whichever tool I’m using and uploading the resulting 
files to the host. So if GitHub starts looking dodgy, it’s about as easy as 
it can be to migrate.&lt;&#x2F;p&gt;
&lt;p&gt;So the next step, then, was picking the tool I’d use to generate this blog. 
There are plenty of static site generators out there, and it was to some extent
a matter of excluding the ones I didn’t like for increasing arbitrary 
reasons until I had just one left:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Nothing in Ruby, NodeJS, Python, etc - I want a single binary and don’t 
want to wind up in Gem hell or &lt;code&gt;node_modules&lt;&#x2F;code&gt; hell or whatever (this 
excludes a lot already), and I’m probably going to use this on Windows 
quite a bit so the experience should be reasonable there.&lt;&#x2F;li&gt;
&lt;li&gt;Must have basic support for non-blog pages (like an About page)&lt;&#x2F;li&gt;
&lt;li&gt;I’d like to be able to at least pretend that I could enjoy contributing 
to whichever tool I’m using and I’m a bit of a language snob - I love Rust 
and dislike (but don’t hate) GoLang, for example.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The end result of that was that I chose &lt;a href=&quot;https:&#x2F;&#x2F;www.getzola.org&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt;. It’s a static site 
generator, with a single binary and is written in Rust. It’s very simple to 
use, and while it might not be as full-featured as older tools like &lt;a href=&quot;https:&#x2F;&#x2F;jekyllrb.com&#x2F;&quot;&gt;Jekyll&lt;&#x2F;a&gt;
&lt;a href=&quot;https:&#x2F;&#x2F;jekyllrb.com&#x2F;&quot;&gt;jekyll&lt;&#x2F;a&gt;, it’s more than enough for my needs.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Cross-compiling a simple Rust web app</title>
        <published>2018-12-03T15:57:29+00:00</published>
        <updated>2018-12-03T15:57:29+00:00</updated>
        <author>
          <name>Andrew</name>
        </author>
        <link rel="alternate" href="https://www.andrew-thorburn.com/cross-compiling-a-simple-rust-web-app/" type="text/html"/>
        <id>https://www.andrew-thorburn.com/cross-compiling-a-simple-rust-web-app/</id>
        
        <content type="html">&lt;p&gt;As I mentioned in another post, I wrote a simple Rust app to handle the posting of a simple contact form from this blog. If you’re looking for a simple set of instructions, check the &lt;a href=&quot;https:&#x2F;&#x2F;www.andrew-thorburn.com&#x2F;cross-compiling-a-simple-rust-web-app&#x2F;#conclusion&quot;&gt;conclusion&lt;&#x2F;a&gt; - but be aware that you might need to read the rest of the post to understand &lt;em&gt;why&lt;&#x2F;em&gt; these things are needed.&lt;&#x2F;p&gt;
&lt;p&gt;Writing the app itself wasn’t too hard, and I managed to get it working with only minor issues (the biggest issue was noticing that I only had my IP address from my previous ISP whitelisted with Mailgun!)&lt;&#x2F;p&gt;
&lt;p&gt;However, when it came time to deploy it, I ran into some major issues:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;I was deploying to &lt;a href=&quot;https:&#x2F;&#x2F;run.pivotal.io&quot;&gt;PWS&lt;&#x2F;a&gt;, because I work for Pivotal and have a small amount of free credit to use on the platform.&lt;&#x2F;li&gt;
&lt;li&gt;PWS is a hosted version of &lt;a href=&quot;https:&#x2F;&#x2F;pivotal.io&#x2F;platform&#x2F;pivotal-application-service&quot;&gt;Pivotal Application Service&lt;&#x2F;a&gt; (PAS)&lt;&#x2F;li&gt;
&lt;li&gt;PAS runs all applications on a Linux container (by default - there is Windows support, but I’m not interested in that)&lt;&#x2F;li&gt;
&lt;li&gt;PAS builds and supports the container itself - I can’t install any &lt;code&gt;deb&lt;&#x2F;code&gt; files on it, for example.&lt;&#x2F;li&gt;
&lt;li&gt;This means that I need to build a statically-linked binary&lt;&#x2F;li&gt;
&lt;li&gt;I’m doing development on macOS, so I need to cross-compile. If possible, I want to be able to do &lt;em&gt;everything&lt;&#x2F;em&gt; on macOS directly - I don’t want to have to boot a Linux VM just to “cross-compile”, for example.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;So what do I do? Well, there has been a bit written about cross-compiling Rust &lt;a href=&quot;https:&#x2F;&#x2F;chr4.org&#x2F;blog&#x2F;2017&#x2F;03&#x2F;15&#x2F;cross-compile-and-link-a-static-binary-on-macos-for-linux-with-cargo-and-rust&#x2F;&quot;&gt;here&lt;&#x2F;a&gt; and &lt;a href=&quot;http:&#x2F;&#x2F;timryan.org&#x2F;2018&#x2F;07&#x2F;27&#x2F;cross-compiling-linux-binaries-from-macos.html&quot;&gt;here&lt;&#x2F;a&gt;, for example, so let’s give that a go!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;&#x2F;h2&gt;
&lt;p&gt;This was initially done with the dependency line &lt;code&gt;actix-web = { version=&amp;quot;0.7&amp;quot;, features=[&amp;quot;ssl&amp;quot;] }&lt;&#x2F;code&gt;, which will be important later on. Note that it’s &lt;em&gt;not&lt;&#x2F;em&gt; the same &lt;code&gt;feature&lt;&#x2F;code&gt; that I use in my other post, for reasons that should become apparant.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;target&quot;&gt;Target&lt;&#x2F;h2&gt;
&lt;p&gt;We have a choice between &lt;code&gt;x86_64-unknown-linux-gnu&lt;&#x2F;code&gt; and &lt;code&gt;x86_64-unknown-linux-musl&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The former is the GNU compiler, targeting the &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;&quot;&gt;GNU C library&lt;&#x2F;a&gt;, &lt;code&gt;glibc&lt;&#x2F;code&gt;, and only supports (AFAIK) dynamic linking – I’m unsure if this a fundamental limition of how &lt;code&gt;glibc&lt;&#x2F;code&gt; is compiled&#x2F;designed, or if it’s a licencing issue (or similar). In any event, because of our deployment requirements, we need to build a statically-linked binary, for which we should be using &lt;code&gt;x86_64-unknown-linux-musl&lt;&#x2F;code&gt; – this uses the &lt;a href=&quot;https:&#x2F;&#x2F;www.musl-libc.org&quot;&gt;MUSL &lt;code&gt;libc&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, which was designed from the ground up to be (a) compatible with  &lt;code&gt;glibc&lt;&#x2F;code&gt;, and (b) to support static linking.&lt;&#x2F;p&gt;
&lt;p&gt;So the first thing we need to do is install our target&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rustup&lt;&#x2F;span&gt;&lt;span&gt; target add x86_64-unknown-linux-musl
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;compiling&quot;&gt;Compiling&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;first-attempt&quot;&gt;First Attempt&lt;&#x2F;h3&gt;
&lt;p&gt;OK, now that we’ve chosen and installed our target, let’s run the build against that target with &lt;code&gt;cargo build --release --target=x86_64-unknown-linux-musl&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;error: failed to run custom build command for `backtrace-sys v0.1.24`
&lt;&#x2F;span&gt;&lt;span&gt;process didn&amp;#39;t exit successfully: `&#x2F;Users&#x2F;ipsi&#x2F;workspace&#x2F;mailgun-contact-form&#x2F;target&#x2F;release&#x2F;build&#x2F;backtrace-sys-12a3dac99ded15c9&#x2F;build-script-build` (exit code: 101)
&lt;&#x2F;span&gt;&lt;span&gt;--- stdout
&lt;&#x2F;span&gt;&lt;span&gt;TARGET = Some(&amp;quot;x86_64-unknown-linux-musl&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;OPT_LEVEL = Some(&amp;quot;3&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;HOST = Some(&amp;quot;x86_64-apple-darwin&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;CC_x86_64-unknown-linux-musl = None
&lt;&#x2F;span&gt;&lt;span&gt;CC_x86_64_unknown_linux_musl = None
&lt;&#x2F;span&gt;&lt;span&gt;TARGET_CC = None
&lt;&#x2F;span&gt;&lt;span&gt;CC = None
&lt;&#x2F;span&gt;&lt;span&gt;CROSS_COMPILE = None
&lt;&#x2F;span&gt;&lt;span&gt;CFLAGS_x86_64-unknown-linux-musl = None
&lt;&#x2F;span&gt;&lt;span&gt;CFLAGS_x86_64_unknown_linux_musl = None
&lt;&#x2F;span&gt;&lt;span&gt;TARGET_CFLAGS = None
&lt;&#x2F;span&gt;&lt;span&gt;CFLAGS = None
&lt;&#x2F;span&gt;&lt;span&gt;DEBUG = Some(&amp;quot;false&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;running: &amp;quot;musl-gcc&amp;quot; &amp;quot;-O3&amp;quot; &amp;quot;-ffunction-sections&amp;quot; &amp;quot;-fdata-sections&amp;quot; &amp;quot;-fPIC&amp;quot; &amp;quot;-m64&amp;quot; &amp;quot;-static&amp;quot; &amp;quot;-I&amp;quot; &amp;quot;src&#x2F;libbacktrace&amp;quot; &amp;quot;-I&amp;quot; &amp;quot;&#x2F;Users&#x2F;ipsi&#x2F;workspace&#x2F;mailgun-contact-form&#x2F;target&#x2F;x86_64-unknown-linux-musl&#x2F;release&#x2F;build&#x2F;backtrace-sys-4d856653fde87a65&#x2F;out&amp;quot; &amp;quot;-fvisibility=hidden&amp;quot; &amp;quot;-DBACKTRACE_ELF_SIZE=64&amp;quot; &amp;quot;-DBACKTRACE_SUPPORTED=1&amp;quot; &amp;quot;-DBACKTRACE_USES_MALLOC=1&amp;quot; &amp;quot;-DBACKTRACE_SUPPORTS_THREADS=0&amp;quot; &amp;quot;-DBACKTRACE_SUPPORTS_DATA=0&amp;quot; &amp;quot;-DHAVE_DL_ITERATE_PHDR=1&amp;quot; &amp;quot;-D_GNU_SOURCE=1&amp;quot; &amp;quot;-D_LARGE_FILES=1&amp;quot; &amp;quot;-Dbacktrace_full=__rbt_backtrace_full&amp;quot; &amp;quot;-Dbacktrace_dwarf_add=__rbt_backtrace_dwarf_add&amp;quot; &amp;quot;-Dbacktrace_initialize=__rbt_backtrace_initialize&amp;quot; &amp;quot;-Dbacktrace_pcinfo=__rbt_backtrace_pcinfo&amp;quot; &amp;quot;-Dbacktrace_syminfo=__rbt_backtrace_syminfo&amp;quot; &amp;quot;-Dbacktrace_get_view=__rbt_backtrace_get_view&amp;quot; &amp;quot;-Dbacktrace_release_view=__rbt_backtrace_release_view&amp;quot; &amp;quot;-Dbacktrace_alloc=__rbt_backtrace_alloc&amp;quot; &amp;quot;-Dbacktrace_free=__rbt_backtrace_free&amp;quot; &amp;quot;-Dbacktrace_vector_finish=__rbt_backtrace_vector_finish&amp;quot; &amp;quot;-Dbacktrace_vector_grow=__rbt_backtrace_vector_grow&amp;quot; &amp;quot;-Dbacktrace_vector_release=__rbt_backtrace_vector_release&amp;quot; &amp;quot;-Dbacktrace_close=__rbt_backtrace_close&amp;quot; &amp;quot;-Dbacktrace_open=__rbt_backtrace_open&amp;quot; &amp;quot;-Dbacktrace_print=__rbt_backtrace_print&amp;quot; &amp;quot;-Dbacktrace_simple=__rbt_backtrace_simple&amp;quot; &amp;quot;-Dbacktrace_qsort=__rbt_backtrace_qsort&amp;quot; &amp;quot;-Dbacktrace_create_state=__rbt_backtrace_create_state&amp;quot; &amp;quot;-Dbacktrace_uncompress_zdebug=__rbt_backtrace_uncompress_zdebug&amp;quot; &amp;quot;-o&amp;quot; &amp;quot;&#x2F;Users&#x2F;ipsi&#x2F;workspace&#x2F;mailgun-contact-form&#x2F;target&#x2F;x86_64-unknown-linux-musl&#x2F;release&#x2F;build&#x2F;backtrace-sys-4d856653fde87a65&#x2F;out&#x2F;src&#x2F;libbacktrace&#x2F;alloc.o&amp;quot; &amp;quot;-c&amp;quot; &amp;quot;src&#x2F;libbacktrace&#x2F;alloc.c&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;--- stderr
&lt;&#x2F;span&gt;&lt;span&gt;thread &amp;#39;main&amp;#39; panicked at &amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Internal error occurred: Failed to find tool. Is `musl-gcc` installed?
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &#x2F;Users&#x2F;ipsi&#x2F;.cargo&#x2F;registry&#x2F;src&#x2F;github.com-1ecc6299db9ec823&#x2F;cc-1.0.25&#x2F;src&#x2F;lib.rs:2260:5
&lt;&#x2F;span&gt;&lt;span&gt;note: Run with `RUST_BACKTRACE=1` for a backtrace.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;warning: build failed, waiting for other jobs to finish...
&lt;&#x2F;span&gt;&lt;span&gt;error: build failed
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Um. Hmm. That looks bad. &lt;code&gt;error: build failed&lt;&#x2F;code&gt; definitely looks bad.&lt;&#x2F;p&gt;
&lt;p&gt;The primary issue here is towards the end of that output, and is the line&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Internal error occurred: Failed to find tool. Is `musl-gcc` installed?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So what’s happened is that it’s looking for the binary &lt;code&gt;musl-gcc&lt;&#x2F;code&gt;, but it can’t find it. Oh no!&lt;&#x2F;p&gt;
&lt;p&gt;When on macOS, you can sometimes just do &lt;code&gt;brew install &amp;lt;missing_binary&amp;gt;&lt;&#x2F;code&gt;, and everything will be OK. Let’s try that&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; brew install musl-gcc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Error:&lt;&#x2F;span&gt;&lt;span&gt; No available formula with the name &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;musl-gcc&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, I guess not?&lt;&#x2F;p&gt;
&lt;p&gt;As it turns out, there is a a Homebrew tap available at &lt;code&gt;[filosottile&#x2F;musl-cross&#x2F;musl-cross](https:&#x2F;&#x2F;github.com&#x2F;FiloSottile&#x2F;homebrew-musl-cross)&lt;&#x2F;code&gt; - it’s not available by default, as per &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Homebrew&#x2F;homebrew-core&#x2F;pull&#x2F;2974&quot;&gt;this pull request&lt;&#x2F;a&gt;, but it’s simple enough to install&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;brew&lt;&#x2F;span&gt;&lt;span&gt; install filosottile&#x2F;musl-cross&#x2F;musl-cross
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This provides us with a &lt;code&gt;musl-gcc&lt;&#x2F;code&gt; binary that runs on macOS and provides output for Linux - it’s built from &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;richfelker&#x2F;musl-cross-make&quot;&gt;richfelker&#x2F;musl-cross-make&lt;&#x2F;a&gt;, which in turns builds (and patches) &lt;code&gt;musl&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So we’re good now - we have our &lt;code&gt;musl-gcc&lt;&#x2F;code&gt; command, right?&lt;&#x2F;p&gt;
&lt;p&gt;Well… no. In order to provide broad support for multiple targets, all the commands are prefixed with &lt;code&gt;x86_64-linux-musl-&lt;&#x2F;code&gt;, so it’s actually &lt;code&gt;x86_64-linux-musl-gcc&lt;&#x2F;code&gt;, but that’s OK - it’s fairly easy to deal with.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;second-attempt&quot;&gt;Second Attempt&lt;&#x2F;h3&gt;
&lt;p&gt;Having installed the command, let’s try again!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;error: failed to run custom build command for `backtrace-sys v0.1.24`
&lt;&#x2F;span&gt;&lt;span&gt;process didn&amp;#39;t exit successfully: `&#x2F;Users&#x2F;ipsi&#x2F;workspace&#x2F;mailgun-contact-form&#x2F;target&#x2F;release&#x2F;build&#x2F;backtrace-sys-12a3dac99ded15c9&#x2F;build-script-build` (exit code: 101)
&lt;&#x2F;span&gt;&lt;span&gt;--- stdout
&lt;&#x2F;span&gt;&lt;span&gt;TARGET = Some(&amp;quot;x86_64-unknown-linux-musl&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;OPT_LEVEL = Some(&amp;quot;3&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;HOST = Some(&amp;quot;x86_64-apple-darwin&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;CC_x86_64-unknown-linux-musl = None
&lt;&#x2F;span&gt;&lt;span&gt;CC_x86_64_unknown_linux_musl = None
&lt;&#x2F;span&gt;&lt;span&gt;TARGET_CC = None
&lt;&#x2F;span&gt;&lt;span&gt;CC = None
&lt;&#x2F;span&gt;&lt;span&gt;CROSS_COMPILE = None
&lt;&#x2F;span&gt;&lt;span&gt;CFLAGS_x86_64-unknown-linux-musl = None
&lt;&#x2F;span&gt;&lt;span&gt;CFLAGS_x86_64_unknown_linux_musl = None
&lt;&#x2F;span&gt;&lt;span&gt;TARGET_CFLAGS = None
&lt;&#x2F;span&gt;&lt;span&gt;CFLAGS = None
&lt;&#x2F;span&gt;&lt;span&gt;DEBUG = Some(&amp;quot;false&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;running: &amp;quot;musl-gcc&amp;quot; &amp;quot;-O3&amp;quot; &amp;quot;-ffunction-sections&amp;quot; &amp;quot;-fdata-sections&amp;quot; &amp;quot;-fPIC&amp;quot; &amp;quot;-m64&amp;quot; &amp;quot;-static&amp;quot; &amp;quot;-I&amp;quot; &amp;quot;src&#x2F;libbacktrace&amp;quot; &amp;quot;-I&amp;quot; &amp;quot;&#x2F;Users&#x2F;ipsi&#x2F;workspace&#x2F;mailgun-contact-form&#x2F;target&#x2F;x86_64-unknown-linux-musl&#x2F;release&#x2F;build&#x2F;backtrace-sys-4d856653fde87a65&#x2F;out&amp;quot; &amp;quot;-fvisibility=hidden&amp;quot; &amp;quot;-DBACKTRACE_ELF_SIZE=64&amp;quot; &amp;quot;-DBACKTRACE_SUPPORTED=1&amp;quot; &amp;quot;-DBACKTRACE_USES_MALLOC=1&amp;quot; &amp;quot;-DBACKTRACE_SUPPORTS_THREADS=0&amp;quot; &amp;quot;-DBACKTRACE_SUPPORTS_DATA=0&amp;quot; &amp;quot;-DHAVE_DL_ITERATE_PHDR=1&amp;quot; &amp;quot;-D_GNU_SOURCE=1&amp;quot; &amp;quot;-D_LARGE_FILES=1&amp;quot; &amp;quot;-Dbacktrace_full=__rbt_backtrace_full&amp;quot; &amp;quot;-Dbacktrace_dwarf_add=__rbt_backtrace_dwarf_add&amp;quot; &amp;quot;-Dbacktrace_initialize=__rbt_backtrace_initialize&amp;quot; &amp;quot;-Dbacktrace_pcinfo=__rbt_backtrace_pcinfo&amp;quot; &amp;quot;-Dbacktrace_syminfo=__rbt_backtrace_syminfo&amp;quot; &amp;quot;-Dbacktrace_get_view=__rbt_backtrace_get_view&amp;quot; &amp;quot;-Dbacktrace_release_view=__rbt_backtrace_release_view&amp;quot; &amp;quot;-Dbacktrace_alloc=__rbt_backtrace_alloc&amp;quot; &amp;quot;-Dbacktrace_free=__rbt_backtrace_free&amp;quot; &amp;quot;-Dbacktrace_vector_finish=__rbt_backtrace_vector_finish&amp;quot; &amp;quot;-Dbacktrace_vector_grow=__rbt_backtrace_vector_grow&amp;quot; &amp;quot;-Dbacktrace_vector_release=__rbt_backtrace_vector_release&amp;quot; &amp;quot;-Dbacktrace_close=__rbt_backtrace_close&amp;quot; &amp;quot;-Dbacktrace_open=__rbt_backtrace_open&amp;quot; &amp;quot;-Dbacktrace_print=__rbt_backtrace_print&amp;quot; &amp;quot;-Dbacktrace_simple=__rbt_backtrace_simple&amp;quot; &amp;quot;-Dbacktrace_qsort=__rbt_backtrace_qsort&amp;quot; &amp;quot;-Dbacktrace_create_state=__rbt_backtrace_create_state&amp;quot; &amp;quot;-Dbacktrace_uncompress_zdebug=__rbt_backtrace_uncompress_zdebug&amp;quot; &amp;quot;-o&amp;quot; &amp;quot;&#x2F;Users&#x2F;ipsi&#x2F;workspace&#x2F;mailgun-contact-form&#x2F;target&#x2F;x86_64-unknown-linux-musl&#x2F;release&#x2F;build&#x2F;backtrace-sys-4d856653fde87a65&#x2F;out&#x2F;src&#x2F;libbacktrace&#x2F;alloc.o&amp;quot; &amp;quot;-c&amp;quot; &amp;quot;src&#x2F;libbacktrace&#x2F;alloc.c&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;--- stderr
&lt;&#x2F;span&gt;&lt;span&gt;thread &amp;#39;main&amp;#39; panicked at &amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Internal error occurred: Failed to find tool. Is `musl-gcc` installed?
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &#x2F;Users&#x2F;ipsi&#x2F;.cargo&#x2F;registry&#x2F;src&#x2F;github.com-1ecc6299db9ec823&#x2F;cc-1.0.25&#x2F;src&#x2F;lib.rs:2260:5
&lt;&#x2F;span&gt;&lt;span&gt;note: Run with `RUST_BACKTRACE=1` for a backtrace.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;warning: build failed, waiting for other jobs to finish...
&lt;&#x2F;span&gt;&lt;span&gt;error: build failed
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oops. That didn’t work at all. It turns out that we need to tell Cargo (and Rust) about the compiler we’ve just installed.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;cargo-configuration&quot;&gt;Cargo Configuration&lt;&#x2F;h3&gt;
&lt;p&gt;That’s fairly simple - we just need to add a file &lt;code&gt;.cargo&#x2F;config&lt;&#x2F;code&gt; to our project, with the following content&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span&gt;[target.x86_64-unknown-linux-musl]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;linker &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;x86_64-linux-musl-gcc&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Right! And now let’s do that again!&lt;&#x2F;p&gt;
&lt;p&gt;…&lt;&#x2F;p&gt;
&lt;p&gt;It fails again, with the same output (well, actually, quite a lot more, but still - the original error is still there).&lt;&#x2F;p&gt;
&lt;p&gt;Here, we need to add &lt;code&gt;CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc&lt;&#x2F;code&gt; when running Cargo, e.g.,&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CC_x86_64_unknown_linux_musl&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;x86_64-linux-musl-gcc&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cargo&lt;&#x2F;span&gt;&lt;span&gt; build&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --release --target&lt;&#x2F;span&gt;&lt;span&gt;=x86_64-unknown-linux-musl
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I don’t know why, or why this environment is used, who uses it, or where’s it documented (please drop me a note if you know!)&lt;&#x2F;p&gt;
&lt;p&gt;OK, so let’s try that again.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;error: failed to run custom build command for `openssl-sys v0.9.39`
&lt;&#x2F;span&gt;&lt;span&gt;process didn&amp;#39;t exit successfully: `&#x2F;Users&#x2F;ipsi&#x2F;workspace&#x2F;mailgun-contact-form&#x2F;target&#x2F;release&#x2F;build&#x2F;openssl-sys-a4f1f3adb43f2007&#x2F;build-script-main` (exit code: 101)
&lt;&#x2F;span&gt;&lt;span&gt;--- stdout
&lt;&#x2F;span&gt;&lt;span&gt;cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
&lt;&#x2F;span&gt;&lt;span&gt;cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
&lt;&#x2F;span&gt;&lt;span&gt;cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
&lt;&#x2F;span&gt;&lt;span&gt;cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
&lt;&#x2F;span&gt;&lt;span&gt;cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
&lt;&#x2F;span&gt;&lt;span&gt;cargo:rerun-if-env-changed=OPENSSL_DIR
&lt;&#x2F;span&gt;&lt;span&gt;run pkg_config fail: &amp;quot;Cross compilation detected. Use PKG_CONFIG_ALLOW_CROSS=1 to override&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;--- stderr
&lt;&#x2F;span&gt;&lt;span&gt;thread &amp;#39;main&amp;#39; panicked at &amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Could not find directory of OpenSSL installation, and this `-sys` crate cannot
&lt;&#x2F;span&gt;&lt;span&gt;proceed without this knowledge. If OpenSSL is installed and this crate had
&lt;&#x2F;span&gt;&lt;span&gt;trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
&lt;&#x2F;span&gt;&lt;span&gt;compilation process.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Make sure you also have the development packages of openssl installed.
&lt;&#x2F;span&gt;&lt;span&gt;For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;If you&amp;#39;re in a situation where you think the directory *should* be found
&lt;&#x2F;span&gt;&lt;span&gt;automatically, please open a bug at https:&#x2F;&#x2F;github.com&#x2F;sfackler&#x2F;rust-openssl
&lt;&#x2F;span&gt;&lt;span&gt;and include information about your system as well as this message.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    $HOST = x86_64-apple-darwin
&lt;&#x2F;span&gt;&lt;span&gt;    $TARGET = x86_64-unknown-linux-musl
&lt;&#x2F;span&gt;&lt;span&gt;    openssl-sys = 0.9.39
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &#x2F;Users&#x2F;ipsi&#x2F;.cargo&#x2F;registry&#x2F;src&#x2F;github.com-1ecc6299db9ec823&#x2F;openssl-sys-0.9.39&#x2F;build&#x2F;main.rs:269:9
&lt;&#x2F;span&gt;&lt;span&gt;note: Run with `RUST_BACKTRACE=1` for a backtrace.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;warning: build failed, waiting for other jobs to finish...
&lt;&#x2F;span&gt;&lt;span&gt;error: build failed
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It still doesn’t work, but at least we’re progressing! Now it’s complaining about OpenSSL (having successfully compiled the packages it was complaining about previously).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;openssl&quot;&gt;OpenSSL&lt;&#x2F;h3&gt;
&lt;p&gt;This is where I hit an impenetrable wall.&lt;&#x2F;p&gt;
&lt;p&gt;All the other libraries I depend on appear to only require &lt;code&gt;libc&lt;&#x2F;code&gt;, and compile all the other code as part of their build. However, the &lt;code&gt;openssl-sys&lt;&#x2F;code&gt; crate assumes, by default, that OpenSSL has been installed somewhere, and it looks for the header files&#x2F;etc, as you would expect it to do for a pre-compiled library.&lt;&#x2F;p&gt;
&lt;p&gt;However, I’m (a) running on macOS and (b) using MUSL, while any OpenSSL I’m likely to find will be compiled with GNU &lt;code&gt;libc&lt;&#x2F;code&gt; and dynamic linking. I will admit that I didn’t go down this route, of compiling it on my own, partly because OpenSSL is… not trivial to compile.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;openssl&lt;&#x2F;code&gt; crate &lt;em&gt;does&lt;&#x2F;em&gt; support compiling OpenSSL on my behalf, but that didn’t work for me. Initially, I followed the docs, adding &lt;code&gt;openssl = { version = &amp;quot;0.10&amp;quot;, feature = [&amp;quot;vendored&amp;quot;] }&lt;&#x2F;code&gt; to my &lt;code&gt;cargo.toml&lt;&#x2F;code&gt; , but that still results in &lt;code&gt;openssl-sys&lt;&#x2F;code&gt; being built, and I see a warning in the output of &lt;code&gt;warning: unused manifest key: dependencies.openssl.feature&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Turns out that the docs are &lt;em&gt;slightly&lt;&#x2F;em&gt; wrong (it’s fixed in master, but not in the released docs), and it should say &lt;code&gt;openssl = { version = &amp;quot;0.10&amp;quot;, features = [&amp;quot;vendored&amp;quot;] }&lt;&#x2F;code&gt; – &lt;code&gt;features&lt;&#x2F;code&gt;, &lt;strong&gt;not&lt;&#x2F;strong&gt;&lt;code&gt;feature&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;With that changed, it now attempts to compile OpenSSL, but fails with a &lt;em&gt;ton&lt;&#x2F;em&gt; of errors like&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;apps&#x2F;version.o:version.c:(.text.version_main+0x301): more undefined references to `OpenSSL_version&amp;#39; follow
&lt;&#x2F;span&gt;&lt;span&gt;apps&#x2F;version.o: In function `version_main&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;version.c:(.text.version_main+0x321): undefined reference to `BN_options&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;version.c:(.text.version_main+0x337): undefined reference to `RC4_options&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;version.c:(.text.version_main+0x34d): undefined reference to `DES_options&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;version.c:(.text.version_main+0x363): undefined reference to `IDEA_options&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;version.c:(.text.version_main+0x379): undefined reference to `BF_options&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;version.c:(.text.version_main+0x39e): undefined reference to `OpenSSL_version&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;collect2: error: ld returned 1 exit status
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ultimately, my fix for this was to remove my dependency on OpenSSL entirely, replacing it with &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ctz&#x2F;rustls&quot;&gt;Rustls&lt;&#x2F;a&gt;. This is done by setting a feature-flag on &lt;code&gt;actix-web&lt;&#x2F;code&gt;, like so&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;actix-web &lt;&#x2F;span&gt;&lt;span&gt;= { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.7&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;features&lt;&#x2F;span&gt;&lt;span&gt;=[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rust-tls&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;] }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It’s a lot newer than OpenSSL, and doesn’t provide any support for TLS 1.1 and older, or any known-insecure algorithms. As a consequence, it’s a lot simpler than OpenSSL, and is built purely in Rust, so doesn’t require any magic to cross-compile.&lt;&#x2F;p&gt;
&lt;p&gt;I don’t know if it’s considered “production-ready” (there is some discussion about that &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang-nursery&#x2F;wg-net&#x2F;issues&#x2F;40&quot;&gt;here&lt;&#x2F;a&gt;), but it certainly works for me. That said, PAS handles SSL termination for me, so I don’t need to worry about inbound TLS support.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;The ultimate point of this was to explore how I could cross-compile my application. It turns out that is very straightforward, &lt;em&gt;unless&lt;&#x2F;em&gt; you’re using native code, or a dependency that uses native code, in which case you &lt;em&gt;might&lt;&#x2F;em&gt; have issues.&lt;&#x2F;p&gt;
&lt;p&gt;Making a Rust project compile to another platform requires&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Adding the target via &lt;code&gt;rustup&lt;&#x2F;code&gt; – &lt;code&gt;rustup target add x86_64-unknown-linux-musl&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Installing a compiler&#x2F;linker&#x2F;etc for your target – for macOS, we do this with &lt;code&gt;brew install filosottile&#x2F;musl-cross&#x2F;musl-cross&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Tell cargo which binaries to use for cross-compilation – in this case, we just add&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span&gt;[target.x86_64-unknown-linux-musl]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;linker &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;x86_64-linux-musl-gcc&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
to our &lt;code&gt;.cargo&#x2F;config&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;If using dependencies with native libraries, pass the correct compiler&#x2F;linker to &lt;code&gt;cargo&lt;&#x2F;code&gt; via the &lt;code&gt;CC_x86_64_unknown_linux_musl&lt;&#x2F;code&gt; environment varible, or &lt;code&gt;CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc&lt;&#x2F;code&gt; for us.&lt;&#x2F;li&gt;
&lt;li&gt;Avoid OpenSSL! It seems like it’s reasonably complex to compile into a statically-linked application, so if you can use Rustls (or &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;briansmith&#x2F;ring&quot;&gt;Ring&lt;&#x2F;a&gt;, the underlying crypto library), I’d recommend it!
Note that this might be a feature flag on a dependency you use - check your dependency tree carefully!&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Writing a simple Rust app for handling a contact form</title>
        <published>2018-12-03T15:56:07+00:00</published>
        <updated>2018-12-03T15:57:54+00:00</updated>
        <author>
          <name>Andrew</name>
        </author>
        <link rel="alternate" href="https://www.andrew-thorburn.com/writing-a-simple-rust-app-for-handling-a-contact-form/" type="text/html"/>
        <id>https://www.andrew-thorburn.com/writing-a-simple-rust-app-for-handling-a-contact-form/</id>
        
        <content type="html">&lt;h1 id=&quot;what-and-why&quot;&gt;What and Why&lt;&#x2F;h1&gt;
&lt;p&gt;At the time of writing this post, I do not have a simple “Contact Me” page on this site, making it a bit harder for anyone who wants to, to contact me.&lt;&#x2F;p&gt;
&lt;p&gt;Actually making that happen isn’t as simple as it sounds, if I don’t want to just publish my email address on the site (and I’d rather not make it &lt;em&gt;quite&lt;&#x2F;em&gt; that easy for spammers to scrape it).&lt;&#x2F;p&gt;
&lt;p&gt;So what do I do? Well, I figured I would use a service like &lt;a href=&quot;https:&#x2F;&#x2F;www.mailgun.com&quot;&gt;MailGun&lt;&#x2F;a&gt; to send emails to my own address. I don’t have any affinity to MailGun in particular - it just happened to have a free tier.&lt;&#x2F;p&gt;
&lt;p&gt;Now, MailGun requires an API key (or use of SMTP), and, again, I’m not all that keen on publishing my API key where anyone can view it - apart from anything else, it’s &lt;em&gt;really&lt;&#x2F;em&gt; bad security practice.&lt;&#x2F;p&gt;
&lt;p&gt;So, given the requirement of “Send email by calling MailGuns API, but without publishing any secrets on this blog, either openly, or in JavaScript”, I figured that I would write a simple web app to receive the contact form POST, and forward that to MailGun, keeping everything nice and secure (relatively speaking).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;&#x2F;h2&gt;
&lt;p&gt;So, simply put, what do I need the app to do?&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Receive an HTML form &lt;code&gt;POST&lt;&#x2F;code&gt;ed over HTTP(S)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;POST&lt;&#x2F;code&gt; an HTML form over HTTP(S), using the contents of the received form and some static data, like the API key and a “to” email address.&lt;&#x2F;li&gt;
&lt;li&gt;Handle the response, and redirect the user to particular page, using an HTTP &lt;code&gt;303 See Other&lt;&#x2F;code&gt; response code.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;That’s it - this should’t be hard, right?&lt;&#x2F;p&gt;
&lt;h1 id=&quot;writing-the-app&quot;&gt;Writing the app&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;language-choice&quot;&gt;Language choice&lt;&#x2F;h2&gt;
&lt;p&gt;To start with, I had to choose a language - what would it be? GoLang would have been a good choice for this (but I don’t like GoLang all that much - a topic for another post!). Java is what I would normally use, but that requires a ton of memory and ceremony that feels a bit out of place for something this simple. I could probably have used PHP, but I also don’t like PHP. I find getting set up with Python and Ruby to be a pain, and I’m not terribly familiar with either of them, so I ruled those out.&lt;&#x2F;p&gt;
&lt;p&gt;I eventually settled on Rust, primarily because it’s a language that I quite like, and that I want to do more with. This isn’t really the best use-case for it (as we’ll see soon), but it has the advantages of being easier to set up than Ruby and Python (until we get to cross-compilation), and, being a native app, it will by default consume a &lt;em&gt;lot&lt;&#x2F;em&gt; less memory than Java, while being about as performant, if not more so.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;framework-choice&quot;&gt;Framework Choice&lt;&#x2F;h2&gt;
&lt;p&gt;After making the decision to use Rust, I had to decide on a web framework. There are a few of them out there (see &lt;a href=&quot;https:&#x2F;&#x2F;www.arewewebyet.org&quot;&gt;“Are we web yet?” for Rust&lt;&#x2F;a&gt;), and I chose &lt;a href=&quot;https:&#x2F;&#x2F;actix.rs&quot;&gt;Actix Web&lt;&#x2F;a&gt; for a couple of reasons:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;I’ve read about, and played around with it, before&lt;&#x2F;li&gt;
&lt;li&gt;It’s under active development&lt;&#x2F;li&gt;
&lt;li&gt;It uses Tokio &amp;amp; Futures under the hood (for non-blocking&#x2F;async IO)&lt;&#x2F;li&gt;
&lt;li&gt;It seems relatively simple to get set up with.&lt;&#x2F;li&gt;
&lt;li&gt;It has a proper user guide, on top of API documentation.&lt;&#x2F;li&gt;
&lt;li&gt;It includes an HTTP client as well as an HTTP server&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The other choice I would likely have made would have been &lt;a href=&quot;https:&#x2F;&#x2F;rocket.rs&quot;&gt;Rocket&lt;&#x2F;a&gt;, as it seems to be the most mature.&lt;&#x2F;p&gt;
&lt;p&gt;However, for this application, I definitely wanted a non-blocking framework, as 99% of the work will be receiving data, sending data, and waiting for a response.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;getting-started&quot;&gt;Getting started&lt;&#x2F;h2&gt;
&lt;p&gt;Before we get started, you can find the finished application on &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ipsi&#x2F;mailgun-contact-form&#x2F;&quot;&gt;GitHub&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;dependencies&quot;&gt;Dependencies&lt;&#x2F;h3&gt;
&lt;p&gt;A quick note on dependencies - our &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt; file looks like this&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[dependencies]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;actix-web &lt;&#x2F;span&gt;&lt;span&gt;= { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.7&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;features&lt;&#x2F;span&gt;&lt;span&gt;=[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;rust-tls&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;] }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;futures &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;serde &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;serde_derive &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;serde_urlencoded &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.5&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;base64 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.10&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lazy_static &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;env_logger &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.6&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;log &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.4&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;url &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1.7&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We’re using Actix Web, so we obviously pull that in. We need the Futures crate so we can directly use Futures in our code. Serde is used to handle the serialization and deserialization of our Form structs. Base64 is needed for encoding our HTTP basic authentication, Env Logger and Log for logging, and URL for doing basic URL encoding of strings.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;enabling-tls-for-client-and-server&quot;&gt;Enabling TLS for client and server&lt;&#x2F;h4&gt;
&lt;p&gt;Note that &lt;code&gt;actix-web&lt;&#x2F;code&gt; has a feature flag enabled, of &lt;code&gt;rust-tls&lt;&#x2F;code&gt; - as Mailgun exposes their API over HTTPS, we need to make sure our client is TLS-enabled, which it isn’t by default. Our options are one of:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;tls&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ssl&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;rust-tls&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Our ultimate deployment target is Linux (see last section), so using &lt;code&gt;tls&lt;&#x2F;code&gt; or &lt;code&gt;ssl&lt;&#x2F;code&gt; would involve pulling in &lt;code&gt;OpenSSL&lt;&#x2F;code&gt;, and that’s a nightmare to build into a statically-compiled binary with MUSL (see my other post &lt;a href=&quot;https:&#x2F;&#x2F;www.andrew-thorburn.com&#x2F;writing-a-simple-rust-app-for-handling-a-contact-form&#x2F;__GHOST_URL__&#x2F;cross-compiling-a-simple-rust-web-app&quot;&gt;here&lt;&#x2F;a&gt; for more on that!). A far, far easier option is to use &lt;code&gt;rust-tls&lt;&#x2F;code&gt;, which is a pure-Rust implementation of the most modern parts of TLS.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;starting-the-server&quot;&gt;Starting the server&lt;&#x2F;h3&gt;
&lt;p&gt;To get started with the application, we need to start with a main function, and a handler:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;send_form&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;req&lt;&#x2F;span&gt;&lt;span&gt;: Form&amp;lt;FormData&amp;gt;) -&amp;gt; Result&amp;lt;Box&amp;lt;Future&amp;lt;Item = impl Responder, Error = ResponseError&amp;gt;&amp;gt;, actix_web::Error&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    futures::finished(HttpResponse::build(StatusCode::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NO_CONTENT&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;finish&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DEFAULT_PORT&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;static str &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;8088&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DEFAULT_BIND_ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;static str &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;0.0.0.0&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() -&amp;gt; Result&amp;lt;(), Box&amp;lt;Error&amp;gt;&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; bind_address = std::env::var(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;BIND_ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap_or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DEFAULT_BIND_ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; port = std::env::var(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;PORT&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap_or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DEFAULT_PORT&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    info!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Binding to {}:{}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, bind_address, port);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    server::new(|| App::new().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;route&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, http::Method::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;POST&lt;&#x2F;span&gt;&lt;span&gt;, send_form))
&lt;&#x2F;span&gt;&lt;span&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;bind&lt;&#x2F;span&gt;&lt;span&gt;(format!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;amp;bind_address, &amp;amp;port))?
&lt;&#x2F;span&gt;&lt;span&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Ok(())
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will create a handler for any request to &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; (and &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; only!), expecting to receive &lt;code&gt;POST&lt;&#x2F;code&gt; requests whose body is a &lt;code&gt;application&#x2F;x-www-form-urlencoded&lt;&#x2F;code&gt; form (e.g. from an HTML &lt;code&gt;&amp;lt;form&amp;gt;&lt;&#x2F;code&gt;), and return a &lt;code&gt;204&lt;&#x2F;code&gt; response, since we’re not returning any actual content.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-the-client&quot;&gt;Using the client&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;creating-sending-a-request&quot;&gt;Creating &amp;amp; sending a request&lt;&#x2F;h3&gt;
&lt;p&gt;One of the reasons for choosing Actix Web was that in includes an HTTP client by default. Unfortunately, this client is &lt;em&gt;not&lt;&#x2F;em&gt; well-documented, and it was a bit frustrating to use, and to figure out how it interacted with the server, particularly in terms of return types.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing we should do is create a client, and send a request.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;client::post(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HOST&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Authorization&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AUTH_HEADER&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Content-Type&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;application&#x2F;x-www-form-urlencoded&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;form&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;data)?
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that &lt;code&gt;header&lt;&#x2F;code&gt; is defined as&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;K, V&amp;gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;: K, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;: V) -&amp;gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mut Self
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    HeaderName: HttpTryFrom&amp;lt;K&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    V: IntoHeaderValue,
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So both the header name and value need to meet certain requirements - namely, it must be possible to convert the type of the header name into a &lt;code&gt;HeaderName&lt;&#x2F;code&gt; struct, and there must be an implementation of &lt;code&gt;IntoHeaderValue&lt;&#x2F;code&gt; for the type of the value. This is the case for &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;, but *not *for &lt;code&gt;String&lt;&#x2F;code&gt;, hence the conversion of &lt;code&gt;AUTH_HEADER&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Sending the body is, for us, fairly simple - the &lt;code&gt;form()&lt;&#x2F;code&gt; method will convert anything which implements &lt;code&gt;serde::ser::Serialize&lt;&#x2F;code&gt; into an &lt;code&gt;application&#x2F;x-www-form-urlencoded&lt;&#x2F;code&gt;-compatible string. In our case, we have the simple &lt;code&gt;struct&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Serialize)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;MailGunData&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;a str&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;to&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;a str&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;subject&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;a str&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;text&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;a str&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which has a derived serialisation method.&lt;&#x2F;p&gt;
&lt;p&gt;There are other alternatives, such as&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;json&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;body&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;streaming&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;code&gt;json&lt;&#x2F;code&gt; is much the same as &lt;code&gt;form&lt;&#x2F;code&gt; (both require Serde), while &lt;code&gt;body&lt;&#x2F;code&gt; takes any of binary data, streaming data, or an Actix Actor. &lt;code&gt;streaming&lt;&#x2F;code&gt; would be used where you don’t want to store the entire body in-memory.&lt;&#x2F;p&gt;
&lt;p&gt;Calling any of the previous functions ( &lt;code&gt;json&lt;&#x2F;code&gt;, &lt;code&gt;body&lt;&#x2F;code&gt;, &lt;code&gt;form&lt;&#x2F;code&gt;, &lt;code&gt;streaming&lt;&#x2F;code&gt;) will build the &lt;code&gt;ClientRequest&lt;&#x2F;code&gt;, ready for sending.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;handling-the-response&quot;&gt;Handling the response&lt;&#x2F;h3&gt;
&lt;p&gt;Calling &lt;code&gt;send()&lt;&#x2F;code&gt; creates a &lt;code&gt;SendRequest&lt;&#x2F;code&gt; struct – this implements &lt;code&gt;Future&lt;&#x2F;code&gt;, and that’s the primary way to interact with it. In our case&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;.from_err::&amp;lt;ResponseError&amp;gt;()
&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;resp&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;    info!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Received response with status {}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;status&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return if&lt;&#x2F;span&gt;&lt;span&gt; resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;status&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;is_success&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        Either::A(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;create_ok_response&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else if&lt;&#x2F;span&gt;&lt;span&gt; resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;status&lt;&#x2F;span&gt;&lt;span&gt;() == StatusCode::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;UNAUTHORIZED &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Doesn&amp;#39;t return a JSON response on a 401...
&lt;&#x2F;span&gt;&lt;span&gt;        error!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Received a 401 error trying to call MailGun...&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; f = resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;raw_body&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; body = String::from_utf8_lossy(raw_body.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;as_ref&lt;&#x2F;span&gt;&lt;span&gt;()).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;create_err_response&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;body)
&lt;&#x2F;span&gt;&lt;span&gt;        }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;from_err&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        Either::B(Either::A(f))
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; f = resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;json&lt;&#x2F;span&gt;&lt;span&gt;().from_err::&amp;lt;ResponseError&amp;gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;: MailGunErrorResponse| {
&lt;&#x2F;span&gt;&lt;span&gt;            error!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Received an error from MailGun: {}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, body.message);
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;create_err_response&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;body.message)
&lt;&#x2F;span&gt;&lt;span&gt;        }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;from_err&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        Either::B(Either::B(f))
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;or_else&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;err&lt;&#x2F;span&gt;&lt;span&gt;: ResponseError| {
&lt;&#x2F;span&gt;&lt;span&gt;    error!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Received an error processing the request: {}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, err);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;create_err_response&lt;&#x2F;span&gt;&lt;span&gt;(err.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;ResponseError&lt;&#x2F;code&gt; is a &lt;code&gt;struct&lt;&#x2F;code&gt; defined earlier in the file (implementations elided)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Debug)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;ResponseError(String);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;ResponseError { ... }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;std::fmt::Display &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;ResponseError { ... }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;actix_web::ResponseError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;ResponseError { ... }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;From&amp;lt;PayloadError&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;ResponseError { ... }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;From&amp;lt;SendRequestError&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;ResponseError { ... }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;From&amp;lt;JsonPayloadError&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;ResponseError { ... }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h4 id=&quot;error-conversion&quot;&gt;Error Conversion&lt;&#x2F;h4&gt;
&lt;p&gt;The first thing we have to do is apply a conversion for any errors we get out of the response, otherwise our life will be quite awkard - we would be trying to return multiple different error types from this function.&lt;&#x2F;p&gt;
&lt;p&gt;As a quick aside, we have defined a custom error type because it’s not possible to convert between &lt;code&gt;SendRequestError&lt;&#x2F;code&gt; and &lt;code&gt;PayloadError&lt;&#x2F;code&gt; (or any of the other error types we’ll encounter), and it doesn’t seem that there’s a conversion defined for &lt;code&gt;Box&amp;lt;Error&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For example, &lt;code&gt;SendRequest&lt;&#x2F;code&gt; has an associated error type of &lt;code&gt;SendRequestError&lt;&#x2F;code&gt; so if we don’t call &lt;code&gt;from_err&lt;&#x2F;code&gt;, and just call &lt;code&gt;and_then&lt;&#x2F;code&gt; directly, the closure passed to &lt;code&gt;and_then&lt;&#x2F;code&gt; would be expected to return &lt;code&gt;IntoFuture&amp;lt;Error = Self::Error&amp;gt;&lt;&#x2F;code&gt;, or, in this case &lt;code&gt;IntoFuture&amp;lt;Error = SendRequestError&amp;gt;&lt;&#x2F;code&gt;. So if our closure were to call something like &lt;code&gt;resp.body()...&lt;&#x2F;code&gt; (ignoring all the other errors we’d get)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;resp&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;    resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;        futures::finished(body)
&lt;&#x2F;span&gt;&lt;span&gt;    })
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It would be expecting the closure to return &lt;code&gt;IntoFuture&amp;lt;Bytes, SendRequestError&amp;gt;&lt;&#x2F;code&gt;, but we’re returning an &lt;code&gt;FutureResult&amp;lt;Bytes, PayloadError&amp;gt;&lt;&#x2F;code&gt;.  &lt;code&gt;IntoFuture&lt;&#x2F;code&gt; is a &lt;code&gt;trait&lt;&#x2F;code&gt;, and it’s been implemented for &lt;code&gt;FutureResult&lt;&#x2F;code&gt; so we can happily return a &lt;code&gt;FutureResult&lt;&#x2F;code&gt; here – &lt;em&gt;provided&lt;&#x2F;em&gt; the generic types match, which they don’t.&lt;&#x2F;p&gt;
&lt;p&gt;Whereas if we add a &lt;code&gt;.from_err::&amp;lt;ResponseError&amp;gt;()&lt;&#x2F;code&gt; call before the outer-most &lt;code&gt;and_then&lt;&#x2F;code&gt;, like so&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;.from_err::&amp;lt;ResponseError&amp;gt;()
&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;resp&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;    resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;        futures::finished(body)
&lt;&#x2F;span&gt;&lt;span&gt;    })
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This changes things, as the &lt;code&gt;from_err()&lt;&#x2F;code&gt; returns a &lt;code&gt;FromErr&amp;lt;Self, E&amp;gt;&lt;&#x2F;code&gt;, or &lt;code&gt;FromErr&amp;lt;ClientResponse, ResponseError&amp;gt;&lt;&#x2F;code&gt; in our case. Since we have the &lt;code&gt;impl&lt;&#x2F;code&gt; block &lt;code&gt;impl From&amp;lt;SendRequestError&amp;gt; for ResponseError&lt;&#x2F;code&gt;, it’ll automatically convert that for us. In addition, as &lt;code&gt;FromErr&lt;&#x2F;code&gt; implements &lt;code&gt;Future&lt;&#x2F;code&gt;, it also, effectively, returns &lt;code&gt;Future&amp;lt;ClientResponse, ResponseError&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So now we can call &lt;code&gt;and_then&lt;&#x2F;code&gt; on our &lt;code&gt;FromErr&lt;&#x2F;code&gt;, and it’s expecting the closure to return a &lt;code&gt;FutureResult&amp;lt;Bytes, ResponseError&amp;gt;&lt;&#x2F;code&gt;, but it’s actually returning a &lt;code&gt;FutureResult&amp;lt;Bytes, PayloadError&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Again, there’s a simple enough fix here – we just have to add another call to &lt;code&gt;.from_err()&lt;&#x2F;code&gt;, either after calling &lt;code&gt;body()&lt;&#x2F;code&gt;, like &lt;code&gt;resp.body().from_err()&lt;&#x2F;code&gt;, or after calling &lt;code&gt;and_then()&lt;&#x2F;code&gt;, like &lt;code&gt;resp.body().and_then(...).from_err()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It doesn’t matter where we put it, as the closure in the &lt;code&gt;and_then&lt;&#x2F;code&gt; call never returns an error, and thus doesn’t ever change the error type.&lt;&#x2F;p&gt;
&lt;p&gt;If, on the other hand, we change the type of the error in the &lt;code&gt;and_then&lt;&#x2F;code&gt; closure, like so&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;From&amp;lt;Box&amp;lt;dyn Error&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;ResponseError { ... }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;from_err&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; body.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;binary_search&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;() == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        futures::finished::&amp;lt;Bytes, Box&amp;lt;dyn Error&amp;gt;&amp;gt;(body)
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; err: Box&amp;lt;dyn Error&amp;gt; = From::from(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;I&amp;#39;m an error!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;        futures::failed::&amp;lt;Bytes, Box&amp;lt;dyn Error&amp;gt;&amp;gt;(err)
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then, initially, we get a really strange error, stating (amongst other things)&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;error[E0277]: the trait bound `actix_web::error::PayloadError: std::error::Error` is not satisfied                                                                                                                                       
&lt;&#x2F;span&gt;&lt;span&gt;   --&amp;gt; src&#x2F;main.rs:203:57                                                                                                                                                                                                                
&lt;&#x2F;span&gt;&lt;span&gt;    |                                                                                                                                                                                                                                    
&lt;&#x2F;span&gt;&lt;span&gt;203 |             resp.body().from_err().and_then(|body| {                                                                                                                                                          
&lt;&#x2F;span&gt;&lt;span&gt;    |                                    ^^^^^^^^ the trait `std::error::Error` is not implemented for `actix_web::error::PayloadError`                                                                             
&lt;&#x2F;span&gt;&lt;span&gt;    |                                                                                                                                                                                                                                    
&lt;&#x2F;span&gt;&lt;span&gt;    = note: required because of the requirements on the impl of `std::convert::From&amp;lt;actix_web::error::PayloadError&amp;gt;` for `std::boxed::Box&amp;lt;dyn std::error::Error&amp;gt;`                                                                        
&lt;&#x2F;span&gt;&lt;span&gt;    = note: required because of the requirements on the impl of `futures::Future` for `futures::future::FromErr&amp;lt;actix_web::dev::MessageBody&amp;lt;actix_web::client::ClientResponse&amp;gt;, std::boxed::Box&amp;lt;dyn std::error::Error&amp;gt;&amp;gt;` 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This isn’t actually the error I was expecting, by the way!&lt;&#x2F;p&gt;
&lt;p&gt;It appears the reason for this is that the compiler has inferred the return type of &lt;code&gt;from_err()&lt;&#x2F;code&gt; as &lt;code&gt;FromErr&amp;lt;Box&amp;lt;Error&amp;gt;&amp;gt;&lt;&#x2F;code&gt; – which makes sense, as that’s what we’re returning from the closure. The issue here, though, is that &lt;code&gt;PayloadError&lt;&#x2F;code&gt; doesn’t implement the trait &lt;code&gt;Error&lt;&#x2F;code&gt;, so we can’t covert from &lt;code&gt;PayloadError&lt;&#x2F;code&gt; to &lt;code&gt;Box&amp;lt;Error&amp;gt;&lt;&#x2F;code&gt; – &lt;code&gt;Box&amp;lt;Error&amp;gt;&lt;&#x2F;code&gt; only has conversions defined for very generic types. OK, that makes sense!&lt;&#x2F;p&gt;
&lt;p&gt;We can get a step further by explicitly telling &lt;code&gt;from_error&lt;&#x2F;code&gt; which type we’re converting our errors to, like so&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;resp.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;().from_err::&amp;lt;ResponseError&amp;gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;| {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; body.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;binary_search&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;() == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        futures::finished::&amp;lt;Bytes, Box&amp;lt;dyn Error&amp;gt;&amp;gt;(body)
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; err: Box&amp;lt;dyn Error&amp;gt; = From::from(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;I&amp;#39;m an error!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;        futures::failed::&amp;lt;Bytes, Box&amp;lt;dyn Error&amp;gt;&amp;gt;(err)
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us another error message, this time looking like&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;error[E0271]: type mismatch resolving `&amp;lt;futures::FutureResult&amp;lt;bytes::Bytes, std::boxed::Box&amp;lt;dyn std::error::Error&amp;gt;&amp;gt; as futures::IntoFuture&amp;gt;::Error == ResponseError`                                                                     
&lt;&#x2F;span&gt;&lt;span&gt;   --&amp;gt; src&#x2F;main.rs:203:53                                                                                                                                                                                                                
&lt;&#x2F;span&gt;&lt;span&gt;    |                                                                                                                                                                                                                                    
&lt;&#x2F;span&gt;&lt;span&gt;203 |             resp.body().from_err::&amp;lt;ResponseError&amp;gt;().and_then(|body| {                                                                                                                                                              
&lt;&#x2F;span&gt;&lt;span&gt;    |                                                     ^^^^^^^^ expected struct `std::boxed::Box`, found struct `ResponseError`                                                                                                       
&lt;&#x2F;span&gt;&lt;span&gt;    |                                                                                                                                                                                                                                    
&lt;&#x2F;span&gt;&lt;span&gt;    = note: expected type `std::boxed::Box&amp;lt;dyn std::error::Error&amp;gt;`                                                                                                                                                                       
&lt;&#x2F;span&gt;&lt;span&gt;               found type `ResponseError` 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is because, when we call &lt;code&gt;and_then()&lt;&#x2F;code&gt;, the value it returns has the same Error type as the previous struct&#x2F;trait, which in this case is &lt;code&gt;FromErr&amp;lt;Self, ResponseError&amp;gt;&lt;&#x2F;code&gt;, so our only choice in this situation is to return a &lt;code&gt;ResponseError&lt;&#x2F;code&gt; – in all other cases, the compiler &lt;em&gt;will&lt;&#x2F;em&gt; complain. We can’t put a &lt;code&gt;from_err()&lt;&#x2F;code&gt; after the &lt;code&gt;and_then()&lt;&#x2F;code&gt; call because the restriction is on the return type of the closure passed to &lt;code&gt;and_then()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;sending-the-response&quot;&gt;Sending the response&lt;&#x2F;h3&gt;
&lt;p&gt;Creating a response is fairly straightforward – your handler function &lt;em&gt;must&lt;&#x2F;em&gt; return something that implements the &lt;code&gt;Into&amp;lt;Responder&amp;gt;&lt;&#x2F;code&gt;. Two of the most relevant implementions are&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;T: Responder, E: Into&amp;lt;Error&amp;gt;&amp;gt; Responder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;Result&amp;lt;T, E&amp;gt; { ... }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;I, E&amp;gt; Responder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;Box&amp;lt;Future&amp;lt;Item = I, Error = E&amp;gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    I: Responder + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;static&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    E: Into&amp;lt;Error&amp;gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;&amp;#39;static&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    ...
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the first instance, if your handler function returns &lt;code&gt;Result&amp;lt;T, E&amp;gt;&lt;&#x2F;code&gt;, then as long as &lt;code&gt;E&lt;&#x2F;code&gt; can be converted into an &lt;code&gt;Error&lt;&#x2F;code&gt;, either because it implements the trait, or because there’s an &lt;code&gt;impl Into&amp;lt;Error&amp;gt;&lt;&#x2F;code&gt; for it, and as long as &lt;code&gt;T&lt;&#x2F;code&gt; implements &lt;code&gt;Responder&lt;&#x2F;code&gt; directly, you’ll have no issues.&lt;&#x2F;p&gt;
&lt;p&gt;For our purposes, our &lt;code&gt;ResponseError&lt;&#x2F;code&gt; struct has an impl of &lt;code&gt;Error&lt;&#x2F;code&gt;, &lt;code&gt;impl Error for ResponseError { ... }&lt;&#x2F;code&gt;, and we are returning the struct &lt;code&gt;HttpResponse&lt;&#x2F;code&gt;, which implements &lt;code&gt;Responder&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;However, if we &lt;em&gt;just&lt;&#x2F;em&gt; want to return &lt;code&gt;Result&amp;lt;HttpResponse, ResponseError&amp;gt;&lt;&#x2F;code&gt;, then we have to resolve the futures ourselves. That’s rather annoying, so that’s where the second &lt;code&gt;impl&lt;&#x2F;code&gt; mentioned earlier comes into play.&lt;&#x2F;p&gt;
&lt;p&gt;Since &lt;code&gt;Box&amp;lt;Future&amp;lt;I, E&amp;gt;&amp;gt;&lt;&#x2F;code&gt;&lt;em&gt;also&lt;&#x2F;em&gt; implements &lt;code&gt;Responder&lt;&#x2F;code&gt;, we can return a Future from our handler function, and Actix will resolve that for us. Easy!&lt;&#x2F;p&gt;
&lt;p&gt;Hence, the signature for the return type for our handler function is &lt;code&gt;Result&amp;lt;Box&amp;lt;Future&amp;lt;Item = impl Responder, Error = ResponseError&amp;gt;&amp;gt;, actix_web::Error&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Breaking that down, we will return either an &lt;code&gt;actix_web::Error&lt;&#x2F;code&gt; if the function fails to even make it into the Futures-chain (this can only happen if &lt;code&gt;.form(&amp;amp;data)?&lt;&#x2F;code&gt; returns an &lt;code&gt;Err&lt;&#x2F;code&gt;), or we return a Boxed Future, which resolves to &lt;em&gt;something&lt;&#x2F;em&gt; that implements &lt;code&gt;Responder&lt;&#x2F;code&gt;, or it resolves to a &lt;code&gt;ResponseError&lt;&#x2F;code&gt;. Thankfully, we can use &lt;code&gt;impl Responder&lt;&#x2F;code&gt; here, as otherwise the type signature would be a bit… awful.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;OrElse&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;    AndThen&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;        FromErr&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;            SendRequest,
&lt;&#x2F;span&gt;&lt;span&gt;            ResponseError
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        Either&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;            FutureResult&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                HttpResponse,
&lt;&#x2F;span&gt;&lt;span&gt;                ResponseError
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;            Either&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                FromErr&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                    AndThen&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                        MessageBody&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                            ClientResponse
&lt;&#x2F;span&gt;&lt;span&gt;                        &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        FutureResult&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                            HttpResponse,
&lt;&#x2F;span&gt;&lt;span&gt;                            error::PayloadError
&lt;&#x2F;span&gt;&lt;span&gt;                        &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        [closure&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span&gt;src&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;rs:200:46: 203:18]
&lt;&#x2F;span&gt;&lt;span&gt;                    &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    ResponseError
&lt;&#x2F;span&gt;&lt;span&gt;                &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                FromErr&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                    AndThen&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                        FromErr&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                            JsonBody&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                                ClientResponse,
&lt;&#x2F;span&gt;&lt;span&gt;                                MailGunErrorResponse
&lt;&#x2F;span&gt;&lt;span&gt;                            &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                            ResponseError
&lt;&#x2F;span&gt;&lt;span&gt;                        &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        FutureResult&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;                            HttpResponse,
&lt;&#x2F;span&gt;&lt;span&gt;                            ResponseError
&lt;&#x2F;span&gt;&lt;span&gt;                        &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                        [closure&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span&gt;src&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;rs:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;206&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;74&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;209&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;                    &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                    ResponseError
&lt;&#x2F;span&gt;&lt;span&gt;                &amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        [closure@src&#x2F;main.rs:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;194&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;19&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;212&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    FutureResult&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;        HttpResponse,
&lt;&#x2F;span&gt;&lt;span&gt;        ResponseError
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    [closure@src&#x2F;main.rs:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;213&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;18&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;216&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All of that can be replaced with &lt;code&gt;Item = impl Responder&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In any event, the actual response that’s being sent back is simple – create a &lt;code&gt;ResponseBuilder&lt;&#x2F;code&gt; and set the &lt;code&gt;Location&lt;&#x2F;code&gt; header to the URL we’re redirecting the user to. If the request to Mailgun was successful, the redirect includes a URL param of &lt;code&gt;status=success&lt;&#x2F;code&gt;, otherwise it includes two URL params, being &lt;code&gt;status=error&amp;amp;message=...&lt;&#x2F;code&gt;, where &lt;code&gt;message&lt;&#x2F;code&gt; is whatever was returned to us either by Actix, Serde, or Mailgun, depending on where the error occurred.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;futures-either&quot;&gt;&lt;code&gt;futures::Either&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;Note the uses of&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;Either::A(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;create_ok_response&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;Either::B(Either::A(f))
&lt;&#x2F;span&gt;&lt;span&gt;Either::B(Either::B(f))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the three different &lt;code&gt;if&lt;&#x2F;code&gt; branches - because each of them returns a different type (the first one doesn’t a body, while the second takes the body as string, and the third converts the body to JSON – hence none of them have the same type signature), we need some way to sharing the type signature. This is what &lt;code&gt;futures::Either&lt;&#x2F;code&gt; lets us do - it’s an &lt;code&gt;enum&lt;&#x2F;code&gt; that says “we could return &lt;em&gt;either&lt;&#x2F;em&gt; of these future types – if you want to know which, you’ll have to &lt;code&gt;match&lt;&#x2F;code&gt; on the return value” – &lt;em&gt;provided&lt;&#x2F;em&gt; they all have the same &lt;code&gt;Item&lt;&#x2F;code&gt; and &lt;code&gt;Error&lt;&#x2F;code&gt; types.&lt;&#x2F;p&gt;
&lt;p&gt;It’s possible this can be done just by using &lt;code&gt;Box&lt;&#x2F;code&gt;es, but it wasn’t obvious to me how to make all the different types play nicely together!&lt;&#x2F;p&gt;
&lt;p&gt;You can also nest these ever-more deeply if required&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;Either::A(Either::A(Either::A(f)))
&lt;&#x2F;span&gt;&lt;span&gt;Either::A(Either::A(Either::B(f)))
&lt;&#x2F;span&gt;&lt;span&gt;Either::A(Either::B(Either::A(f)))
&lt;&#x2F;span&gt;&lt;span&gt;Either::A(Either::B(Either::B(f)))
&lt;&#x2F;span&gt;&lt;span&gt;Either::B(Either::A(Either::A(f)))
&lt;&#x2F;span&gt;&lt;span&gt;Either::B(Either::A(Either::B(f)))
&lt;&#x2F;span&gt;&lt;span&gt;Either::B(Either::B(Either::A(f)))
&lt;&#x2F;span&gt;&lt;span&gt;Either::B(Either::B(Either::B(f)))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Either::A(Either::A(Either::A(Either::A(f))))
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Though nesting them more than one level deep will drive you completely batty very quickly, and is &lt;em&gt;not&lt;&#x2F;em&gt; recommended (nesting them &lt;em&gt;at all&lt;&#x2F;em&gt; isn’t really recommended).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;deployment&quot;&gt;Deployment&lt;&#x2F;h2&gt;
&lt;p&gt;Now that the application has been written, how do we deploy it?&lt;&#x2F;p&gt;
&lt;p&gt;In my case, I will be deploying it to &lt;a href=&quot;https:&#x2F;&#x2F;run.pivotal.io&quot;&gt;PWS&lt;&#x2F;a&gt;, because I work for Pivotal and have a small amount of free credit to use on the platform. This is built around Linux containers, much like &lt;a href=&quot;https:&#x2F;&#x2F;www.heroku.com&quot;&gt;Heroku&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This means a few main things for us&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We want to compile an &lt;code&gt;x86_64&lt;&#x2F;code&gt; binary, statically compiled, for Linux&lt;&#x2F;li&gt;
&lt;li&gt;PWS handles SSL termination, so we don’t have to worry about that in the application&lt;&#x2F;li&gt;
&lt;li&gt;The simplest way to handle secrets in PWS is to pass them in as environment variables (there are other options, but this is by far the quickest)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The hardest part about this is cross-compiling. Once the binary builds, it deploys without issue! See my &lt;a href=&quot;https:&#x2F;&#x2F;www.andrew-thorburn.com&#x2F;writing-a-simple-rust-app-for-handling-a-contact-form&#x2F;__GHOST_URL__&#x2F;cross-compiling-a-simple-rust-web-app-2&quot;&gt;other guide&lt;&#x2F;a&gt; for instructions on cross-compiling the binary, what steps are required, and &lt;em&gt;why&lt;&#x2F;em&gt; they’re required.&lt;&#x2F;p&gt;
&lt;p&gt;Once you have a cross-compiled binary, deploying it is simple&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;VARS_FILE&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;vars_file_here&amp;gt;.yml&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cf&lt;&#x2F;span&gt;&lt;span&gt; push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; --vars-file &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;VARS_FILE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;VARS_FILE&lt;&#x2F;code&gt; must look something like this&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MAILGUN_API_KEY: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;api_key&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MAILGUN_DOMAIN: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;mailgun_domain&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MAILGUN_TO_ADDRESS: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Name &amp;lt;email@domain.com&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;MAILGUN_REDIRECT_URL: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;https:&#x2F;&#x2F;....&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Presto! You now have a working application, which you can test with a simple cURL command&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;curl -v &lt;&#x2F;span&gt;&lt;span&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -XPOST &lt;&#x2F;span&gt;&lt;span&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -H &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Content-Type: application&#x2F;x-www-form-urlencoded&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -d &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;from_name=Test User&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -d &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;from_email=user@domain.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -d &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title=A test&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; \
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;    -d &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;body=a body&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; \
&lt;&#x2F;span&gt;&lt;span&gt;    https:&#x2F;&#x2F;&amp;lt;deployed_url&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will return a response along the lines of&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;* upload completely sent off: 70 out of 70 bytes
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; HTTP&#x2F;1.1 303 See Other
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; Date: Mon, 03 Dec 2018 15:52:42 GMT
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; Location: https:&#x2F;&#x2F;&amp;lt;redirect_url&amp;gt;?status=success
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; Content-Length: 0
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; Connection: keep-alive
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; 
&lt;&#x2F;span&gt;&lt;span&gt;* Connection #0 to host &amp;lt;deployed_url&amp;gt; left intact
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>English Marginal Tax Rates</title>
        <published>2018-07-11T16:55:00+00:00</published>
        <updated>2018-07-11T16:55:00+00:00</updated>
        <author>
          <name>Andrew</name>
        </author>
        <link rel="alternate" href="https://www.andrew-thorburn.com/english-marginal-tax-rates/" type="text/html"/>
        <id>https://www.andrew-thorburn.com/english-marginal-tax-rates/</id>
        
        <content type="html">&lt;p&gt;This is a look at the English marginal tax rates, and how the default assumption that there are three (Basic, Higher, Additional) isn’t at all correct, and how increasing your pension contribution doesn’t cost you nearly as much as you might think.&lt;&#x2F;p&gt;
&lt;p&gt;Unlike what I suspect most of my posts will be, this isn’t a technical post, more just a place to record some things I have been thinking about.&lt;&#x2F;p&gt;
&lt;p&gt;In most (almost all?) countries, the government applies taxes to your income based upon “&lt;a href=&quot;https:&#x2F;&#x2F;www.cbpp.org&#x2F;research&#x2F;federal-tax&#x2F;policy-basics-marginal-and-average-tax-rates&quot;&gt;progressive, marginal tax rates&lt;&#x2F;a&gt;”. So all income between &lt;code&gt;A&lt;&#x2F;code&gt; and &lt;code&gt;B&lt;&#x2F;code&gt; is taxed at &lt;code&gt;X%&lt;&#x2F;code&gt;, &lt;code&gt;B + 1&lt;&#x2F;code&gt; and &lt;code&gt;D&lt;&#x2F;code&gt; at &lt;code&gt;Y%&lt;&#x2F;code&gt;, &lt;code&gt;D + 1&lt;&#x2F;code&gt; or more at &lt;code&gt;Z%&lt;&#x2F;code&gt;. In the UK, the &lt;a href=&quot;https:&#x2F;&#x2F;www.gov.uk&#x2F;government&#x2F;publications&#x2F;rates-and-allowances-income-tax&#x2F;income-tax-rates-and-allowances-current-and-past#tax-rates-and-bands&quot;&gt;official&lt;&#x2F;a&gt; tax rates (factoring in your personal allowance, which we’ll cover soon) are:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;45% (Addtnl)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Note the names in brackets - the 20% rate is called the “Basic Rate”, the 40% rate is the “Higher Rate”, and the 45% rate is the “Additional Rate”.&lt;&#x2F;p&gt;
&lt;p&gt;This post makes a few simplifying assumptions:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;You haven’t lost your tax-free allowance for any reason (e.g. foreign income taxed on a remittance basis)&lt;&#x2F;li&gt;
&lt;li&gt;You live in England (the Scottish system has different thresholds, for example)&lt;&#x2F;li&gt;
&lt;li&gt;You have a post-2012 English student loan&lt;&#x2F;li&gt;
&lt;li&gt;You’re entitled to exactly zero benefits&lt;&#x2F;li&gt;
&lt;li&gt;We are talking only about the 2018&#x2F;2019 tax year - &lt;em&gt;all&lt;&#x2F;em&gt; the details might be different for any other tax year.&lt;&#x2F;li&gt;
&lt;li&gt;You are employed, pay tax via PAYE, and don’t have to file a self-assessment.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Now, that seems fairly straightforward, right? If you earn £25,000&#x2F;year, you pay £2,630 in tax, which is &lt;code&gt;(25000-11850)*0.2&lt;&#x2F;code&gt;. If you earn £50,000&#x2F;year, you pay £8,360 in tax, which is &lt;code&gt;(46350-11850)*0.2 + (50000-46350)*0.4&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Except… That’s actually not the case. You also have to pay &lt;a href=&quot;https:&#x2F;&#x2F;www.gov.uk&#x2F;government&#x2F;publications&#x2F;rates-and-allowances-national-insurance-contributions&#x2F;rates-and-allowances-national-insurance-contributions&quot;&gt;National Insurance Contributions&lt;&#x2F;a&gt; (also called “NICs”), as does your employer. If you have a student loan, that can be considered a &lt;a href=&quot;https:&#x2F;&#x2F;www.moneysavingexpert.com&#x2F;students&#x2F;student-loans-tuition-fees-changes&#x2F;#gradtax&quot;&gt;graduate tax&lt;&#x2F;a&gt; for this purpose as well. Plus, if you earn over £100,000, your Personal Allowance starts to &lt;a href=&quot;https:&#x2F;&#x2F;www.gov.uk&#x2F;income-tax-rates&#x2F;income-over-100000&quot;&gt;decrease&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to see how the tax is broken down on your income, have a look at &lt;a href=&quot;https:&#x2F;&#x2F;www.uktaxcalculators.co.uk&#x2F;&quot;&gt;UK Tax Calculators&lt;&#x2F;a&gt; - I have no affiliation, I just find it &lt;em&gt;really&lt;&#x2F;em&gt; useful.&lt;&#x2F;p&gt;
&lt;p&gt;So, what does this all work out to?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;personal-allowance&quot;&gt;Personal Allowance&lt;&#x2F;h2&gt;
&lt;p&gt;First, let’s have a look at your Personal Allowance, and how that actually works.&lt;&#x2F;p&gt;
&lt;p&gt;It turns out, the table above was a little misleading - it suggests that the threshold for the Basic Rate is £11,850. That is not the case. Rather, you &lt;em&gt;subtract&lt;&#x2F;em&gt; your personal allowance from your income before paying tax.&lt;&#x2F;p&gt;
&lt;p&gt;So, the table should &lt;em&gt;really&lt;&#x2F;em&gt; look like this:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£     0&lt;&#x2F;td&gt;&lt;td&gt;£34,500&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£34,501&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;45% (Addtnl)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Now, in our first example, that doesn’t really matter, since at £25,000, you’re only being taxed at the basic rate anyway.&lt;&#x2F;p&gt;
&lt;p&gt;In our second example, though, of earning £50,000, you would subtract the personal allowance of £11,850 from your gross income, giving us £38,150. Of that, £3,650 is taxed at the higher rate, and £34,500 is taxed at the basic rate. The end result is the same for anyone earning &amp;lt;£100,000&#x2F;year.&lt;&#x2F;p&gt;
&lt;p&gt;Going forwards, we’ll continue to use the first table, as that’s a lot easier to deal with as all the other thresholds (NI, Student Loan, etc) don’t care about the personal allowance, and most people will only lose their personal allowance when earning over £100,000.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;personal-allowance-reduction&quot;&gt;Personal Allowance Reduction&lt;&#x2F;h2&gt;
&lt;p&gt;Now, let’s factor the cost of losing your Personal Allowance in. You lose £1 of allowance for every £2 of income over £100,000. As we’ve just seen, you’d subtract your personal allowance from your gross income before paying tax. But, because we’re earning over £100,000&#x2F;year, we get less personal allowance to subtract.&lt;&#x2F;p&gt;
&lt;p&gt;What that means is that, if you got a raise of £2&#x2F;year (very stingy) from £100,000 to £100,002, you would expect to pay an extra £0.80 in tax, based on the above table, correct? 40% of the extra income. However, we’ve lost £1 of our personal allowance, which was &lt;em&gt;saving&lt;&#x2F;em&gt; us £0.40 of tax, so we actually have to pay that on top of our £0.80 tax, for a total of £1.20 in tax, or 60%. You can read more about this “60% tax rate” in the Hargreaves Lansdown article “&lt;a href=&quot;http:&#x2F;&#x2F;www.hl.co.uk&#x2F;news&#x2F;articles&#x2F;are-you-caught-in-the-income-tax-trap&quot;&gt;Are you caught in a 60% income tax trap?&lt;&#x2F;a&gt;”. Based on this, we can update our original table:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;60% (?)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;45% (Addtnl)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;As we can see, there is a brief period where your marginal tax rate is 60%, before dropping back down to 40%, and then climbs again to 45%. This isn’t an &lt;em&gt;official&lt;&#x2F;em&gt; marginal tax rate, so it doesn’t have a name like the others.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;employee-national-insurance-contributions&quot;&gt;Employee National Insurance Contributions&lt;&#x2F;h2&gt;
&lt;p&gt;Next, let’s add in your NICs. It can be tricky to figure out exactly what you need to pay, and it depends on what your &lt;a href=&quot;https:&#x2F;&#x2F;www.gov.uk&#x2F;guidance&#x2F;rates-and-thresholds-for-employers-2018-to-2019#class-1-national-insurance-rates&quot;&gt;National Insurance class and category letter&lt;&#x2F;a&gt; are. For these purposes, I will assume that we are talking about Class 1, Category A (which is &lt;em&gt;by far&lt;&#x2F;em&gt; the most common if you’re on PAYE).&lt;&#x2F;p&gt;
&lt;p&gt;The first threshold for NI contributions is £8,424&#x2F;year, and you pay 12% of all your income from £8,424 up to £46,350. From £46,351&#x2F;year, it drops down to 2%. Updating our table with those rates,&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Tax Rate&lt;&#x2F;th&gt;&lt;th&gt;NI Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;60% (?)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;45% (Addtnl)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;This means that all earnings over £11,850 are effectively taxed at 32%, not the listed 20%. That said, since this is a progressive, marginal system, your &lt;em&gt;effective&lt;&#x2F;em&gt; tax rate is much less. We’ll cover a couple more things first, and then look at what your effective tax rate is.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;student-loans&quot;&gt;Student Loans&lt;&#x2F;h2&gt;
&lt;p&gt;As I mentioned earlier, the UK student loan system can be thought of as a “Graduate Tax”. I don’t want to dive into exactly what that means, as there have already been a &lt;em&gt;lot&lt;&#x2F;em&gt; of discussions about why that could be. For now, let’s just assume it is a form of tax, and apply it to our marginal tax rates. We will assume that you have a post-2012 Student Loan, though the pre-2012 loan merely adjusts the threshold, rather than the rate.&lt;&#x2F;p&gt;
&lt;p&gt;With a post-2012 student loan, you repay 9% of all income over £25,000. Simple and straightforward. Adding that to our table,&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Tax Rate&lt;&#x2F;th&gt;&lt;th&gt;NI Rate&lt;&#x2F;th&gt;&lt;th&gt;Student Loan&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£25,000&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£25,001&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;60% (?)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;45% (Addtnl)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h2 id=&quot;combined-tax-rates&quot;&gt;Combined Tax Rates&lt;&#x2F;h2&gt;
&lt;p&gt;Now, this is starting to look a bit complicated. Let’s merge this all together, so we can see what the effective marginal tax rate is at each threshold.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£25,000&lt;&#x2F;td&gt;&lt;td&gt;32%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£25,001&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;41%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;51%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;71%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;51%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;56%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h2 id=&quot;effective-tax-rate&quot;&gt;Effective Tax Rate&lt;&#x2F;h2&gt;
&lt;p&gt;Now, that looks pretty scary - the highest marginal tax rate is 71%! But what’s the &lt;em&gt;effective&lt;&#x2F;em&gt; tax rate at each threshold? That is, what percentage of your total income goes to paying tax, assuming you earn &lt;em&gt;exactly&lt;&#x2F;em&gt; the upper threshold amount? As we’ll see in the following table, it’s not quite as bad as it looks.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£8,425&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;3.47%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£25,000&lt;&#x2F;td&gt;&lt;td&gt;18.48%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£25,001&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;28.85%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;40.73%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;46.53%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;47.32%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;&amp;gt;47.32%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;The absolute limit would be ~51%, if your salary were several &lt;em&gt;million&lt;&#x2F;em&gt; pounds.&lt;&#x2F;p&gt;
&lt;p&gt;Now, all this means is that if you’re earning &lt;em&gt;exactly&lt;&#x2F;em&gt; £46,350&#x2F;year, you will pay 28.85% of that in tax - if you’re earning less, the percentage of your income that you pay will be lower as well.&lt;&#x2F;p&gt;
&lt;p&gt;In any event, this looks a bit nicer - yes, it’s not quite as nice as it just being a flat 20% up to £46,350, but it’s a lot better than paying 41% on everything.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pension-contributions&quot;&gt;Pension Contributions&lt;&#x2F;h2&gt;
&lt;p&gt;There’s one last major quirk, and that’s about your pension. I can only talk about salary sacrifice here, as that’s all I’m familiar with.&lt;&#x2F;p&gt;
&lt;p&gt;If your employer uses salary sacrifice, you would reduce your taxable income by the amount you contribute to your pension. And this reduces the amount of &lt;em&gt;all&lt;&#x2F;em&gt; types of tax you pay - Student Loan, National Insurance, normal tax, etc. It even gives you back some of your basic allowance, if you’re over £100,000.&lt;&#x2F;p&gt;
&lt;p&gt;So, what does this mean in practice?&lt;&#x2F;p&gt;
&lt;p&gt;Well, if you earn, say, £65,000, and you have a student loan, you pay a total of 	£22,887.52 in taxes, giving you a net income of £42,112.48. Now, let’s say you contribute 5% to your pension. That would be £3,250&#x2F;year, which reduces your taxable income to £61,750, at which point you only pay £21,230.02 in taxes. Your net income is now £40,519.98, which is £1,592.50 lower than before you paid in your pension contribution. But you’re contributing £3,250 to your pension!&lt;&#x2F;p&gt;
&lt;p&gt;Because your marginal tax rate is 51% (as we talked about earlier), and because salary sacrifice reduces your taxable income, you are effectively contributing £1,657.5 of taxes to your pension, rather than paying it directly to the government. That’s a pretty good deal. Yes, you’ll have to pay taxes on it when you draw it down after retirement (probably - that gets quite complex), but that’s potentially less than you’re paying now.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;employer-national-insurance-contributions&quot;&gt;Employer National Insurance Contributions&lt;&#x2F;h2&gt;
&lt;p&gt;This is going to be a little controversial, but I think it’s worth looking at. On top of the other taxes, your employer will contribute a percentage of your income as their NICs. Again, the exact amount depends on your letter code and class.&lt;&#x2F;p&gt;
&lt;p&gt;Now, you never see this money, so thinking about it as being a tax on &lt;em&gt;your&lt;&#x2F;em&gt; income isn’t quite accurate. On the other hand, one could argue that if the employer wasn’t paying this contribution, they would be able to pay it you, instead.&lt;&#x2F;p&gt;
&lt;p&gt;This is probably only true in a &lt;em&gt;very&lt;&#x2F;em&gt; limited subset of jobs, where you’re in-demand and hard to replace. It will not be true if you’re being paid minimum wage.&lt;&#x2F;p&gt;
&lt;p&gt;So, let’s assume that you &lt;em&gt;are&lt;&#x2F;em&gt; in a job like that, where your employer might well be willing to pay you their share of NI contributions, if they could.&lt;&#x2F;p&gt;
&lt;p&gt;How would we account for this?&lt;&#x2F;p&gt;
&lt;p&gt;Well, we’re looking at how much of &lt;em&gt;your&lt;&#x2F;em&gt; income is being paid in tax, but as Employer NICs aren’t part of your income it’s a bit tricky to figure out how to account for them. I’m honestly not sure what the best approach is, so I will take the rate for Employer NICs and add it the other marginal rates, unless someone can suggest a better way.&lt;&#x2F;p&gt;
&lt;p&gt;Adding this to our table of marginal rates gives us&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Tax Rate&lt;&#x2F;th&gt;&lt;th&gt;NI Rate&lt;&#x2F;th&gt;&lt;th&gt;Student Loan&lt;&#x2F;th&gt;&lt;th&gt;Employer NI&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;13.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£25,000&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;td&gt;13.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£25,001&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;20% (Basic)&lt;&#x2F;td&gt;&lt;td&gt;12%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;td&gt;13.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;td&gt;13.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;60% (?)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;td&gt;13.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;40% (Higher)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;td&gt;13.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;45% (Addtnl)&lt;&#x2F;td&gt;&lt;td&gt;2%&lt;&#x2F;td&gt;&lt;td&gt;9%&lt;&#x2F;td&gt;&lt;td&gt;13.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Which, when merged together, gives us marginal rates of&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;25.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£25,000&lt;&#x2F;td&gt;&lt;td&gt;45.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£25,001&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;54.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;64.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;84.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;64.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;69.8%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;And effective tax rates of&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lower Threshold&lt;&#x2F;th&gt;&lt;th&gt;Upper Threshold&lt;&#x2F;th&gt;&lt;th&gt;Rate&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;£0&lt;&#x2F;td&gt;&lt;td&gt;£8,424&lt;&#x2F;td&gt;&lt;td&gt;0%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£8,425&lt;&#x2F;td&gt;&lt;td&gt;£11,850&lt;&#x2F;td&gt;&lt;td&gt;7.46%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£11,851&lt;&#x2F;td&gt;&lt;td&gt;£25,000&lt;&#x2F;td&gt;&lt;td&gt;27.63%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£25,001&lt;&#x2F;td&gt;&lt;td&gt;£46,350&lt;&#x2F;td&gt;&lt;td&gt;40.14%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£46,351&lt;&#x2F;td&gt;&lt;td&gt;£100,001&lt;&#x2F;td&gt;&lt;td&gt;53.37%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£100,002&lt;&#x2F;td&gt;&lt;td&gt;£123,700&lt;&#x2F;td&gt;&lt;td&gt;59.39%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£123,701&lt;&#x2F;td&gt;&lt;td&gt;£150,000&lt;&#x2F;td&gt;&lt;td&gt;60.34%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;£150,001&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;td&gt;&amp;gt;60.34%&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;So, once we take Employer NICs into account, the government is receiving tax equivalent to anywhere up to 60% of your income, if you earn enough.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Getting Started</title>
        <published>2018-06-09T20:48:00+00:00</published>
        <updated>2018-06-09T20:48:00+00:00</updated>
        <author>
          <name>Andrew</name>
        </author>
        <link rel="alternate" href="https://www.andrew-thorburn.com/getting-started/" type="text/html"/>
        <id>https://www.andrew-thorburn.com/getting-started/</id>
        
        <content type="html">&lt;p&gt;So of course, the first blog post(s) will be a description of how I got this blog set up (well, this blog + another machine with Concourse on it).&lt;&#x2F;p&gt;
&lt;p&gt;So, here we go!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;powered-by&quot;&gt;Powered by?&lt;&#x2F;h2&gt;
&lt;p&gt;Powered by &lt;a href=&quot;https:&#x2F;&#x2F;ghost.org&#x2F;&quot;&gt;Ghost&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-ghost&quot;&gt;Why Ghost?&lt;&#x2F;h3&gt;
&lt;p&gt;I was looking for something relatively simple, and I’m not a huge fan of Wordpress, for all that it’s a well-known, well-tested platform. I also don’t need all the features that Wordpress provides - Ghost is much closer to what I actually &lt;em&gt;need&lt;&#x2F;em&gt; out of a blogging platform.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hosting&quot;&gt;Hosting&lt;&#x2F;h2&gt;
&lt;p&gt;This blog is hosted on &lt;a href=&quot;https:&#x2F;&#x2F;wwww.digitalocean.com&quot;&gt;Digital Ocean&lt;&#x2F;a&gt;. They have an API, and &lt;a href=&quot;https:&#x2F;&#x2F;www.terraform.io&#x2F;&quot;&gt;Terraform&lt;&#x2F;a&gt; has helpfully built out a &lt;a href=&quot;https:&#x2F;&#x2F;www.terraform.io&#x2F;docs&#x2F;providers&#x2F;do&#x2F;&quot;&gt;provider&lt;&#x2F;a&gt; on top of that API.&lt;&#x2F;p&gt;
&lt;p&gt;Did I have to use Terraform for this? No, of course not. I could have handily configured all of this manually, but I would &lt;em&gt;prefer&lt;&#x2F;em&gt; to have my entire setup stored in Git, so that it’s easy to re-create from scratch, e.g., if my droplet is compromised somehow.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;before-you-start&quot;&gt;Before you start&lt;&#x2F;h3&gt;
&lt;p&gt;You will need to&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Create a Digital Ocean Account.&lt;&#x2F;li&gt;
&lt;li&gt;Create a &lt;a href=&quot;https:&#x2F;&#x2F;cloud.digitalocean.com&#x2F;settings&#x2F;api&#x2F;tokens&quot;&gt;Digital Ocean API key&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Create a &lt;a href=&quot;https:&#x2F;&#x2F;cloud.digitalocean.com&#x2F;settings&#x2F;security&quot;&gt;Digital Ocean SSH key pair&lt;&#x2F;a&gt; (under SSH Keys) and save the private key somewhere secure.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.terraform.io&#x2F;intro&#x2F;getting-started&#x2F;install.html&quot;&gt;Install Terraform&lt;&#x2F;a&gt; on your local machine. Note that Terraform do &lt;em&gt;not&lt;&#x2F;em&gt; have an Ubuntu PPA or the like, so you’ll need to install manually if you’re on Linux.&lt;&#x2F;li&gt;
&lt;li&gt;Install &lt;a href=&quot;https:&#x2F;&#x2F;stedolan.github.io&#x2F;jq&#x2F;&quot;&gt;Jq&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;A domain that you have control enough (enough control to add &lt;code&gt;A&lt;&#x2F;code&gt;, &lt;code&gt;CNAME&lt;&#x2F;code&gt;, and &lt;code&gt;TXT&lt;&#x2F;code&gt; records, at least)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;terraform&quot;&gt;Terraform&lt;&#x2F;h3&gt;
&lt;p&gt;My Terraform config looks like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;resource &amp;quot;digitalocean_droplet&amp;quot; &amp;quot;ghost&amp;quot; {
&lt;&#x2F;span&gt;&lt;span&gt;  image    = &amp;quot;ghost-16-04&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  name     = &amp;quot;ghost&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  region   = &amp;quot;lon1&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  size     = &amp;quot;s-1vcpu-1gb&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  ssh_keys = [1234]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;resource &amp;quot;digitalocean_firewall&amp;quot; &amp;quot;allow-outbound&amp;quot; {
&lt;&#x2F;span&gt;&lt;span&gt;  name = &amp;quot;allow-outbound&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  droplet_ids = [&amp;quot;${digitalocean_droplet.ghost.id}&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  outbound_rule = [
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      protocol                = &amp;quot;icmp&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      port_range              = &amp;quot;1-65535&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      destination_addresses   = [&amp;quot;0.0.0.0&#x2F;0&amp;quot;, &amp;quot;::&#x2F;0&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      protocol                = &amp;quot;tcp&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      port_range              = &amp;quot;1-65535&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      destination_addresses   = [&amp;quot;0.0.0.0&#x2F;0&amp;quot;, &amp;quot;::&#x2F;0&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      protocol                = &amp;quot;udp&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      port_range              = &amp;quot;1-65535&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      destination_addresses   = [&amp;quot;0.0.0.0&#x2F;0&amp;quot;, &amp;quot;::&#x2F;0&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;resource &amp;quot;digitalocean_firewall&amp;quot; &amp;quot;allow-ssh&amp;quot; {
&lt;&#x2F;span&gt;&lt;span&gt;  name = &amp;quot;allow-ssh&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  inbound_rule = [
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      protocol           = &amp;quot;tcp&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      port_range         = &amp;quot;22&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      source_addresses   = &amp;quot;${var.ssh_ips}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  droplet_ids = [&amp;quot;${digitalocean_droplet.ghost.id}&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;resource &amp;quot;digitalocean_firewall&amp;quot; &amp;quot;allow-web-all&amp;quot; {
&lt;&#x2F;span&gt;&lt;span&gt;  name = &amp;quot;allow-web-all&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  droplet_ids = [&amp;quot;${digitalocean_droplet.ghost.id}&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  inbound_rule = [
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      protocol           = &amp;quot;tcp&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      port_range         = &amp;quot;80&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      source_addresses   = [&amp;quot;0.0.0.0&#x2F;0&amp;quot;, &amp;quot;::&#x2F;0&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      protocol           = &amp;quot;tcp&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      port_range         = &amp;quot;443&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      source_addresses   = [&amp;quot;0.0.0.0&#x2F;0&amp;quot;, &amp;quot;::&#x2F;0&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Most of this should be relatively straightfoward, assuming you have a basic understanding of Terraform (it is beyond the scope of this post to provide a Terraform tutorial):&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Create a new Droplet resource called &lt;code&gt;ghost&lt;&#x2F;code&gt;,&lt;&#x2F;li&gt;
&lt;li&gt;Create a firewall rule allowing for inbound SSH traffice from a whitelisted set of IP address&lt;&#x2F;li&gt;
&lt;li&gt;Create a firewall rule allowing for incoming web traffic on ports 80 and 443&lt;&#x2F;li&gt;
&lt;li&gt;Create a firewall rule allowing all outgoing TCP&#x2F;UDP&#x2F;ICMP traffic.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;However, there are a few points that I need to explain in more detail, particularly how you find the various Digital Ocean slugs, as you do &lt;em&gt;not&lt;&#x2F;em&gt; appear to be able to get them from the Web UI.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;ssh-keys&quot;&gt;SSH Keys&lt;&#x2F;h4&gt;
&lt;p&gt;How do you find the value you should put in &lt;code&gt;ssh_keys = [1234]&lt;&#x2F;code&gt;? You’ll need your Digital Ocean API Key, and then we can manually curl the API to get a list of valid IDs:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;curl -s \
&lt;&#x2F;span&gt;&lt;span&gt;    -X GET \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Content-Type: application&#x2F;json&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Authorization: Bearer &amp;lt;do_api_key&amp;gt; \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;https:&#x2F;&#x2F;api.digitalocean.com&#x2F;v2&#x2F;account&#x2F;keys&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    | jq \
&lt;&#x2F;span&gt;&lt;span&gt;    -r \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;#39;.ssh_keys | map(&amp;quot;\(.name)=\(.id)&amp;quot;) | .[]&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will give you something like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Home SSH Key=1234
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the name of the SSH key, and the ID that you will need to use in your Terraform file.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;regions&quot;&gt;Regions&lt;&#x2F;h4&gt;
&lt;p&gt;To get a list of the region slugs you will need for &lt;code&gt; &lt;&#x2F;code&gt;, then you will need to run the following command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;curl -s \
&lt;&#x2F;span&gt;&lt;span&gt;    -X GET \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Content-Type: application&#x2F;json&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Authorization: Bearer &amp;lt;do_api_key&amp;gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;https:&#x2F;&#x2F;api.digitalocean.com&#x2F;v2&#x2F;regions?per_page=999&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    | jq \
&lt;&#x2F;span&gt;&lt;span&gt;    -r \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;#39;([{&amp;quot;name&amp;quot;: &amp;quot;Name&amp;quot;, &amp;quot;slug&amp;quot;: &amp;quot;Slug&amp;quot;}] + (.regions | sort_by(.name))) | map(&amp;quot;\(.name)|\(.slug)&amp;quot;) | .[]&amp;#39; \
&lt;&#x2F;span&gt;&lt;span&gt;    | column -t -s \|
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will give you output like&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Name             Slug
&lt;&#x2F;span&gt;&lt;span&gt;Amsterdam 3      ams3
&lt;&#x2F;span&gt;&lt;span&gt;Bangalore 1      blr1
&lt;&#x2F;span&gt;&lt;span&gt;Frankfurt 1      fra1
&lt;&#x2F;span&gt;&lt;span&gt;London 1         lon1
&lt;&#x2F;span&gt;&lt;span&gt;New York 1       nyc1
&lt;&#x2F;span&gt;&lt;span&gt;New York 3       nyc3
&lt;&#x2F;span&gt;&lt;span&gt;San Francisco 2  sfo2
&lt;&#x2F;span&gt;&lt;span&gt;Singapore 1      sgp1
&lt;&#x2F;span&gt;&lt;span&gt;Toronto 1        tor1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Take the appropriate slug, and add it to your Terraform file.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;droplet-size&quot;&gt;Droplet Size&lt;&#x2F;h4&gt;
&lt;p&gt;To get the slug of the droplet size you will need for &lt;code&gt;size = &amp;quot;s-1vcpu-1gb&amp;quot;&lt;&#x2F;code&gt;, then you will need to run the following command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;curl -s \
&lt;&#x2F;span&gt;&lt;span&gt;    -X GET \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Content-Type: application&#x2F;json&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Authorization: Bearer &amp;lt;do_api_key&amp;gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;https:&#x2F;&#x2F;api.digitalocean.com&#x2F;v2&#x2F;sizes?per_page=999&amp;quot;  \
&lt;&#x2F;span&gt;&lt;span&gt;    | jq \
&lt;&#x2F;span&gt;&lt;span&gt;    -r \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;#39;(.sizes + [{&amp;quot;price_monthly&amp;quot;: &amp;quot;Price&amp;quot;, &amp;quot;memory&amp;quot;: &amp;quot;Memory&amp;quot;, &amp;quot;vcpus&amp;quot;: &amp;quot;VCPU&amp;quot;, &amp;quot;disk&amp;quot;: &amp;quot;Disk&amp;quot;, &amp;quot;slug&amp;quot;: &amp;quot;Slug&amp;quot;, &amp;quot;regions&amp;quot;: []}]) | map(&amp;quot;\(.price_monthly)$|\(.memory)|\(.vcpus)|\(.disk)|\(.slug)|in regions [\(.regions | join(&amp;quot;, &amp;quot;))]&amp;quot;) | .[]&amp;#39; \
&lt;&#x2F;span&gt;&lt;span&gt;    | sort -n \
&lt;&#x2F;span&gt;&lt;span&gt;    | column -t -s \|
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will produce output like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Price$  Memory  VCPU  Disk  Slug            in regions []
&lt;&#x2F;span&gt;&lt;span&gt;5$      1024    1     25    s-1vcpu-1gb     in regions [ams2, ams3, blr1, fra1, lon1, nyc1, nyc2, nyc3, sfo1, sfo2, sgp1, tor1]
&lt;&#x2F;span&gt;&lt;span&gt;5$      512     1     20    512mb           in regions [ams2, ams3, blr1, fra1, lon1, nyc1, nyc2, nyc3, sfo1, sfo2, sgp1, tor1]
&lt;&#x2F;span&gt;&lt;span&gt;10$     1024    1     30    1gb             in regions [ams2, ams3, blr1, fra1, lon1, nyc1, nyc2, nyc3, sfo1, sfo2, sgp1, tor1]
&lt;&#x2F;span&gt;&lt;span&gt;10$     2048    1     50    s-1vcpu-2gb     in regions [ams2, ams3, blr1, fra1, lon1, nyc1, nyc2, nyc3, sfo1, sfo2, sgp1, tor1]
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Find the slug for the desired droplet size (e.g. &lt;code&gt;s-1vcpu-2gb&lt;&#x2F;code&gt;), and insert that into your Terraform script.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;image-slug&quot;&gt;Image Slug&lt;&#x2F;h4&gt;
&lt;p&gt;The image slug that I’m using in &lt;code&gt;image = &amp;quot;ghost-16-04&amp;quot;&lt;&#x2F;code&gt; doesn’t match up terribly well with the description you can find in the Digital Ocean dashboard. You will need to use the API to match up the image name with the image slug, like so:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;curl -s \
&lt;&#x2F;span&gt;&lt;span&gt;    -X GET \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Content-Type: application&#x2F;json&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    -H &amp;quot;Authorization: Bearer &amp;lt;do_api_key&amp;gt;&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;https:&#x2F;&#x2F;api.digitalocean.com&#x2F;v2&#x2F;images?per_page=999&amp;quot; \
&lt;&#x2F;span&gt;&lt;span&gt;    | jq \
&lt;&#x2F;span&gt;&lt;span&gt;    -r \
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;#39;[.images[] | select(.slug != null)] | map(&amp;quot;\(.distribution) \(.name)|\(.slug)|in regions [\(.regions | join(&amp;quot;, &amp;quot;))]&amp;quot;) | .[]&amp;#39; \
&lt;&#x2F;span&gt;&lt;span&gt;    | sort \
&lt;&#x2F;span&gt;&lt;span&gt;    | column -t -s \|
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will produce a list of available slugs, like so:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;CentOS 6.9 x32                          centos-6-x32          in regions [nyc1, sfo1, nyc2, ams2, sgp1, lon1, nyc3, ams3, fra1, tor1, sfo2, blr1]
&lt;&#x2F;span&gt;&lt;span&gt;CentOS 6.9 x64                          centos-6-x64          in regions [nyc1, sfo1, nyc2, ams2, sgp1, lon1, nyc3, ams3, fra1, tor1, sfo2, blr1]
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that not all images are availalable in all regions (e.g. &lt;code&gt;coreos-alpha&lt;&#x2F;code&gt; is &lt;em&gt;not&lt;&#x2F;em&gt; available in &lt;code&gt;sfo2&lt;&#x2F;code&gt; or &lt;code&gt;blr1&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;h4 id=&quot;terraform-variables&quot;&gt;Terraform Variables&lt;&#x2F;h4&gt;
&lt;p&gt;I also have a &lt;code&gt;vars.tf&lt;&#x2F;code&gt; file which looks like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;variable &amp;quot;ssh_ips&amp;quot; {
&lt;&#x2F;span&gt;&lt;span&gt;  type    = &amp;quot;list&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  default = [
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;1.2.3.4&#x2F;32&amp;quot;, # insert actual IP here
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You should replace &lt;code&gt;1.2.3.4&#x2F;32&lt;&#x2F;code&gt; with your own IP address, or with &lt;code&gt;0.0.0.0&#x2F;0&lt;&#x2F;code&gt; if you want to allow SSH logins from anywhere.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;executing&quot;&gt;Executing&lt;&#x2F;h3&gt;
&lt;p&gt;Once the Terraform configuration is up and running, just run &lt;code&gt;terraform plan&lt;&#x2F;code&gt; to see what’s going to happen:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ terraform plan
&lt;&#x2F;span&gt;&lt;span&gt;provider.digitalocean.token
&lt;&#x2F;span&gt;&lt;span&gt;  The token key for API operations.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  Enter a value: &amp;lt;insert_do_api_key&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If everything looks good, run &lt;code&gt;terraform apply&lt;&#x2F;code&gt; to actually make the changes. Wait a little while - maybe 10 minutes - and everything should be done.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;dns&quot;&gt;DNS&lt;&#x2F;h2&gt;
&lt;p&gt;Once you have created your virtual machines, you will need to add DNS records for them.&lt;&#x2F;p&gt;
&lt;p&gt;You can get the IPV4 address out of Terraform like so:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;terraform state show digitalocean_droplet.ghost \
&lt;&#x2F;span&gt;&lt;span&gt;    | grep &amp;#39;ipv4_address&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Make sure to add that to your DNS provider (in my case, that’s &lt;a href=&quot;https:&#x2F;&#x2F;www.namecheap.com&#x2F;&quot;&gt;Namecheap&lt;&#x2F;a&gt;). This could potentially be done through Terraform as well, but I had to wait for my Namecheap API to become active, and I was too impatient for that, so I did it manually.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;next&quot;&gt;Next&lt;&#x2F;h1&gt;
&lt;p&gt;In the next step, I will describe how I configured the server with &lt;a href=&quot;https:&#x2F;&#x2F;www.ansible.com&#x2F;&quot;&gt;Ansible&lt;&#x2F;a&gt;. For the Ghost server, this is limited to configuring &lt;a href=&quot;https:&#x2F;&#x2F;letsencrypt.org&#x2F;&quot;&gt;Let’s Encrypt&lt;&#x2F;a&gt;, but I will delve into how I built a very basic &lt;a href=&quot;https:&#x2F;&#x2F;concourse-ci.org&#x2F;&quot;&gt;Concourse CI&lt;&#x2F;a&gt; server later on.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>First Post</title>
        <published>2018-06-07T14:16:00+00:00</published>
        <updated>2018-06-07T14:16:00+00:00</updated>
        <author>
          <name>Andrew</name>
        </author>
        <link rel="alternate" href="https://www.andrew-thorburn.com/first-post/" type="text/html"/>
        <id>https://www.andrew-thorburn.com/first-post/</id>
        
        <content type="html">&lt;p&gt;Welcome to this blog! The first &lt;em&gt;real&lt;&#x2F;em&gt; post should be coming soon, and will be a guide to how I got this website set up, and why I chose the tools that I did. Or maybe just part one of several - it was a fair amount of work to get set up.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
