<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Articles on Jan Pieter Bruins Slot - Software Engineer | Software Consultant</title>
    <link>https://bruinsslot.jp/articles/</link>
    <description>Recent content in Articles on Jan Pieter Bruins Slot - Software Engineer | Software Consultant</description>
    
    <language>en-us</language>
    <copyright>J.P.H. Bruins Slot</copyright>
    <lastBuildDate>Mon, 29 Nov 2021 08:00:00 +0000</lastBuildDate>
    
        <atom:link href="https://bruinsslot.jp/articles/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Making a simple HTTP webserver in C</title>
      <link>https://bruinsslot.jp/articles/simple-http-webserver-in-c/</link>
      <pubDate>Mon, 29 Nov 2021 08:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/simple-http-webserver-in-c/</guid>
      <description>&lt;h2 id=&#34;intro&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;While reading, and working through &lt;em&gt;Operating Systems: Three Easy
pieces&lt;/em&gt;&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; I started with one of the
assignments for making a webserver concurrent. The webserver was
graciously provided by the authors, and the authors recommend figuring
stuff out yourself, and do some more in-depth research. So, I thought it
would be a cool idea to implement the webserver myself, as the basis for
making it concurrent as the assignment&lt;a href=&#34;#fn2&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref2&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;
asks you to do, and in the process learn more about socket programming.
Additionally, I thought it would be a good learning experience, to
create a post of the process, and share it.&lt;/p&gt;
&lt;p&gt;So, in this post we will go over creating a simple webserver in C,
step-by-step. First, we’ll start with some background into webservers,
and subsequently add more code to our program. I’ve tried to make it so
that you’ll be able to figure out what you need to code before looking
at the results. However, if you just want to look at a specific part or
just at the end result, then check the &lt;em&gt;Implementation&lt;/em&gt; sections
throughout the article. You can also look at the resulting code in this
&lt;a
href=&#34;https://github.com/jpbruinsslot/webserver-c&#34;&gt;repository&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So, let’s get to it!&lt;/p&gt;
&lt;h2 id=&#34;basics&#34;&gt;Basics&lt;/h2&gt;
&lt;p&gt;First, it might be good idea to get a feel of what we’re trying to
create, to investigate what it is that we actually want to make. So,
let’s figure out what a webserver actually is, and does.&lt;/p&gt;
&lt;h3 id=&#34;webserver&#34;&gt;webserver&lt;/h3&gt;
&lt;p&gt;A webserver, and in this case on the software side is able to satisfy
client requests over HTTP, and other several related protocols. Its
primary function is to store, process, deliver files to that client. At
minimum this is an HTTP Server, which is a piece of software that
understand URLs (Universal Resource Locator) and HTTP (Hypertext
Transfer Protocol).&lt;/p&gt;
&lt;p&gt;A webserver can be either be, or a combination of, a &lt;em&gt;static
webserver&lt;/em&gt; which simply serves files “as-is”. Or, a &lt;em&gt;dynamic
webserver&lt;/em&gt; in which the webserver runs an executable file on the
webserver, and the output is returned to the client. It is dynamic
because the webserver updates the hosted files before sending them to
the client it does this “on-the-fly”.&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref3&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;http&#34;&gt;HTTP&lt;/h3&gt;
&lt;p&gt;As mentioned above a webserver in our case should be able to
understand the HTTP protocol. What does a protocol mean in this context?
Well, it is a set of rules for communication between two computers. In
this case it specifies how to transfer hyper text documents, meaning
documents that are interconnected by hyperlinks. The protocol is textual
and stateless. Textual because that all the commands are plain text, and
you’ll be able to read and inspect it. Stateless, because that neither
the client nor server remembers previous communications.&lt;a href=&#34;#fn4&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref4&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It means on the client side the application (for instance a web
browser) needs to speak the same ‘language’ as the webserver in order to
communicate. The ‘language’ that is used is HTTP.&lt;/p&gt;
&lt;p&gt;A message is constructed that is either a request or a response from
either the client or the server. This message needs to be transported,
and that is where TCP comes in.&lt;/p&gt;
&lt;h3 id=&#34;tcp&#34;&gt;TCP&lt;/h3&gt;
&lt;p&gt;HTTP presumes an underlying transport layer protocol to establish
host-to-host data transfer channels, and manage the data exchange in a
client-to-server or peer-to-peer networking model. The protocol that is
commonly used for HTTP server is TCP (Transmission Control Protocol),
but it can also be adapted to be used with for instance UDP (User
Datagram Protocol). However, because of RFC 2616&lt;a href=&#34;#fn5&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref5&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt;
that states that the transport layer should be reliable, we will be
using TCP instead of UDP.&lt;a href=&#34;#fn6&#34; class=&#34;footnote-ref&#34; id=&#34;fnref6&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt;&lt;a href=&#34;#fn7&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref7&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;TCP maintains communications between application processes between
hosts (client and server), and they use port numbers to track sessions.
HTTP, and TCP protocols are part of a suite of several multiple
protocols on each layer of the request/response cycle. This suite is
also known as: TCP/IP.&lt;/p&gt;
&lt;h3 id=&#34;internet-protocol-suite-a.k.a.-tcpip&#34;&gt;Internet Protocol Suite
a.k.a. TCP/IP&lt;/h3&gt;
&lt;p&gt;HTTP is part of the &lt;em&gt;Internet Protocol Suite&lt;/em&gt; and it is called
an application layer protocol. The &lt;em&gt;Internet Protocol Suite&lt;/em&gt; is a
model that is commonly known as TCP/IP because of the foundational
protocols that make up the &lt;em&gt;Internet Protocol Suite&lt;/em&gt;. Namely, the
&lt;em&gt;Transmission Control Protocol&lt;/em&gt; (TCP, present on the transport
layer), and &lt;em&gt;Internet Protocol&lt;/em&gt; (IP, present on the internet
layer). &lt;a href=&#34;#fn8&#34; class=&#34;footnote-ref&#34; id=&#34;fnref8&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;8&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This suite is a conceptual model, and it consists out of set of
protocols used in internet, and similar computer networks. It specifies
how data should be packetized, addressed, transmitted, routed and
received. The model is made up of 4 abstraction layers: the application,
transport, internet and link layer.&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ APPLICATION LAYER                ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ http, ftp, smtp, ssh, etc.       │
└──────────────────────────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ TRANSPORT LAYER                  ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ tcp, udp, etc.                   │
└──────────────────────────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ INTERNET LAYER                   ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ ipv4, ipv6, etc.                 │
└──────────────────────────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ LINK LAYER                       ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ arp, mac (ethernet, wifi, etc.)  │
└──────────────────────────────────┘&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We stated above that: &lt;em&gt;“HTTP presumes an underlying transport
layer”&lt;/em&gt;. From the Internet Protocol Suite we can see that there are
several available, and we already stated that TCP is commonly used. We
can also see that the HTTP is in the application layer, next to other
protocols that you might be familiar with such as FTP, Telnet, SSH,
SMTP, etc. In essence HTTP is layered over TCP and uses it to transport
its message data. In turn TCP is layered over IP, to make sure it ends
up at the right location.&lt;/p&gt;
&lt;p&gt;When data to the application layer is received, by which every
program you are using (in the case of HTTP, a browser for instance), it
talks to the transport layer through a port. Each port can be assigned
to a different protocol in the application layer. In the case of HTTP
this is port 80, so that TCP knows where the data is coming from.&lt;/p&gt;
&lt;h3 id=&#34;request-response&#34;&gt;Request / Response&lt;/h3&gt;
&lt;p&gt;As mentioned above: &lt;em&gt;“HTTP is a set of rules for
communication”&lt;/em&gt;. These rules are implemented in the request and
response messages. You’re probably already familiar with its structure.
The request message consist out of the following: a request line, the
request header fields, an empty line, and an optional message body. In
the following diagram you can see how a request and response message is
built up when we access the webserver we are going to create using
curl.&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                                        
            ┌─┐                                        ┌─┐              
            └┬┘              ―――――――――――――▶            ╞ │              
            ▔▔▔              ◀―――――――――――――            └─┘              
    client request message                    server response message   
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓              ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ GET / HTTP/1.1            ┃ request line ┃ HTTP/1.0 200 OK           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━┩              ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Host: localhost:8080      │    headers   │ Server: webserver-c       │
│ User-Agent: curl/7.52.1   │              │ Content-type: text/html   │
│ Accept: */*               │              │                           │
└───────────────────────────┘              ├───────────────────────────┤
                                  body     │ &lt;html&gt;hello, world&lt;/html&gt; │
                                           └───────────────────────────┘&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can even inspect what curl is sending and receiving, and we can
see that everything is just in plain text. Pretty cool!&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ curl -vs http://localhost:8080

* Rebuilt URL to: http://localhost:8080/
*   Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
&gt; GET / HTTP/1.1
&gt; Host: localhost:8080
&gt; User-Agent: curl/7.52.1
&gt; Accept: */*
&gt;
* HTTP 1.0, assume close after body
&lt; HTTP/1.0 200 OK
&lt; Server: webserver-c
&lt; Content-type: text/html
&lt;
&lt;html&gt;hello, world&lt;/html&gt;
* Curl_http_done: called premature == 0
* Closing connection 0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When a request is made by the application layer, the message passes
through the layers on one side, and back up through layer on the other
side (represented by the solid line). Logically, one layer talks to the
corresponding layer at the other side (represented by the dashed
line).&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                         
      ┌─┐                       ┌─┐      
      └┬┘                       ╞ │      
      ▔▔▔                       └─┘      
     client                    server    
┏━━━━━━━━━━━━━┓╷╷       ╭╮┏━━━━━━━━━━━━━┓
┃ APPLICATION ┃││ ◀---▶ ││┃ APPLICATION ┃
┗━━━━━━━━━━━━━┛││       ││┗━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━┓│▲       ▼│┏━━━━━━━━━━━━━┓
┃ TRANSPORT   ┃││ ◀---▶ ││┃ TRANSPORT   ┃
┗━━━━━━━━━━━━━┛││       ││┗━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━┓││       ││┏━━━━━━━━━━━━━┓
┃ INTERNET    ┃││ ◀---▶ ││┃ INTERNET    ┃
┗━━━━━━━━━━━━━┛▼│       │▲┗━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━┓││       ││┏━━━━━━━━━━━━━┓
┃ LINK        ┃││ ◀---▶ ││┃ LINK        ┃
┗━━━━━━━━━━━━━┛│╰───────╯│┗━━━━━━━━━━━━━┛
               ╰─────────╯               &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, to recap: we’re creating an application (a webserver) that is
able to receive/send plain text messages that adhere to rules of HTTP.
Those messages are received through a transport layer. Our application
will use TCP as this transport layer, and we need to implement that
transport layer of HTTP. Both of these protocols are part of the
&lt;em&gt;Internet Protocol Suite&lt;/em&gt; (TCP/IP) and is provided by the
operating system.&lt;a href=&#34;#fn9&#34; class=&#34;footnote-ref&#34; id=&#34;fnref9&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;9&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;implementation&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;We can use the man pages to reference on how to start implementing
this. Throughout this article we will be using man pages to get all the
information we need to implement our webserver. The first man page we
can look at is:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 7 tcp

NAME
       tcp - TCP protocol

SYNOPSIS
       #include &lt;sys/socket.h&gt;
       #include &lt;netinet/in.h&gt;
       #include &lt;netinet/tcp.h&gt;

       tcp_socket = socket(AF_INET, SOCK_STREAM, 0);

DESCRIPTION

       ...

       A newly created TCP socket has no remote or local address and is
       not fully specified. To create an outgoing TCP connection use
       connect(2) to establish a connection to another TCP socket. To
       receive new incoming connections, first bind(2) the socket to a
       local address and port and then call listen(2) to put the socket
       into the listening state. After that a new socket for each
       incoming connection can be accepted using accept(2). A socket
       which has had accept(2) or connect(2) successfully called on it
       is fully specified and may transmit data. Data cannot be
       transmitted on listening or not yet connected sockets.  

       ...&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; The &lt;code&gt;7&lt;/code&gt; stands for the section
number the page is from, and you can check what section it is by typing
&lt;code&gt;man man&lt;/code&gt;. Typically, man pages referred to using the
notation &lt;code&gt;name(section)&lt;/code&gt;, since the same name can be present
in different sections. Throughout this document we will use this
notation so that you’ll be able to inspect the man pages. If you’re
trying to find a specific man page, you can use the
&lt;code&gt;apropos {name}&lt;/code&gt; command to find name usage through the man
pages.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;From this man page we can read that we need to implement a ‘socket’
on which we can ‘listen’ for incoming connections, then we need to
‘bind’ the socket to a local address, and port. Then put the socket in a
‘listen’ state. After that we’re able to ‘accept’ incoming connections,
for each accepted connection a new socket will be created, and we will
be able to read and write to this socket. The following diagram gives a
bit of an overview of what we need to implement.&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                                                    
┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━━━┓
┃   SOCKET   ┃ ▶ ┃    BIND    ┃ ▶ ┃   LISTEN   ┃ ▶ ┃   ACCEPT   ┃ ▶ ┃  READ/WRITE  ┃
┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━━━┛&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;tcp(7)&lt;/code&gt; man page also states, as we’ve uncovered from
above, that it is layered upon &lt;code&gt;ip(7)&lt;/code&gt;, so let’s also take a
look at the man page for that.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 7 ip

NAME
       ip - Linux IPv4 protocol implementation

SYNOPSIS
       #include &lt;sys/socket.h&gt;
       #include &lt;netinet/in.h&gt;
       #include &lt;netinet/ip.h&gt; /* superset of previous */

       tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
       udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
       raw_socket = socket(AF_INET, SOCK_RAW, protocol);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, cool! This gives us some more information on how to create
sockets using other protocols, like udp, and raw. We’ll keep that in
mind for further on in this article. But first let’s start with setting
up our project.&lt;/p&gt;
&lt;h2 id=&#34;setting_up&#34;&gt;Setting up&lt;/h2&gt;
&lt;p&gt;First, let’s start with setting up our environment in which we want
to develop. We will start very simple, just to make sure everything is
working:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb8&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb8-1&#34;&gt;&lt;a href=&#34;#cb8-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// ./steps/step000.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb8-2&#34;&gt;&lt;a href=&#34;#cb8-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb8-3&#34;&gt;&lt;a href=&#34;#cb8-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb8-4&#34;&gt;&lt;a href=&#34;#cb8-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb8-5&#34;&gt;&lt;a href=&#34;#cb8-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;hello, world&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb8-6&#34;&gt;&lt;a href=&#34;#cb8-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb8-7&#34;&gt;&lt;a href=&#34;#cb8-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next we need to compile it, and we will be using
&lt;code&gt;gcc&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb9&#34;&gt;&lt;pre
class=&#34;sourceCode bash&#34;&gt;&lt;code class=&#34;sourceCode bash&#34;&gt;&lt;span id=&#34;cb9-1&#34;&gt;&lt;a href=&#34;#cb9-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; gcc &lt;span class=&#34;at&#34;&gt;-Wall&lt;/span&gt; webserver.c &lt;span class=&#34;at&#34;&gt;-o&lt;/span&gt; webserver&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s check if every worked.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb10&#34;&gt;&lt;pre
class=&#34;sourceCode bash&#34;&gt;&lt;code class=&#34;sourceCode bash&#34;&gt;&lt;span id=&#34;cb10-1&#34;&gt;&lt;a href=&#34;#cb10-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; ./webserver&lt;/span&gt;
&lt;span id=&#34;cb10-2&#34;&gt;&lt;a href=&#34;#cb10-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;hello,&lt;/span&gt; world&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Ok, now that we’re set up. Lets get started with implementing our
socket.&lt;/p&gt;
&lt;h2 id=&#34;implement_socket&#34;&gt;Implement the socket&lt;/h2&gt;
&lt;p&gt;From what we’ve read from the man page &lt;code&gt;tcp(7)&lt;/code&gt;, we need
to implement a tcp socket. But, let’s also inspect what a socket is, and
we can also use the man pages for this.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man socket

NAME
       socket - create an endpoint for communication

SYNOPSIS
       #include &lt;sys/types.h&gt;          /* See NOTES */
       #include &lt;sys/socket.h&gt;

       int socket(int domain, int type, int protocol);

DESCRIPTION
       socket() creates an endpoint for communication and returns a file
       descriptor that refers to that endpoint. The file descriptor returned by
       a successful call will be the lowest-numbered file descriptor not
       currently open for the process.
       ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, a socket creates an endpoint for communication. Furthermore, we
can read that we need to include the &lt;code&gt;&lt;sys/socket.h&gt;&lt;/code&gt;
header file, and that we can create a socket endpoint to communicate by
using the function: &lt;code&gt;socket(2)&lt;/code&gt;. This function returns a file
descriptor which is an integer. The arguments that it accepts are:
&lt;code&gt;domain&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;protocol&lt;/code&gt;. We will
look at the individual arguments and investigate how they need to be set
in the following sections.&lt;/p&gt;
&lt;h3 id=&#34;domain&#34;&gt;domain&lt;/h3&gt;
&lt;p&gt;The argument &lt;code&gt;domain&lt;/code&gt; is an integer that specifies a
communication domain, and it selects the protocol family that which will
be used for communication. These families are defined in
&lt;code&gt;&lt;sys/socket.h&gt;&lt;/code&gt; these families are defined as
constants in the header file, and we can reference them by their name
and use them as the domain argument.&lt;/p&gt;
&lt;p&gt;See the man page for an overview of what kind formats you’re able to
choose. Since we’re creating a webserver that uses TCP we will be using
&lt;code&gt;AF_INET&lt;/code&gt;, which uses the IPv4 Internet protocols.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; Now, I wanted to know how this header file
looked like, and I was able to inspect it further by installing the
POSIX man page. These are described as: “Manual pages about using a
POSIX system for development”. And will give more information about the
specification of a C standard library for POSIX systems. It’s a
specification for a number of routines that should be available in a
basic C standard library, and it depends on how this standard C library
is implemented on a system. The most commonly used implementation on
Linux is the GNU C Library: &lt;a
href=&#34;http://www.gnu.org/software/libc/&#34;&gt;&lt;code&gt;glibc&lt;/code&gt;&lt;/a&gt;. With
these manpages we can thus reference the specification.&lt;a href=&#34;#fn10&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref10&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;/a&gt;
You can also find these pages on the following site: &lt;a
href=&#34;https://pubs.opengroup.org/onlinepubs/9699919799/idx/head.html&#34;&gt;The
Open Group&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;How I installed the posix man pages on a debian based distro of
linux:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb12&#34;&gt;&lt;pre
class=&#34;sourceCode bash&#34;&gt;&lt;code class=&#34;sourceCode bash&#34;&gt;&lt;span id=&#34;cb12-1&#34;&gt;&lt;a href=&#34;#cb12-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; sudo apt install manpages-posix-dev&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And was able to reference the man page for
&lt;code&gt;&lt;sys/socket.h&gt;&lt;/code&gt; with the following command:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man sys_socket.h&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;h3 id=&#34;type&#34;&gt;type&lt;/h3&gt;
&lt;p&gt;The argument &lt;code&gt;type&lt;/code&gt; specifies the ‘communication
semantics’. So, which socket type do we need to use here? Well, we said
we wanted to create a TCP webserver, so which of the options resembles
that? Let’s refer back to man pages of &lt;code&gt;tcp(7)&lt;/code&gt;, and
&lt;code&gt;ip(7)&lt;/code&gt;. There we can see that the valid socket type for a
TCP socket is &lt;code&gt;SOCK_STREAM&lt;/code&gt;. &lt;code&gt;SOCK_STREAM&lt;/code&gt; is a
full-duplex byte stream, and it is characterized as a type that ensures
that data is not lost or duplicated.&lt;/p&gt;
&lt;h3 id=&#34;protocol&#34;&gt;protocol&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;protocol&lt;/code&gt;, according to the man page, is the particular
protocol to be used with the socket. It is common that there exists only
one protocol that will support a specific socket type. In our case where
we are choosing &lt;code&gt;SOCK_STREAM&lt;/code&gt; as the &lt;code&gt;type&lt;/code&gt;, and
as stated by the &lt;code&gt;man 7 ip&lt;/code&gt; man page, &lt;code&gt;protocol&lt;/code&gt;
is the IP protocol in the IP header to be received or sent. And in this
case of creating a TCP socket were the valid value is &lt;code&gt;0&lt;/code&gt; for
TCP sockets.&lt;/p&gt;
&lt;h3 id=&#34;return-value&#34;&gt;return value&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;int socket(int domain, int type, int protocol)&lt;/code&gt;
function returns an integer which is an file descriptor for the
socket.&lt;a href=&#34;#fn11&#34; class=&#34;footnote-ref&#34; id=&#34;fnref11&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;11&lt;/sup&gt;&lt;/a&gt; The file descriptor is an unique
number that identifies an open file, in this case this is our socket and
just as a regular file we will be able to read and write to it. When an
error occurred it will return the value &lt;code&gt;-1&lt;/code&gt;, it will also
set an &lt;code&gt;errno&lt;/code&gt; which we can use to properly handle
errors.&lt;/p&gt;
&lt;h3 id=&#34;errno&#34;&gt;errno&lt;/h3&gt;
&lt;p&gt;From the last section the return value of the &lt;code&gt;socket&lt;/code&gt;
function the &lt;code&gt;errno&lt;/code&gt; will be set. So what is this
&lt;code&gt;errno&lt;/code&gt;? Let’s check if there is a man page about it.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man errno

NAME
       errno - number of last error

SYNOPSIS
       #include &lt;errno.h&gt;

DESCRIPTION
       The &lt;errno.h&gt; header file defines the integer variable errno, which is
       set by system calls and some library functions in the event of an error
       to indicate what went wrong.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;errno&lt;/code&gt; is an integer variable that can be set to signify
what exactly has gone wrong. And in order to inspect what kind of error
was raised, we can use &lt;code&gt;perror(3)&lt;/code&gt; to print the error, it
will translate the error code that has been set in the variable
&lt;code&gt;errno&lt;/code&gt; to a human-readable form. Lets check the man page for
&lt;code&gt;perror(3)&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 3 perror

NAME
       perror - print a system error message

SYNOPSIS
       #include &lt;stdio.h&gt;

       void perror(const char *s);

DESCRIPTION
       The perror() function produces a message on standard error describing
       the last error encountered during a call to a system or library
       function.
       ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can use &lt;code&gt;perror(3)&lt;/code&gt; and set the argument &lt;code&gt;s&lt;/code&gt;
with a string, and it then will be appended with an error message that
corresponds with the current value of &lt;code&gt;errno&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;implementation-1&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;Now that we know how we should implement the &lt;code&gt;socket(2)&lt;/code&gt;
function, let’s update our file with what we have discussed above. It
should resemble something like this:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb16&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb16-1&#34;&gt;&lt;a href=&#34;#cb16-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// ./steps/step001.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-2&#34;&gt;&lt;a href=&#34;#cb16-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-3&#34;&gt;&lt;a href=&#34;#cb16-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-4&#34;&gt;&lt;a href=&#34;#cb16-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-5&#34;&gt;&lt;a href=&#34;#cb16-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-6&#34;&gt;&lt;a href=&#34;#cb16-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-7&#34;&gt;&lt;a href=&#34;#cb16-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-8&#34;&gt;&lt;a href=&#34;#cb16-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-9&#34;&gt;&lt;a href=&#34;#cb16-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-10&#34;&gt;&lt;a href=&#34;#cb16-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-11&#34;&gt;&lt;a href=&#34;#cb16-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-12&#34;&gt;&lt;a href=&#34;#cb16-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-13&#34;&gt;&lt;a href=&#34;#cb16-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-14&#34;&gt;&lt;a href=&#34;#cb16-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-15&#34;&gt;&lt;a href=&#34;#cb16-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb16-16&#34;&gt;&lt;a href=&#34;#cb16-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now when we check our diagram from the section &lt;a
href=&#34;#basics&#34;&gt;Basics&lt;/a&gt; we’ve now created the socket, but we need to
bind it to an address otherwise no communication can be sent or received
to this socket as we have read from the man page
&lt;code&gt;tcp(7)&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                                                  
┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓
┃   SOCKET   ┃ ▶ ┃    BIND    ┃ ▶ ┃   LISTEN   ┃ ▶ ┃   ACCEPT   ┃ ▶ ┃ READ/WRITE ┃
┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛
 ───────────▶                                                                     &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So our next step will be to &lt;em&gt;bind&lt;/em&gt; the socket to a local
address and port.&lt;/p&gt;
&lt;h2 id=&#34;bind_socket&#34;&gt;Bind the socket to an address&lt;/h2&gt;
&lt;p&gt;The socket is created and exists in a namespace (an address family,
the &lt;code&gt;AF&lt;/code&gt; in &lt;code&gt;AF_INET&lt;/code&gt; stands for address family),
and we need to bind the socket to a local address, in order for the
socket to receive connections. We need to be using the
&lt;code&gt;bind(2)&lt;/code&gt; function for this, so let’s check out the
&lt;code&gt;bind(2)&lt;/code&gt; man pages on how we need to implement this.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man bind

NAME
       bind - bind a name to a socket

SYNOPSIS
       #include &lt;sys/types.h&gt;          /* See NOTES */
       #include &lt;sys/socket.h&gt;

       int bind(int sockfd, const struct sockaddr *addr,
                socklen_t addrlen);

DESCRIPTION                                                              
       When a socket is created with socket(2), it exists in a name space
       (address family) but has no address assigned to it. bind() assigns the
       address specified by addr to the socket referred to by the file
       descriptor sockfd. addrlen specifies the size, in bytes, of the address
       structure pointed to by addr. Traditionally, this operation is called
       “assigning a name to a socket”.
    
       It is normally necessary to assign a local address using bind() before
       a SOCK_STREAM socket may receive connections (see accept(2)).

       ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see that &lt;code&gt;bind(2)&lt;/code&gt; is included in the header file,
&lt;code&gt;&lt;sys/socket.h&gt;&lt;/code&gt;. And, on success it will return zero.
It accepts as arguments: &lt;code&gt;sockfd&lt;/code&gt;, &lt;code&gt;*addr&lt;/code&gt;, and
&lt;code&gt;addrln&lt;/code&gt;. Let’s go over the arguments, and make sense of what
we need to do in order to implement it.&lt;/p&gt;
&lt;h3 id=&#34;sockfd&#34;&gt;sockfd&lt;/h3&gt;
&lt;p&gt;This is the file descriptor that we’ve created with
&lt;code&gt;socket(2)&lt;/code&gt; in the last section. And we need to use that here
as the first argument.&lt;/p&gt;
&lt;h3 id=&#34;addr&#34;&gt;addr&lt;/h3&gt;
&lt;p&gt;This defines the address structure to which we want to bind the
socket to, and it depends on the address family we’re using. So let’s
check what &lt;code&gt;addr&lt;/code&gt; needs to look like. We can inspect the
rules used in the name binding, by referencing the man page of the
communication domain we’re using: &lt;code&gt;AF_INET&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 7 ip&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the section ‘Address format’ we can see an example. The address
structure will look like the following:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb20&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb20-1&#34;&gt;&lt;a href=&#34;#cb20-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-2&#34;&gt;&lt;a href=&#34;#cb20-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;   &lt;span class=&#34;dt&#34;&gt;sa_family_t&lt;/span&gt;    sin_family&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;co&#34;&gt;/* address family: AF_INET */&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-3&#34;&gt;&lt;a href=&#34;#cb20-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;   &lt;span class=&#34;dt&#34;&gt;in_port_t&lt;/span&gt;      sin_port&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;   &lt;span class=&#34;co&#34;&gt;/* port in network byte order */&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-4&#34;&gt;&lt;a href=&#34;#cb20-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;   &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; in_addr sin_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;   &lt;span class=&#34;co&#34;&gt;/* internet address */&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-5&#34;&gt;&lt;a href=&#34;#cb20-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-6&#34;&gt;&lt;a href=&#34;#cb20-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-7&#34;&gt;&lt;a href=&#34;#cb20-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;/* Internet address. */&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-8&#34;&gt;&lt;a href=&#34;#cb20-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; in_addr &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-9&#34;&gt;&lt;a href=&#34;#cb20-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;   &lt;span class=&#34;dt&#34;&gt;uint32_t&lt;/span&gt;       &lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;     &lt;span class=&#34;co&#34;&gt;/* address in network byte order */&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb20-10&#34;&gt;&lt;a href=&#34;#cb20-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;sin_family&lt;/code&gt; is always set to &lt;code&gt;AF_INET&lt;/code&gt;,
&lt;code&gt;sin_port&lt;/code&gt; contains the port in network byte order. Network
byte order represents how bytes are arranged when sending data over a
network, an order must be chosen to make sure that on both ends, the
machines interpret the numbers the same way independent of the cpu
architecture.&lt;/p&gt;
&lt;p&gt;For example an integer value of 1 represented as 4 bytes would be
represented on ‘big endian’ machines as &lt;code&gt;0 0 0 1&lt;/code&gt;, on a
‘little endian’ machines this would be &lt;code&gt;1 0 0 0&lt;/code&gt;. The value
of &lt;code&gt;0 0 0 1&lt;/code&gt; of the ‘big endian’ machine would then be
interpreted by the ‘little endian’ machine as the value
&lt;code&gt;16777216&lt;/code&gt;, and vice versa.&lt;a href=&#34;#fn12&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref12&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;12&lt;/sup&gt;&lt;/a&gt;&lt;a
href=&#34;#fn13&#34; class=&#34;footnote-ref&#34; id=&#34;fnref13&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;13&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And, as such, like the man page states, we need to call
&lt;code&gt;htons(3)&lt;/code&gt; on the number that is assigned to the port. Like
so: &lt;code&gt;htons(8080)&lt;/code&gt;. It will convert the host byte order to
network byte order. See the man page at for &lt;code&gt;htons(3)&lt;/code&gt; for
more information.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sin_addr&lt;/code&gt; contains the host interface address in network
byte order, and it is a member of the &lt;code&gt;struct&lt;/code&gt; named
&lt;code&gt;in_addr&lt;/code&gt;. The man page states that it should be one of the
&lt;code&gt;INADDR_*&lt;/code&gt; values. These are defined as symbolic constants in
the header file &lt;code&gt;&lt;netinet/in.h&gt;&lt;/code&gt;, or can set it by
using one of the &lt;code&gt;inet_aton(3)&lt;/code&gt;, &lt;code&gt;inet_addr(3)&lt;/code&gt;,
or &lt;code&gt;inet_makeaddr(3)&lt;/code&gt; library functions, to specify a
specific address. We can also inspect the POSIX man page to see how the
header file should be implemented on systems:
&lt;code&gt;man netinet_in.h&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We will make use of the symbolic constant &lt;code&gt;INADDR_ANY&lt;/code&gt;,
and it means ‘any address’, which translates to the
&lt;code&gt;0.0.0.0&lt;/code&gt;. &lt;code&gt;INADDRY_ANY&lt;/code&gt; is already in network
byte order, so we don’t really have to convert it. The man page advices
us to convert, so lets just implement it. We do this by calling
&lt;code&gt;htonl(3)&lt;/code&gt; on the address. But why are we using
&lt;code&gt;0.0.0.0&lt;/code&gt;, here? This is just your machine’s IP address. Your
machine will have one IP address for each network interface. When your
machine has for example Wi-Fi, and an ethernet connection, then that
machine will have two addresses, one for each interface. When we don’t
care what interface is going to be used we use the special address for
this, &lt;code&gt;0.0.0.0&lt;/code&gt; which is defined in the symbolic constant
&lt;code&gt;INADDR_ANY&lt;/code&gt; translates to this address.&lt;/p&gt;
&lt;h3 id=&#34;addrlen&#34;&gt;addrlen&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;addrlen&lt;/code&gt; argument specifies the size of the address
structure &lt;code&gt;addr&lt;/code&gt; in bytes. To get this we can use the
&lt;code&gt;sizeof()&lt;/code&gt; operator (it looks like a function, but it is an
operator like &lt;code&gt;&amp;&amp;&lt;/code&gt;, &lt;code&gt;||&lt;/code&gt;, etc.). The
argument is of type &lt;code&gt;socklen_t&lt;/code&gt; which is an integer type, and
we can get some background on specific type by inspecting the man page
of &lt;code&gt;accept(2)&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;In the original BSD sockets implementation (and on other older systems)
the third argument of accept() was declared as an int *. A POSIX.1g
draft standard wanted to change it into a size_t *C; later POSIX
standards and glibc 2.x have socklen_t *.&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;return-value-1&#34;&gt;return value&lt;/h3&gt;
&lt;p&gt;On success the return value for &lt;code&gt;bind(2)&lt;/code&gt; will be zero,
when an error occurred it will return &lt;code&gt;-1&lt;/code&gt;, and
&lt;code&gt;errno&lt;/code&gt; will also be set.&lt;/p&gt;
&lt;h3 id=&#34;implementation-2&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From what we have seen we’re able to implement the
&lt;code&gt;bind(2)&lt;/code&gt; function. First, we will create the address
structure, and then we can bind it to the socket. The updated code will
look something like this:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb22&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb22-1&#34;&gt;&lt;a href=&#34;#cb22-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// ./steps/step002.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-2&#34;&gt;&lt;a href=&#34;#cb22-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;arpa/inet.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-3&#34;&gt;&lt;a href=&#34;#cb22-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-4&#34;&gt;&lt;a href=&#34;#cb22-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-5&#34;&gt;&lt;a href=&#34;#cb22-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-6&#34;&gt;&lt;a href=&#34;#cb22-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-7&#34;&gt;&lt;a href=&#34;#cb22-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define PORT &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-8&#34;&gt;&lt;a href=&#34;#cb22-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-9&#34;&gt;&lt;a href=&#34;#cb22-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-10&#34;&gt;&lt;a href=&#34;#cb22-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-11&#34;&gt;&lt;a href=&#34;#cb22-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-12&#34;&gt;&lt;a href=&#34;#cb22-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-13&#34;&gt;&lt;a href=&#34;#cb22-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-14&#34;&gt;&lt;a href=&#34;#cb22-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-15&#34;&gt;&lt;a href=&#34;#cb22-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-16&#34;&gt;&lt;a href=&#34;#cb22-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-17&#34;&gt;&lt;a href=&#34;#cb22-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-18&#34;&gt;&lt;a href=&#34;#cb22-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create the address to bind the socket to&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-19&#34;&gt;&lt;a href=&#34;#cb22-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in host_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-20&#34;&gt;&lt;a href=&#34;#cb22-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; host_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-21&#34;&gt;&lt;a href=&#34;#cb22-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-22&#34;&gt;&lt;a href=&#34;#cb22-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_family &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; AF_INET&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-23&#34;&gt;&lt;a href=&#34;#cb22-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htons&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;PORT&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-24&#34;&gt;&lt;a href=&#34;#cb22-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htonl&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;INADDR_ANY&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-25&#34;&gt;&lt;a href=&#34;#cb22-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-26&#34;&gt;&lt;a href=&#34;#cb22-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Bind the socket to the address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-27&#34;&gt;&lt;a href=&#34;#cb22-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;bind&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; host_addrlen&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-28&#34;&gt;&lt;a href=&#34;#cb22-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (bind)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-29&#34;&gt;&lt;a href=&#34;#cb22-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-30&#34;&gt;&lt;a href=&#34;#cb22-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-31&#34;&gt;&lt;a href=&#34;#cb22-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket successfully bound to address&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-32&#34;&gt;&lt;a href=&#34;#cb22-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-33&#34;&gt;&lt;a href=&#34;#cb22-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb22-34&#34;&gt;&lt;a href=&#34;#cb22-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that we are typecasting &lt;code&gt;addr&lt;/code&gt; to the
&lt;code&gt;struct&lt;/code&gt; pointer &lt;code&gt;struct sockaddr *&lt;/code&gt; in the
argument of the &lt;code&gt;bind(2)&lt;/code&gt; function. Since &lt;code&gt;addr&lt;/code&gt;
is of the type &lt;code&gt;struct sockaddr_in&lt;/code&gt; we need to cast it to
&lt;code&gt;struct sockaddr *&lt;/code&gt;. From the man page &lt;code&gt;bind(2)&lt;/code&gt;
we can read: “The only purpose of this structure (&lt;code&gt;sockaddr&lt;/code&gt;)
is to cast the structure pointer passed in &lt;code&gt;addr&lt;/code&gt; in order to
avoid compiler warnings.” In essence what we are doing here is: whatever
&lt;code&gt;addr&lt;/code&gt; is pointing to, act like a &lt;code&gt;sockaddr&lt;/code&gt;.&lt;a
href=&#34;#fn14&#34; class=&#34;footnote-ref&#34; id=&#34;fnref14&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;14&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                                                  
┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓
┃   SOCKET   ┃ ▶ ┃    BIND    ┃ ▶ ┃   LISTEN   ┃ ▶ ┃   ACCEPT   ┃ ▶ ┃ READ/WRITE ┃
┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛
 ───────────▶     ───────────▶                                                    &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Referring back to our diagram, we’ve now also bound the socket to a
specific address. Now we can are ready to listen for incoming
connections. So lets implement that.&lt;/p&gt;
&lt;h2 id=&#34;listen&#34;&gt;Listen&lt;/h2&gt;
&lt;p&gt;We’ve created a socket and bounded it to a local address, now we need
to make sure that the socket is listening for incoming connection. We do
that by using &lt;code&gt;listen(2)&lt;/code&gt; function. This will make the socket
available for incoming connections. Let’s see what the man pages can
show us on how to use &lt;code&gt;listen(2)&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 2 listen

NAME
       listen - listen for connections on a socket

SYNOPSIS
       #include &lt;sys/types.h&gt;          /* See NOTES */
       #include &lt;sys/socket.h&gt;

       int listen(int sockfd, int backlog);

DESCRIPTION
       listen() marks the socket referred to by sockfd as a passive socket,
       that is, as a socket that will be used to accept incoming connection
       requests using accept(2).
       ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see, the &lt;code&gt;listen(2)&lt;/code&gt; function will put the
socket into ‘passive’ mode. Stream sockets are often ‘active’ or
‘passive’.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When a socket is created with the &lt;code&gt;socket(2)&lt;/code&gt;
function, it is set to &lt;em&gt;active&lt;/em&gt;. This socket can then be used in
the &lt;code&gt;connect(2)&lt;/code&gt; function to establish a connection to a
‘passive’ socket.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;em&gt;passive&lt;/em&gt; socket can allow incoming connections by
passing it to the &lt;code&gt;listen(2)&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In most applications that use stream sockets, the servers we will
perform the so called ‘passive socket open’, and on the client an
‘active socket open’. Since we’re creating a http webserver, and using
the &lt;code&gt;listen(2)&lt;/code&gt; function in order to listen for incoming
connections, the socket that we’ve created will be a passive socket, and
will be used to accept connect connections from other (active) sockets.
&lt;a href=&#34;#fn15&#34; class=&#34;footnote-ref&#34; id=&#34;fnref15&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;15&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;sockfd-1&#34;&gt;sockfd&lt;/h3&gt;
&lt;p&gt;Again, this is the file descriptor of the socket, and thus we will
use the &lt;code&gt;sockfd&lt;/code&gt; that we’ve created in section 3.&lt;/p&gt;
&lt;h3 id=&#34;backlog&#34;&gt;backlog&lt;/h3&gt;
&lt;p&gt;This integer will define how many pending connections will be queued
up for &lt;code&gt;sockfd&lt;/code&gt; socket, before it will be refused. For now,
we will set this to &lt;code&gt;128&lt;/code&gt;. Further connection requests block
until a pending connection is accepted. So, it defines the number of
connections that are accepted, but not yet handled by the application,
until &lt;code&gt;accept(2)&lt;/code&gt; gets it off the queue.&lt;/p&gt;
&lt;p&gt;From &lt;code&gt;listen(2)&lt;/code&gt; the ‘NOTES’ section:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;...

The behavior of the backlog argument on TCP sockets changed with Linux 2.2. Now
it specifies the queue length for completely established sockets waiting to be
accepted, instead of the number of incomplete connection requests. The maximum
length of the queue for incomplete sockets can be set using
/proc/sys/net/ipv4/tcp_max_syn_backlog. When syncookies are enabled there is no
logical maximum length and this setting is ignored. See tcp(7) for more
information.

If the backlog argument is greater than the value in
/proc/sys/net/core/somaxconn, then it is silently truncated to that value;
the default value in this file is 128. In kernels before 2.4.25, this limit was
a hard coded value, SOMAXCONN, with the value 128.

...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The symbolic constant &lt;code&gt;SOMAXCONN&lt;/code&gt; in
&lt;code&gt;&lt;sys/socket.h&gt;&lt;/code&gt; is defined by our system
(&lt;code&gt;128&lt;/code&gt; in the case of Linux), and we can use it to set the
&lt;code&gt;backlog&lt;/code&gt; argument (&lt;code&gt;man sys_socket.h&lt;/code&gt;).&lt;/p&gt;
&lt;h3 id=&#34;return-value-2&#34;&gt;return value&lt;/h3&gt;
&lt;p&gt;On success, zero will be returned, on failure &lt;code&gt;-1&lt;/code&gt; will be
returned, and as before &lt;code&gt;errno&lt;/code&gt; will also set, so we can
check and handle it accordingly.&lt;/p&gt;
&lt;h3 id=&#34;implementation-3&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;With the above information we are able to implement the
&lt;code&gt;listen(2)&lt;/code&gt; function, so let’s update our code:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb26&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb26-1&#34;&gt;&lt;a href=&#34;#cb26-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// ./steps/step003.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-2&#34;&gt;&lt;a href=&#34;#cb26-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;arpa/inet.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-3&#34;&gt;&lt;a href=&#34;#cb26-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-4&#34;&gt;&lt;a href=&#34;#cb26-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-5&#34;&gt;&lt;a href=&#34;#cb26-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-6&#34;&gt;&lt;a href=&#34;#cb26-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-7&#34;&gt;&lt;a href=&#34;#cb26-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define PORT &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-8&#34;&gt;&lt;a href=&#34;#cb26-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-9&#34;&gt;&lt;a href=&#34;#cb26-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-10&#34;&gt;&lt;a href=&#34;#cb26-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-11&#34;&gt;&lt;a href=&#34;#cb26-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-12&#34;&gt;&lt;a href=&#34;#cb26-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-13&#34;&gt;&lt;a href=&#34;#cb26-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-14&#34;&gt;&lt;a href=&#34;#cb26-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-15&#34;&gt;&lt;a href=&#34;#cb26-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-16&#34;&gt;&lt;a href=&#34;#cb26-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-17&#34;&gt;&lt;a href=&#34;#cb26-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-18&#34;&gt;&lt;a href=&#34;#cb26-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create the address to bind the socket to&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-19&#34;&gt;&lt;a href=&#34;#cb26-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in host_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-20&#34;&gt;&lt;a href=&#34;#cb26-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; host_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-21&#34;&gt;&lt;a href=&#34;#cb26-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-22&#34;&gt;&lt;a href=&#34;#cb26-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_family &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; AF_INET&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-23&#34;&gt;&lt;a href=&#34;#cb26-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htons&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;PORT&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-24&#34;&gt;&lt;a href=&#34;#cb26-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htonl&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;INADDR_ANY&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-25&#34;&gt;&lt;a href=&#34;#cb26-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-26&#34;&gt;&lt;a href=&#34;#cb26-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Bind the socket to the address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-27&#34;&gt;&lt;a href=&#34;#cb26-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;bind&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; host_addrlen&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-28&#34;&gt;&lt;a href=&#34;#cb26-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (bind)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-29&#34;&gt;&lt;a href=&#34;#cb26-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-30&#34;&gt;&lt;a href=&#34;#cb26-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-31&#34;&gt;&lt;a href=&#34;#cb26-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket successfully bound to address&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-32&#34;&gt;&lt;a href=&#34;#cb26-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-33&#34;&gt;&lt;a href=&#34;#cb26-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Listen for incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-34&#34;&gt;&lt;a href=&#34;#cb26-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;listen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOMAXCONN&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-35&#34;&gt;&lt;a href=&#34;#cb26-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (listen)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-36&#34;&gt;&lt;a href=&#34;#cb26-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-37&#34;&gt;&lt;a href=&#34;#cb26-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-38&#34;&gt;&lt;a href=&#34;#cb26-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;server listening for connections&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-39&#34;&gt;&lt;a href=&#34;#cb26-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-40&#34;&gt;&lt;a href=&#34;#cb26-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb26-41&#34;&gt;&lt;a href=&#34;#cb26-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Referring back to our diagram, we’ve created a socket, bound it to a
local address, and we’ve put the socket into ‘passive’ mode. Now we can
listen for incoming connections.&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                                                  
┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓
┃   SOCKET   ┃ ▶ ┃    BIND    ┃ ▶ ┃   LISTEN   ┃ ▶ ┃   ACCEPT   ┃ ▶ ┃ READ/WRITE ┃
┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛
 ───────────▶     ───────────▶     ───────────▶                                   &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, on to accept those connections.&lt;/p&gt;
&lt;h2 id=&#34;accept&#34;&gt;Accept&lt;/h2&gt;
&lt;p&gt;Now we’re ready to make sure the socket will accept connections. We
need to use the &lt;code&gt;accept(2)&lt;/code&gt; function, and let’s check the man
pages again on how we need to implement this.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 2 accept

NAME
       accept, accept4 - accept a connection on a socket

SYNOPSIS
       #include &lt;sys/types.h&gt;          /* See NOTES */
       #include &lt;sys/socket.h&gt;

       int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

DESCRIPTION
       The accept() system call is used with connection-based socket types
       (SOCK_STREAM, SOCK_SEQ‐ PACKET). It extracts the first connection
       request on the queue of pending connections for the listening socket,
       sockfd, creates a new connected socket, and returns a new file
       descriptor referring to that socket. The newly created socket is not in
       the listening state. The original socket sockfd is unaffected by this
       call.
       ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So the &lt;code&gt;accept(2)&lt;/code&gt; function will get the first connection
from the queue of the listening socket &lt;code&gt;sockfd&lt;/code&gt;. Then it will
create a &lt;em&gt;new&lt;/em&gt; connected socket, and the file descriptor that
points to that socket will be returned. The newly created socket is
however &lt;em&gt;not in a listening state&lt;/em&gt;, and thus the original socket
is not affected by this call and can be used to accept other
connections. When there are no pending connections when the
&lt;code&gt;accept(2)&lt;/code&gt; function is called, the call blocks until a new
connection arrives.&lt;/p&gt;
&lt;p&gt;Again, let’s look at the arguments that we need to provide to accept
connections.&lt;/p&gt;
&lt;h3 id=&#34;sockfd-2&#34;&gt;sockfd&lt;/h3&gt;
&lt;p&gt;Like before, we will use the original socket that was created in &lt;a
href=&#34;#implement_socket&#34;&gt;Implement the socket&lt;/a&gt; and here
&lt;code&gt;sockfd&lt;/code&gt; is the file descriptor of the socket.&lt;/p&gt;
&lt;h3 id=&#34;addr-1&#34;&gt;addr&lt;/h3&gt;
&lt;p&gt;The argument &lt;code&gt;addr&lt;/code&gt; is a pointer that refers to a
&lt;code&gt;sockaddr&lt;/code&gt; struct, this needs to be the address of the
original socket that we’ve created, and we need the pointer to that
struct here.&lt;/p&gt;
&lt;h3 id=&#34;addrlen-1&#34;&gt;addrlen&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;addrlen&lt;/code&gt; is a value result argument, it points to the
size of the buffer pointed to by the argument &lt;code&gt;addr&lt;/code&gt;. Because
&lt;code&gt;accept()&lt;/code&gt; can accept multiple protocol families we need to
provide the size of the address that we are using. A pointer is used
because: “the caller must initialize it to contain the size (in bytes)
of the structure pointed to by &lt;code&gt;addr&lt;/code&gt;; on return it will
contain the actual size of the peer address.” The kernel then knows how
much space is available to return the socket address. Upon return from
the &lt;code&gt;accept(2)&lt;/code&gt; function, the value of &lt;code&gt;addrlen&lt;/code&gt;
is set to indicate the number of bytes of data actually stored by the
kernel in the socket address structure. &lt;a href=&#34;#fn16&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref16&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;16&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When binding our socket (&lt;a href=&#34;#bind_socket&#34;&gt;Bind the socket to an
address&lt;/a&gt;), we’ve already created our &lt;code&gt;addrlen&lt;/code&gt; variable
with the size of the &lt;code&gt;sockaddr&lt;/code&gt; struct, so we can just pass
it to the &lt;code&gt;accept(2)&lt;/code&gt; function. However, the original
variable was an &lt;code&gt;int&lt;/code&gt;, so we need to typecast it to
&lt;code&gt;socklen_t *&lt;/code&gt; to make it work.&lt;/p&gt;
&lt;h3 id=&#34;return-value-3&#34;&gt;return value&lt;/h3&gt;
&lt;p&gt;It will return a non-negative integer that is a file descriptor for
the accepted socket. On error, it will return &lt;code&gt;-1&lt;/code&gt;, and
&lt;code&gt;errno&lt;/code&gt; will be set.&lt;/p&gt;
&lt;h3 id=&#34;implementation-4&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;Because we want to continue accepting new connections we will put the
&lt;code&gt;accept(2)&lt;/code&gt; function in a continuous loop. Important to note
is that we also need to close the file descriptor we’ve created by using
&lt;code&gt;accept(2)&lt;/code&gt;. We can close the socket by calling the
&lt;code&gt;close(2)&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 2 close

NAME
       close - close a file descriptor

SYNOPSIS
       #include &lt;unistd.h&gt;

       int close(int fd);

DESCRIPTION
       close() closes a file descriptor, so that it no longer refers to any
       file and may be reused. Any record locks (see fcntl(2)) held on the file
       it was associated with, and owned by the process, are removed
       (regardless of the file descriptor that was used to obtain the lock).
       ... &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we’re done with the socket we can just use it as the argument
&lt;code&gt;fd&lt;/code&gt; in the function &lt;code&gt;close(2)&lt;/code&gt; this will close
the file descriptor, so that it no longer refers to any file and may be
reused. When we update our code and implement the &lt;code&gt;accept(2)&lt;/code&gt;
function it should resemble the following:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb30&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb30-1&#34;&gt;&lt;a href=&#34;#cb30-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// ./steps/step004.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-2&#34;&gt;&lt;a href=&#34;#cb30-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;arpa/inet.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-3&#34;&gt;&lt;a href=&#34;#cb30-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-4&#34;&gt;&lt;a href=&#34;#cb30-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-5&#34;&gt;&lt;a href=&#34;#cb30-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-6&#34;&gt;&lt;a href=&#34;#cb30-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;unistd.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-7&#34;&gt;&lt;a href=&#34;#cb30-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-8&#34;&gt;&lt;a href=&#34;#cb30-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define PORT &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-9&#34;&gt;&lt;a href=&#34;#cb30-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-10&#34;&gt;&lt;a href=&#34;#cb30-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-11&#34;&gt;&lt;a href=&#34;#cb30-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-12&#34;&gt;&lt;a href=&#34;#cb30-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-13&#34;&gt;&lt;a href=&#34;#cb30-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-14&#34;&gt;&lt;a href=&#34;#cb30-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-15&#34;&gt;&lt;a href=&#34;#cb30-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-16&#34;&gt;&lt;a href=&#34;#cb30-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-17&#34;&gt;&lt;a href=&#34;#cb30-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-18&#34;&gt;&lt;a href=&#34;#cb30-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-19&#34;&gt;&lt;a href=&#34;#cb30-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create the address to bind the socket to&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-20&#34;&gt;&lt;a href=&#34;#cb30-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in host_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-21&#34;&gt;&lt;a href=&#34;#cb30-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; host_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-22&#34;&gt;&lt;a href=&#34;#cb30-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-23&#34;&gt;&lt;a href=&#34;#cb30-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_family &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; AF_INET&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-24&#34;&gt;&lt;a href=&#34;#cb30-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htons&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;PORT&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-25&#34;&gt;&lt;a href=&#34;#cb30-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htonl&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;INADDR_ANY&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-26&#34;&gt;&lt;a href=&#34;#cb30-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-27&#34;&gt;&lt;a href=&#34;#cb30-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Bind the socket to the address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-28&#34;&gt;&lt;a href=&#34;#cb30-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;bind&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; host_addrlen&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-29&#34;&gt;&lt;a href=&#34;#cb30-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (bind)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-30&#34;&gt;&lt;a href=&#34;#cb30-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-31&#34;&gt;&lt;a href=&#34;#cb30-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-32&#34;&gt;&lt;a href=&#34;#cb30-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket successfully bound to address&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-33&#34;&gt;&lt;a href=&#34;#cb30-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-34&#34;&gt;&lt;a href=&#34;#cb30-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Listen for incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-35&#34;&gt;&lt;a href=&#34;#cb30-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;listen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOMAXCONN&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-36&#34;&gt;&lt;a href=&#34;#cb30-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (listen)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-37&#34;&gt;&lt;a href=&#34;#cb30-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-38&#34;&gt;&lt;a href=&#34;#cb30-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-39&#34;&gt;&lt;a href=&#34;#cb30-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;server listening for connections&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-40&#34;&gt;&lt;a href=&#34;#cb30-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-41&#34;&gt;&lt;a href=&#34;#cb30-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(;;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-42&#34;&gt;&lt;a href=&#34;#cb30-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Accept incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-43&#34;&gt;&lt;a href=&#34;#cb30-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; newsockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; accept&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-44&#34;&gt;&lt;a href=&#34;#cb30-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                               &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;socklen_t&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addrlen&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-45&#34;&gt;&lt;a href=&#34;#cb30-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-46&#34;&gt;&lt;a href=&#34;#cb30-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (accept)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-47&#34;&gt;&lt;a href=&#34;#cb30-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-48&#34;&gt;&lt;a href=&#34;#cb30-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-49&#34;&gt;&lt;a href=&#34;#cb30-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;connection accepted&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-50&#34;&gt;&lt;a href=&#34;#cb30-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-51&#34;&gt;&lt;a href=&#34;#cb30-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        close&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-52&#34;&gt;&lt;a href=&#34;#cb30-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-53&#34;&gt;&lt;a href=&#34;#cb30-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-54&#34;&gt;&lt;a href=&#34;#cb30-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-55&#34;&gt;&lt;a href=&#34;#cb30-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And when we check our diagram again, we can that we’ve implemented
the &lt;code&gt;accept(2)&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                                                  
┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓
┃   SOCKET   ┃ ▶ ┃    BIND    ┃ ▶ ┃   LISTEN   ┃ ▶ ┃   ACCEPT   ┃ ▶ ┃ READ/WRITE ┃
┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛
 ───────────▶     ───────────▶     ───────────▶     ───────────▶                  &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, and we’re now ready to starting reading and writing to the
socket.&lt;/p&gt;
&lt;h2 id=&#34;read&#34;&gt;Read&lt;/h2&gt;
&lt;p&gt;If we recall back from man page of &lt;code&gt;socket(2)&lt;/code&gt; we read the
following:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;...

A connection to another socket is created with a connect(2) call. Once
connected, data may be transferred using read(2) and write(2) calls or some
variant of  the send(2) and recv(2) calls.  When a session has been completed
a close(2) may be performed.

...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can read and write by using the &lt;code&gt;read(2)&lt;/code&gt; and
&lt;code&gt;write(2)&lt;/code&gt; functions, or some variant of &lt;code&gt;send(2)&lt;/code&gt;
and &lt;code&gt;recv(2)&lt;/code&gt; calls. From the man page of
&lt;code&gt;send(2)&lt;/code&gt; we can read that &lt;code&gt;send(2)&lt;/code&gt; provides
extra flags that we might use. In this case we won’t be using those, and
as such we can stick with &lt;code&gt;read(2)&lt;/code&gt; and
&lt;code&gt;write(2)&lt;/code&gt;. &lt;a href=&#34;#fn17&#34; class=&#34;footnote-ref&#34; id=&#34;fnref17&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;17&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Because we’ve setup a connection between the client and the server we
can read the request of the client. Since, we got a file descriptor with
the &lt;code&gt;accept(2)&lt;/code&gt; function, we will be able to use the
&lt;code&gt;read(2)&lt;/code&gt; function to read the data that has been sent by the
client. Let’s check the man pages on how we’re able to use the
&lt;code&gt;read(2)&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 2 read

NAME
       read - read from a file descriptor

SYNOPSIS
       #include &lt;unistd.h&gt;

       ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION:
       read() attempts to read up to count bytes from file descriptor fd into
       the buffer starting at buf.
       ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function &lt;code&gt;read(2)&lt;/code&gt; will read up to &lt;code&gt;count&lt;/code&gt;
bytes from the file descriptor &lt;code&gt;fd&lt;/code&gt; into the buffer
&lt;code&gt;*buf&lt;/code&gt;. On success it will return the number of bytes that
were read, and the file position is advanced by this number. On error
&lt;code&gt;-1&lt;/code&gt; is returned and &lt;code&gt;errno&lt;/code&gt; will be set as
well.&lt;/p&gt;
&lt;p&gt;The file position keeps track of where in the file the next character
is to be read or written. This is ‘offset’ being recorded by the kernel.
&lt;a href=&#34;#fn18&#34; class=&#34;footnote-ref&#34; id=&#34;fnref18&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;18&lt;/sup&gt;&lt;/a&gt; On all POSIX.1 systems, the file
position is an integer representing the number of bytes from the
beginning of the file. The file position is normally set to the
beginning of the file when it is opened, and each time a character is
read or written, the file position is incremented sequentially. &lt;a
href=&#34;#fn19&#34; class=&#34;footnote-ref&#34; id=&#34;fnref19&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;19&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;fd&#34;&gt;fd&lt;/h3&gt;
&lt;p&gt;This argument &lt;code&gt;fd&lt;/code&gt; needs to be the file descriptor, this
is the new socket that was returned by the &lt;code&gt;accept(2)&lt;/code&gt;
function.&lt;/p&gt;
&lt;h3 id=&#34;buf&#34;&gt;buf&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;buf&lt;/code&gt; argument needs to be a pointer to the address of
the memory buffer that you want the contents of the file descriptor
&lt;code&gt;fd&lt;/code&gt; to be read into as a temporary storage. This buffer must
be at least &lt;code&gt;count&lt;/code&gt; bytes long. In our case we will be
creating a buffer that will be an array of the type &lt;code&gt;char&lt;/code&gt;.
And because the array name is converted to pointer, we can use the
variable name of the buffer as the argument.&lt;/p&gt;
&lt;h3 id=&#34;count&#34;&gt;count&lt;/h3&gt;
&lt;p&gt;We need to provide how many bytes we want to ready from the file
descriptor &lt;code&gt;fd&lt;/code&gt; into the buffer. This depends on how large of
a buffer you’re creating. In this example we’ll create an array of 2048
characters.&lt;/p&gt;
&lt;p&gt;The type &lt;code&gt;size_t&lt;/code&gt; is a unsigned integer type. It is
commonly used by the standard library to represent sizes and counts. Its
specific size is platform dependent.&lt;/p&gt;
&lt;h3 id=&#34;return-value-4&#34;&gt;return value&lt;/h3&gt;
&lt;p&gt;The return value is the number of bytes that were read into the
buffer, or 0 if the end of the file has been reached. On error, -1 is
returned, and &lt;code&gt;errno&lt;/code&gt; is set appropriately. The
&lt;code&gt;ssize_t&lt;/code&gt; is a ‘signed’ integer type, again it is commonly
used by the standard library to represent sizes and counts, and it holds
the byte count of what was read into the buffer.&lt;/p&gt;
&lt;h3 id=&#34;implementation-5&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;First, we need to implement the buffer, we will do this as soon as
the program begins. For now, we will create an array of the type
&lt;code&gt;char&lt;/code&gt; with a size of &lt;code&gt;2048&lt;/code&gt;. (each
&lt;code&gt;char&lt;/code&gt; is 1 byte). Don’t forget to include the header file
&lt;code&gt;uninstd.h&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You’ll also be able to check the number of bytes that are read, and
continue reading when the limit of the buffer is reached. However for
now we’ll keep it simple.&lt;/p&gt;
&lt;p&gt;Additionally, because the requests is likely sent using the HTTP
protocol. We can expect a certain format of the request, as we’ve
uncovered that in the beginning. Here you’ll be able to inspect the
contents of the request and return data based on the request, like
return specific requested html file, handling &lt;code&gt;GET&lt;/code&gt;,
&lt;code&gt;POST&lt;/code&gt; requests, as well as error handling for when the
request wasn’t using the HTTP protocol for instance, etc.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb34&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb34-1&#34;&gt;&lt;a href=&#34;#cb34-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// ./steps/step005.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-2&#34;&gt;&lt;a href=&#34;#cb34-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;arpa/inet.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-3&#34;&gt;&lt;a href=&#34;#cb34-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-4&#34;&gt;&lt;a href=&#34;#cb34-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-5&#34;&gt;&lt;a href=&#34;#cb34-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-6&#34;&gt;&lt;a href=&#34;#cb34-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;unistd.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-7&#34;&gt;&lt;a href=&#34;#cb34-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-8&#34;&gt;&lt;a href=&#34;#cb34-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define PORT &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-9&#34;&gt;&lt;a href=&#34;#cb34-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define BUFFER_SIZE &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1024&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-10&#34;&gt;&lt;a href=&#34;#cb34-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-11&#34;&gt;&lt;a href=&#34;#cb34-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-12&#34;&gt;&lt;a href=&#34;#cb34-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-13&#34;&gt;&lt;a href=&#34;#cb34-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-14&#34;&gt;&lt;a href=&#34;#cb34-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-15&#34;&gt;&lt;a href=&#34;#cb34-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-16&#34;&gt;&lt;a href=&#34;#cb34-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-17&#34;&gt;&lt;a href=&#34;#cb34-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-18&#34;&gt;&lt;a href=&#34;#cb34-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-19&#34;&gt;&lt;a href=&#34;#cb34-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-20&#34;&gt;&lt;a href=&#34;#cb34-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-21&#34;&gt;&lt;a href=&#34;#cb34-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-22&#34;&gt;&lt;a href=&#34;#cb34-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create the address to bind the socket to&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-23&#34;&gt;&lt;a href=&#34;#cb34-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in host_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-24&#34;&gt;&lt;a href=&#34;#cb34-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; host_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-25&#34;&gt;&lt;a href=&#34;#cb34-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-26&#34;&gt;&lt;a href=&#34;#cb34-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_family &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; AF_INET&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-27&#34;&gt;&lt;a href=&#34;#cb34-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htons&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;PORT&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-28&#34;&gt;&lt;a href=&#34;#cb34-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htonl&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;INADDR_ANY&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-29&#34;&gt;&lt;a href=&#34;#cb34-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-30&#34;&gt;&lt;a href=&#34;#cb34-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Bind the socket to the address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-31&#34;&gt;&lt;a href=&#34;#cb34-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;bind&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; host_addrlen&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-32&#34;&gt;&lt;a href=&#34;#cb34-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (bind)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-33&#34;&gt;&lt;a href=&#34;#cb34-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-34&#34;&gt;&lt;a href=&#34;#cb34-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-35&#34;&gt;&lt;a href=&#34;#cb34-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket successfully bound to address&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-36&#34;&gt;&lt;a href=&#34;#cb34-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-37&#34;&gt;&lt;a href=&#34;#cb34-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Listen for incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-38&#34;&gt;&lt;a href=&#34;#cb34-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;listen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOMAXCONN&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-39&#34;&gt;&lt;a href=&#34;#cb34-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (listen)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-40&#34;&gt;&lt;a href=&#34;#cb34-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-41&#34;&gt;&lt;a href=&#34;#cb34-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-42&#34;&gt;&lt;a href=&#34;#cb34-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;server listening for connections&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-43&#34;&gt;&lt;a href=&#34;#cb34-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-44&#34;&gt;&lt;a href=&#34;#cb34-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(;;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-45&#34;&gt;&lt;a href=&#34;#cb34-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Accept incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-46&#34;&gt;&lt;a href=&#34;#cb34-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; newsockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; accept&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-47&#34;&gt;&lt;a href=&#34;#cb34-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                               &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;socklen_t&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addrlen&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-48&#34;&gt;&lt;a href=&#34;#cb34-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-49&#34;&gt;&lt;a href=&#34;#cb34-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (accept)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-50&#34;&gt;&lt;a href=&#34;#cb34-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-51&#34;&gt;&lt;a href=&#34;#cb34-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-52&#34;&gt;&lt;a href=&#34;#cb34-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;connection accepted&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-53&#34;&gt;&lt;a href=&#34;#cb34-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-54&#34;&gt;&lt;a href=&#34;#cb34-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Read from the socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-55&#34;&gt;&lt;a href=&#34;#cb34-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; valread &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-56&#34;&gt;&lt;a href=&#34;#cb34-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;valread &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-57&#34;&gt;&lt;a href=&#34;#cb34-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (read)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-58&#34;&gt;&lt;a href=&#34;#cb34-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-59&#34;&gt;&lt;a href=&#34;#cb34-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-60&#34;&gt;&lt;a href=&#34;#cb34-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-61&#34;&gt;&lt;a href=&#34;#cb34-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        close&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-62&#34;&gt;&lt;a href=&#34;#cb34-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-63&#34;&gt;&lt;a href=&#34;#cb34-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-64&#34;&gt;&lt;a href=&#34;#cb34-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-65&#34;&gt;&lt;a href=&#34;#cb34-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&#34;write&#34;&gt;Write&lt;/h2&gt;
&lt;p&gt;Now that we are able to read the message that the client has sent to
us, we also want to relay something back to the client again. Because
we’re implementing a webserver, we’re going to return a simple webpage.
Again, because we are implementing a HTTP webserver we need to adhere to
the HTTP protocol. That means that we need to structure our response to
these rules.&lt;/p&gt;
&lt;p&gt;We will be using the same socket the &lt;code&gt;accept(2)&lt;/code&gt; function
that we’ve just read from. Because this socket is a file descriptor we
will, just as with &lt;code&gt;read(2)&lt;/code&gt;, be able to write to this socket
using the &lt;code&gt;write(2)&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 2 write

NAME
       write - write to a file descriptor

SYNOPSIS
       #include &lt;unistd.h&gt;

       ssize_t write(int fd, const void *buf, size_t count);

       write() writes up to count bytes from the buffer starting at buf to the
       file referred to by the file descriptor fd.
       ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function &lt;code&gt;write(2)&lt;/code&gt; will write bytes up to
&lt;code&gt;count&lt;/code&gt; from the buffer pointed to by &lt;code&gt;buf&lt;/code&gt; to the
file referenced by the file descriptor &lt;code&gt;fd&lt;/code&gt;. On success it
will return number of bytes written. On error &lt;code&gt;-1&lt;/code&gt; is
returned and &lt;code&gt;errno&lt;/code&gt; will be set appropriately.&lt;/p&gt;
&lt;h3 id=&#34;fd-1&#34;&gt;fd&lt;/h3&gt;
&lt;p&gt;As mentioned above the argument &lt;code&gt;fd&lt;/code&gt; is the file
descriptor that references the socket we’ve created by calling the
&lt;code&gt;accept(2)&lt;/code&gt; function. This is the also the same file
descriptor from which we read the request with the function
&lt;code&gt;read(2)&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;buf-1&#34;&gt;buf&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;buf&lt;/code&gt; arguments need to be a pointer to what we want
to write to as a response. For now we will use a pre-defined string that
we will add to the server. We can eventually extend this webserver to
serve actual html files. But for now we will add the following:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb36&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb36-1&#34;&gt;&lt;a href=&#34;#cb36-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; resp&lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;HTTP/1.0 200 OK&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb36-2&#34;&gt;&lt;a href=&#34;#cb36-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;st&#34;&gt;&#34;Server: webserver-c&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb36-3&#34;&gt;&lt;a href=&#34;#cb36-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;st&#34;&gt;&#34;Content-type: text/html&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb36-4&#34;&gt;&lt;a href=&#34;#cb36-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;html&gt;hello, world&lt;/html&gt;&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note, that the string is formatted following the HTTP protocol. (See:
&lt;a href=&#34;#basics&#34;&gt;Basics&lt;/a&gt;) We start with the request line, followed
by the headers, and it ends with the body. The escape code
&lt;code&gt;\r\n&lt;/code&gt; is used to separate the different sections of the
request. The escape code &lt;code&gt;\r&lt;/code&gt; stands for carriage return and
will set the cursor at the beginning of the line, and &lt;code&gt;\n&lt;/code&gt;
for new line and will move the cursor to a new line.&lt;/p&gt;
&lt;h3 id=&#34;count-1&#34;&gt;count&lt;/h3&gt;
&lt;p&gt;The argument &lt;code&gt;count&lt;/code&gt; is the number of bytes we need write
to the file &lt;code&gt;fd&lt;/code&gt; from buffer &lt;code&gt;buf&lt;/code&gt;. Because we
want to write the complete contents we need to know how many bytes there
are in the buffer. We do that by using &lt;code&gt;strlen()&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ man 3 strlen

NAME
       strlen - calculate the length of a string

SYNOPSIS
       #include &lt;string.h&gt;

       size_t strlen(const char *s);

DESCRIPTION
       The  strlen() function calculates the length of the string pointed to by
       s, excluding the terminating null byte (&#39;\0&#39;).

RETURN VALUE
       The strlen() function returns the number of characters in the string
       pointed to by s.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, we can provide the &lt;code&gt;s&lt;/code&gt; argument and we will get the
number of characters we provided in the string point to by
&lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;return-value-5&#34;&gt;return value&lt;/h3&gt;
&lt;p&gt;The return value of the &lt;code&gt;write(2)&lt;/code&gt; function will be the
number of bytes written to the file and its type is
&lt;code&gt;ssize_t&lt;/code&gt;. From before we’ve noted that &lt;code&gt;size_t&lt;/code&gt;
was used to represent sizes and counts, this version is the signed
version of &lt;code&gt;size_t&lt;/code&gt;, which means that it can hold values less
than zero. So, in this case a value of less than zero, &lt;code&gt;-1&lt;/code&gt;
is means an error occurred and &lt;code&gt;errno&lt;/code&gt; will be set
appropriately. When the return value is zero, it indicates that nothing
was written. It will not mean that an error occurred when this number is
smaller than the number of bytes that were requested.&lt;/p&gt;
&lt;h3 id=&#34;implementation-6&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;We will implement this first by creating the buffer, and we will
follow the instructions we’ve mentioned above. Because, we are done with
writing to the newly created socket we need to also close it. We’ve
already called &lt;code&gt;close(2)&lt;/code&gt; in the last section when we used
the &lt;code&gt;read(2)&lt;/code&gt; function. Now before we close it though, we
want to write to it. So put the &lt;code&gt;write(2)&lt;/code&gt; function above the
&lt;code&gt;close(2)&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb38&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb38-1&#34;&gt;&lt;a href=&#34;#cb38-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;arpa/inet.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-2&#34;&gt;&lt;a href=&#34;#cb38-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-3&#34;&gt;&lt;a href=&#34;#cb38-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-4&#34;&gt;&lt;a href=&#34;#cb38-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;string.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-5&#34;&gt;&lt;a href=&#34;#cb38-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-6&#34;&gt;&lt;a href=&#34;#cb38-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;unistd.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-7&#34;&gt;&lt;a href=&#34;#cb38-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-8&#34;&gt;&lt;a href=&#34;#cb38-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define PORT &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-9&#34;&gt;&lt;a href=&#34;#cb38-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define BUFFER_SIZE &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1024&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-10&#34;&gt;&lt;a href=&#34;#cb38-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-11&#34;&gt;&lt;a href=&#34;#cb38-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-12&#34;&gt;&lt;a href=&#34;#cb38-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-13&#34;&gt;&lt;a href=&#34;#cb38-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; resp&lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;HTTP/1.0 200 OK&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-14&#34;&gt;&lt;a href=&#34;#cb38-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;Server: webserver-c&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-15&#34;&gt;&lt;a href=&#34;#cb38-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;Content-type: text/html&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-16&#34;&gt;&lt;a href=&#34;#cb38-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;&lt;html&gt;hello, world&lt;/html&gt;&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-17&#34;&gt;&lt;a href=&#34;#cb38-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-18&#34;&gt;&lt;a href=&#34;#cb38-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-19&#34;&gt;&lt;a href=&#34;#cb38-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-20&#34;&gt;&lt;a href=&#34;#cb38-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-21&#34;&gt;&lt;a href=&#34;#cb38-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-22&#34;&gt;&lt;a href=&#34;#cb38-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-23&#34;&gt;&lt;a href=&#34;#cb38-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-24&#34;&gt;&lt;a href=&#34;#cb38-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-25&#34;&gt;&lt;a href=&#34;#cb38-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-26&#34;&gt;&lt;a href=&#34;#cb38-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create the address to bind the socket to&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-27&#34;&gt;&lt;a href=&#34;#cb38-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in host_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-28&#34;&gt;&lt;a href=&#34;#cb38-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; host_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-29&#34;&gt;&lt;a href=&#34;#cb38-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-30&#34;&gt;&lt;a href=&#34;#cb38-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_family &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; AF_INET&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-31&#34;&gt;&lt;a href=&#34;#cb38-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htons&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;PORT&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-32&#34;&gt;&lt;a href=&#34;#cb38-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htonl&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;INADDR_ANY&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-33&#34;&gt;&lt;a href=&#34;#cb38-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-34&#34;&gt;&lt;a href=&#34;#cb38-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Bind the socket to the address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-35&#34;&gt;&lt;a href=&#34;#cb38-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;bind&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; host_addrlen&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-36&#34;&gt;&lt;a href=&#34;#cb38-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (bind)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-37&#34;&gt;&lt;a href=&#34;#cb38-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-38&#34;&gt;&lt;a href=&#34;#cb38-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-39&#34;&gt;&lt;a href=&#34;#cb38-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket successfully bound to address&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-40&#34;&gt;&lt;a href=&#34;#cb38-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-41&#34;&gt;&lt;a href=&#34;#cb38-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Listen for incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-42&#34;&gt;&lt;a href=&#34;#cb38-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;listen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOMAXCONN&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-43&#34;&gt;&lt;a href=&#34;#cb38-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (listen)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-44&#34;&gt;&lt;a href=&#34;#cb38-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-45&#34;&gt;&lt;a href=&#34;#cb38-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-46&#34;&gt;&lt;a href=&#34;#cb38-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;server listening for connections&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-47&#34;&gt;&lt;a href=&#34;#cb38-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-48&#34;&gt;&lt;a href=&#34;#cb38-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(;;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-49&#34;&gt;&lt;a href=&#34;#cb38-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Accept incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-50&#34;&gt;&lt;a href=&#34;#cb38-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; newsockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; accept&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-51&#34;&gt;&lt;a href=&#34;#cb38-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                               &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;socklen_t&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addrlen&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-52&#34;&gt;&lt;a href=&#34;#cb38-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-53&#34;&gt;&lt;a href=&#34;#cb38-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (accept)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-54&#34;&gt;&lt;a href=&#34;#cb38-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-55&#34;&gt;&lt;a href=&#34;#cb38-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-56&#34;&gt;&lt;a href=&#34;#cb38-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;connection accepted&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-57&#34;&gt;&lt;a href=&#34;#cb38-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-58&#34;&gt;&lt;a href=&#34;#cb38-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Read from the socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-59&#34;&gt;&lt;a href=&#34;#cb38-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; valread &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-60&#34;&gt;&lt;a href=&#34;#cb38-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;valread &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-61&#34;&gt;&lt;a href=&#34;#cb38-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (read)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-62&#34;&gt;&lt;a href=&#34;#cb38-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-63&#34;&gt;&lt;a href=&#34;#cb38-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-64&#34;&gt;&lt;a href=&#34;#cb38-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-65&#34;&gt;&lt;a href=&#34;#cb38-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Write to the socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-66&#34;&gt;&lt;a href=&#34;#cb38-66&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; valwrite &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; write&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; resp&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; strlen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;resp&lt;span class=&#34;op&#34;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-67&#34;&gt;&lt;a href=&#34;#cb38-67&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;valwrite &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-68&#34;&gt;&lt;a href=&#34;#cb38-68&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (write)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-69&#34;&gt;&lt;a href=&#34;#cb38-69&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-70&#34;&gt;&lt;a href=&#34;#cb38-70&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-71&#34;&gt;&lt;a href=&#34;#cb38-71&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-72&#34;&gt;&lt;a href=&#34;#cb38-72&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        close&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-73&#34;&gt;&lt;a href=&#34;#cb38-73&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-74&#34;&gt;&lt;a href=&#34;#cb38-74&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-75&#34;&gt;&lt;a href=&#34;#cb38-75&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb38-76&#34;&gt;&lt;a href=&#34;#cb38-76&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And that concludes the implementation of all the steps from the
section &lt;a href=&#34;#basics&#34;&gt;Basics&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                                                  
┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓   ┏━━━━━━━━━━━━┓
┃   SOCKET   ┃ ▶ ┃    BIND    ┃ ▶ ┃   LISTEN   ┃ ▶ ┃   ACCEPT   ┃ ▶ ┃ READ/WRITE ┃
┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛   ┗━━━━━━━━━━━━┛
 ───────────▶     ───────────▶     ───────────▶     ───────────▶     ───────────▶ &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All what is left to do is to compile and run the program!&lt;/p&gt;
&lt;h3 id=&#34;lets-run-it&#34;&gt;Let’s run it!&lt;/h3&gt;
&lt;p&gt;We’ll go to the command line and run the following command:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb40&#34;&gt;&lt;pre
class=&#34;sourceCode bash&#34;&gt;&lt;code class=&#34;sourceCode bash&#34;&gt;&lt;span id=&#34;cb40-1&#34;&gt;&lt;a href=&#34;#cb40-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; gcc &lt;span class=&#34;at&#34;&gt;-Wall&lt;/span&gt; webserver.c &lt;span class=&#34;at&#34;&gt;-o&lt;/span&gt; webserver&lt;/span&gt;
&lt;span id=&#34;cb40-2&#34;&gt;&lt;a href=&#34;#cb40-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; ./webserver&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, you should be able to open your browser and check: &lt;a
href=&#34;http://localhost:8080&#34; class=&#34;uri&#34;&gt;http://localhost:8080&lt;/a&gt;, and
you should be greeted by the ‘hello, world’ message. Let’s also try to
implement some logging to the terminal, so we can see who is making the
request and what the request was.&lt;/p&gt;
&lt;h2 id=&#34;client_address&#34;&gt;Client address&lt;/h2&gt;
&lt;p&gt;In order to get the client address information we can use the
function &lt;code&gt;getsockname(2)&lt;/code&gt;. Let’s see what the man page
says:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;NAME
       getsockname - get socket name

SYNOPSIS
       #include &lt;sys/socket.h&gt;

       int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

DESCRIPTION
       getsockname() returns the current address to which the socket sockfd is
       bound, in the buffer pointed to by addr. The addrlen argument should be
       initialized to indicate the amount of space (in bytes) pointed to by
       addr. On return it contains the actual size of the socket address.

       The returned address is truncated if the buffer provided is too small;
       in this case, addrlen will return a value greater than was supplied to
       the call.

RETURN VALUE
       On success, zero is returned. On error, -1 is returned, and errno is
       set appropriately.&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;sockfd-3&#34;&gt;sockfd&lt;/h3&gt;
&lt;p&gt;So from the man page we read that we can get the current address to
which the socket &lt;code&gt;sockfd&lt;/code&gt; is bound. We got a new connected
socket with the client from &lt;code&gt;accept&lt;/code&gt; that is called
&lt;code&gt;newsockfd&lt;/code&gt; so we can use that in the
&lt;code&gt;getsockname(2)&lt;/code&gt; function.&lt;/p&gt;
&lt;h3 id=&#34;addr-2&#34;&gt;addr&lt;/h3&gt;
&lt;p&gt;This argument should be a pointer to a &lt;code&gt;struct sockaddr&lt;/code&gt;
structure, and should looking familiar since we’ve used it before when
we used &lt;code&gt;bind(2)&lt;/code&gt;. So we will use the same structure.&lt;/p&gt;
&lt;h3 id=&#34;addrlen-2&#34;&gt;addrlen&lt;/h3&gt;
&lt;p&gt;Like the &lt;code&gt;addr&lt;/code&gt; argument we’ve used in the
&lt;code&gt;bind(2)&lt;/code&gt; function, this argument will also be a pointer to a
&lt;code&gt;socklen_t&lt;/code&gt; struct. Again we will be doing the same as we did
with &lt;code&gt;bind(2)&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;implementation-7&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;When we’ve implemented the &lt;code&gt;getsockname(2)&lt;/code&gt; we will be
able to relay the client’s ip address and port. These are available from
the &lt;code&gt;sin_addr&lt;/code&gt; and &lt;code&gt;sin_port&lt;/code&gt; fields of the
&lt;code&gt;struct sockaddr_in&lt;/code&gt; structure.&lt;/p&gt;
&lt;p&gt;We need to convert them to a string representation, that we can use
to print. For that we will use the &lt;code&gt;inet_ntoa(3)&lt;/code&gt;function
(Internet host address, given in network byte order, to a string in IPv4
dotted decimal notation), and the &lt;code&gt;ntohs(3)&lt;/code&gt; (network byte
order to short integer byte order)&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb42&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb42-1&#34;&gt;&lt;a href=&#34;#cb42-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// ./steps/step007.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-2&#34;&gt;&lt;a href=&#34;#cb42-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;arpa/inet.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-3&#34;&gt;&lt;a href=&#34;#cb42-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-4&#34;&gt;&lt;a href=&#34;#cb42-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-5&#34;&gt;&lt;a href=&#34;#cb42-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;string.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-6&#34;&gt;&lt;a href=&#34;#cb42-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-7&#34;&gt;&lt;a href=&#34;#cb42-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;unistd.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-8&#34;&gt;&lt;a href=&#34;#cb42-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-9&#34;&gt;&lt;a href=&#34;#cb42-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define PORT &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-10&#34;&gt;&lt;a href=&#34;#cb42-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define BUFFER_SIZE &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1024&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-11&#34;&gt;&lt;a href=&#34;#cb42-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-12&#34;&gt;&lt;a href=&#34;#cb42-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-13&#34;&gt;&lt;a href=&#34;#cb42-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-14&#34;&gt;&lt;a href=&#34;#cb42-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; resp&lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;HTTP/1.0 200 OK&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-15&#34;&gt;&lt;a href=&#34;#cb42-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;Server: webserver-c&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-16&#34;&gt;&lt;a href=&#34;#cb42-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;Content-type: text/html&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-17&#34;&gt;&lt;a href=&#34;#cb42-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;&lt;html&gt;hello, world&lt;/html&gt;&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-18&#34;&gt;&lt;a href=&#34;#cb42-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-19&#34;&gt;&lt;a href=&#34;#cb42-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-20&#34;&gt;&lt;a href=&#34;#cb42-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-21&#34;&gt;&lt;a href=&#34;#cb42-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-22&#34;&gt;&lt;a href=&#34;#cb42-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-23&#34;&gt;&lt;a href=&#34;#cb42-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-24&#34;&gt;&lt;a href=&#34;#cb42-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-25&#34;&gt;&lt;a href=&#34;#cb42-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-26&#34;&gt;&lt;a href=&#34;#cb42-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-27&#34;&gt;&lt;a href=&#34;#cb42-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create the address to bind the socket to&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-28&#34;&gt;&lt;a href=&#34;#cb42-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in host_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-29&#34;&gt;&lt;a href=&#34;#cb42-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; host_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-30&#34;&gt;&lt;a href=&#34;#cb42-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-31&#34;&gt;&lt;a href=&#34;#cb42-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_family &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; AF_INET&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-32&#34;&gt;&lt;a href=&#34;#cb42-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htons&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;PORT&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-33&#34;&gt;&lt;a href=&#34;#cb42-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htonl&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;INADDR_ANY&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-34&#34;&gt;&lt;a href=&#34;#cb42-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-35&#34;&gt;&lt;a href=&#34;#cb42-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create client address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-36&#34;&gt;&lt;a href=&#34;#cb42-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in client_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-37&#34;&gt;&lt;a href=&#34;#cb42-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; client_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-38&#34;&gt;&lt;a href=&#34;#cb42-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-39&#34;&gt;&lt;a href=&#34;#cb42-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Bind the socket to the address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-40&#34;&gt;&lt;a href=&#34;#cb42-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;bind&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; host_addrlen&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-41&#34;&gt;&lt;a href=&#34;#cb42-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (bind)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-42&#34;&gt;&lt;a href=&#34;#cb42-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-43&#34;&gt;&lt;a href=&#34;#cb42-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-44&#34;&gt;&lt;a href=&#34;#cb42-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket successfully bound to address&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-45&#34;&gt;&lt;a href=&#34;#cb42-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-46&#34;&gt;&lt;a href=&#34;#cb42-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Listen for incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-47&#34;&gt;&lt;a href=&#34;#cb42-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;listen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOMAXCONN&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-48&#34;&gt;&lt;a href=&#34;#cb42-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (listen)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-49&#34;&gt;&lt;a href=&#34;#cb42-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-50&#34;&gt;&lt;a href=&#34;#cb42-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-51&#34;&gt;&lt;a href=&#34;#cb42-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;server listening for connections&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-52&#34;&gt;&lt;a href=&#34;#cb42-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-53&#34;&gt;&lt;a href=&#34;#cb42-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(;;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-54&#34;&gt;&lt;a href=&#34;#cb42-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Accept incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-55&#34;&gt;&lt;a href=&#34;#cb42-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; newsockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; accept&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-56&#34;&gt;&lt;a href=&#34;#cb42-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                               &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;socklen_t&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addrlen&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-57&#34;&gt;&lt;a href=&#34;#cb42-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-58&#34;&gt;&lt;a href=&#34;#cb42-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (accept)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-59&#34;&gt;&lt;a href=&#34;#cb42-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-60&#34;&gt;&lt;a href=&#34;#cb42-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-61&#34;&gt;&lt;a href=&#34;#cb42-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;connection accepted&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-62&#34;&gt;&lt;a href=&#34;#cb42-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-63&#34;&gt;&lt;a href=&#34;#cb42-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Get client address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-64&#34;&gt;&lt;a href=&#34;#cb42-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockn &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; getsockname&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-65&#34;&gt;&lt;a href=&#34;#cb42-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                                &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;socklen_t&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;client_addrlen&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-66&#34;&gt;&lt;a href=&#34;#cb42-66&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockn &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-67&#34;&gt;&lt;a href=&#34;#cb42-67&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (getsockname)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-68&#34;&gt;&lt;a href=&#34;#cb42-68&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-69&#34;&gt;&lt;a href=&#34;#cb42-69&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-70&#34;&gt;&lt;a href=&#34;#cb42-70&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-71&#34;&gt;&lt;a href=&#34;#cb42-71&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Read from the socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-72&#34;&gt;&lt;a href=&#34;#cb42-72&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; valread &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-73&#34;&gt;&lt;a href=&#34;#cb42-73&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;valread &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-74&#34;&gt;&lt;a href=&#34;#cb42-74&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (read)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-75&#34;&gt;&lt;a href=&#34;#cb42-75&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-76&#34;&gt;&lt;a href=&#34;#cb42-76&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-77&#34;&gt;&lt;a href=&#34;#cb42-77&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;[&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%u&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; inet_ntoa&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-78&#34;&gt;&lt;a href=&#34;#cb42-78&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;               ntohs&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port&lt;span class=&#34;op&#34;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-79&#34;&gt;&lt;a href=&#34;#cb42-79&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-80&#34;&gt;&lt;a href=&#34;#cb42-80&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Write to the socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-81&#34;&gt;&lt;a href=&#34;#cb42-81&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; valwrite &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; write&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; resp&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; strlen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;resp&lt;span class=&#34;op&#34;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-82&#34;&gt;&lt;a href=&#34;#cb42-82&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;valwrite &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-83&#34;&gt;&lt;a href=&#34;#cb42-83&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (write)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-84&#34;&gt;&lt;a href=&#34;#cb42-84&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-85&#34;&gt;&lt;a href=&#34;#cb42-85&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-86&#34;&gt;&lt;a href=&#34;#cb42-86&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-87&#34;&gt;&lt;a href=&#34;#cb42-87&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        close&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-88&#34;&gt;&lt;a href=&#34;#cb42-88&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-89&#34;&gt;&lt;a href=&#34;#cb42-89&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-90&#34;&gt;&lt;a href=&#34;#cb42-90&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb42-91&#34;&gt;&lt;a href=&#34;#cb42-91&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&#34;request_headers&#34;&gt;Get request headers&lt;/h2&gt;
&lt;p&gt;We’ve read the request from the client from the socket with
&lt;code&gt;read(2)&lt;/code&gt;, now we can print the contents of the request
message from the &lt;code&gt;buffer&lt;/code&gt;, and we should see how the request
of the client looks like. Since the client is sending the request
adhering the HTTP protocol, we can use &lt;code&gt;sscanf(3)&lt;/code&gt; to parse
the request. Since the request line is the first line of the request,
and is structured as follows:
&lt;code&gt;&lt;method&gt; &lt;path&gt; &lt;version&gt;&lt;/code&gt;, we can use
&lt;code&gt;sscanf(3)&lt;/code&gt; to parse the request line, and get the method,
path and version.&lt;/p&gt;
&lt;p&gt;Let’s check the man page for &lt;code&gt;sscanf(3)&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;NAME
       scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf - input format conversion

SYNOPSIS
       #include &lt;stdio.h&gt;

       int sscanf(const char *str, const char *format, ...);

DESCRIPTION

        ...
        The scanf() function reads input from the standard input stream
        stdin, fscanf() reads input from the stream pointer stream, and
        sscanf() reads its input from the character string pointed to by
        str.
        ...&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;str&#34;&gt;str&lt;/h3&gt;
&lt;p&gt;This argument is a pointer to a character string that contains the
input data. We will thus use the &lt;code&gt;buffer&lt;/code&gt; that we’ve read
from the socket.&lt;/p&gt;
&lt;h3 id=&#34;format&#34;&gt;format&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;format&lt;/code&gt; argument is a string that specifies the
format of the input data. And we can use string formatting to parse the
request line.&lt;/p&gt;
&lt;h3 id=&#34;return-value-6&#34;&gt;return value&lt;/h3&gt;
&lt;p&gt;On success, the number of input items successfully converted and
assigned is returned. On failure, the return value is EOF, and no
characters are assigned to the arguments.&lt;/p&gt;
&lt;h3 id=&#34;implementation-8&#34;&gt;Implementation&lt;/h3&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb44&#34;&gt;&lt;pre
class=&#34;sourceCode cpp&#34;&gt;&lt;code class=&#34;sourceCode cpp&#34;&gt;&lt;span id=&#34;cb44-1&#34;&gt;&lt;a href=&#34;#cb44-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// webserver.c&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-2&#34;&gt;&lt;a href=&#34;#cb44-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;arpa/inet.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-3&#34;&gt;&lt;a href=&#34;#cb44-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;errno.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-4&#34;&gt;&lt;a href=&#34;#cb44-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-5&#34;&gt;&lt;a href=&#34;#cb44-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;string.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-6&#34;&gt;&lt;a href=&#34;#cb44-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;sys/socket.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-7&#34;&gt;&lt;a href=&#34;#cb44-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#include &lt;/span&gt;&lt;span class=&#34;im&#34;&gt;&lt;unistd.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-8&#34;&gt;&lt;a href=&#34;#cb44-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-9&#34;&gt;&lt;a href=&#34;#cb44-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define PORT &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;8080&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-10&#34;&gt;&lt;a href=&#34;#cb44-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;pp&#34;&gt;#define BUFFER_SIZE &lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1024&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-11&#34;&gt;&lt;a href=&#34;#cb44-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-12&#34;&gt;&lt;a href=&#34;#cb44-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-13&#34;&gt;&lt;a href=&#34;#cb44-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-14&#34;&gt;&lt;a href=&#34;#cb44-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; resp&lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;HTTP/1.0 200 OK&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-15&#34;&gt;&lt;a href=&#34;#cb44-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;Server: webserver-c&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-16&#34;&gt;&lt;a href=&#34;#cb44-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;Content-type: text/html&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-17&#34;&gt;&lt;a href=&#34;#cb44-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                  &lt;span class=&#34;st&#34;&gt;&#34;&lt;html&gt;hello, world&lt;/html&gt;&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\r\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-18&#34;&gt;&lt;a href=&#34;#cb44-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-19&#34;&gt;&lt;a href=&#34;#cb44-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create a socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-20&#34;&gt;&lt;a href=&#34;#cb44-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; socket&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;AF_INET&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOCK_STREAM&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-21&#34;&gt;&lt;a href=&#34;#cb44-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-22&#34;&gt;&lt;a href=&#34;#cb44-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (socket)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-23&#34;&gt;&lt;a href=&#34;#cb44-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-24&#34;&gt;&lt;a href=&#34;#cb44-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-25&#34;&gt;&lt;a href=&#34;#cb44-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket created successfully&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-26&#34;&gt;&lt;a href=&#34;#cb44-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-27&#34;&gt;&lt;a href=&#34;#cb44-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create the address to bind the socket to&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-28&#34;&gt;&lt;a href=&#34;#cb44-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in host_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-29&#34;&gt;&lt;a href=&#34;#cb44-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; host_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-30&#34;&gt;&lt;a href=&#34;#cb44-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-31&#34;&gt;&lt;a href=&#34;#cb44-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_family &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; AF_INET&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-32&#34;&gt;&lt;a href=&#34;#cb44-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htons&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;PORT&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-33&#34;&gt;&lt;a href=&#34;#cb44-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    host_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;va&#34;&gt;s_addr&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; htonl&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;INADDR_ANY&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-34&#34;&gt;&lt;a href=&#34;#cb44-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-35&#34;&gt;&lt;a href=&#34;#cb44-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Create client address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-36&#34;&gt;&lt;a href=&#34;#cb44-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr_in client_addr&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-37&#34;&gt;&lt;a href=&#34;#cb44-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; client_addrlen &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;sizeof&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-38&#34;&gt;&lt;a href=&#34;#cb44-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-39&#34;&gt;&lt;a href=&#34;#cb44-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Bind the socket to the address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-40&#34;&gt;&lt;a href=&#34;#cb44-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;bind&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; host_addrlen&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-41&#34;&gt;&lt;a href=&#34;#cb44-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (bind)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-42&#34;&gt;&lt;a href=&#34;#cb44-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-43&#34;&gt;&lt;a href=&#34;#cb44-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-44&#34;&gt;&lt;a href=&#34;#cb44-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;socket successfully bound to address&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-45&#34;&gt;&lt;a href=&#34;#cb44-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-46&#34;&gt;&lt;a href=&#34;#cb44-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// Listen for incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-47&#34;&gt;&lt;a href=&#34;#cb44-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;listen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; SOMAXCONN&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-48&#34;&gt;&lt;a href=&#34;#cb44-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (listen)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-49&#34;&gt;&lt;a href=&#34;#cb44-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-50&#34;&gt;&lt;a href=&#34;#cb44-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-51&#34;&gt;&lt;a href=&#34;#cb44-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;server listening for connections&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-52&#34;&gt;&lt;a href=&#34;#cb44-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-53&#34;&gt;&lt;a href=&#34;#cb44-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(;;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-54&#34;&gt;&lt;a href=&#34;#cb44-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Accept incoming connections&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-55&#34;&gt;&lt;a href=&#34;#cb44-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; newsockfd &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; accept&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-56&#34;&gt;&lt;a href=&#34;#cb44-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                               &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;socklen_t&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;host_addrlen&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-57&#34;&gt;&lt;a href=&#34;#cb44-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-58&#34;&gt;&lt;a href=&#34;#cb44-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (accept)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-59&#34;&gt;&lt;a href=&#34;#cb44-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-60&#34;&gt;&lt;a href=&#34;#cb44-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-61&#34;&gt;&lt;a href=&#34;#cb44-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;connection accepted&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-62&#34;&gt;&lt;a href=&#34;#cb44-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-63&#34;&gt;&lt;a href=&#34;#cb44-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Get client address&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-64&#34;&gt;&lt;a href=&#34;#cb44-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; sockn &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; getsockname&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;struct&lt;/span&gt; sockaddr &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-65&#34;&gt;&lt;a href=&#34;#cb44-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                                &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;socklen_t&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*)&amp;&lt;/span&gt;client_addrlen&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-66&#34;&gt;&lt;a href=&#34;#cb44-66&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;sockn &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-67&#34;&gt;&lt;a href=&#34;#cb44-67&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (getsockname)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-68&#34;&gt;&lt;a href=&#34;#cb44-68&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-69&#34;&gt;&lt;a href=&#34;#cb44-69&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-70&#34;&gt;&lt;a href=&#34;#cb44-70&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-71&#34;&gt;&lt;a href=&#34;#cb44-71&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Read from the socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-72&#34;&gt;&lt;a href=&#34;#cb44-72&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; valread &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; buffer&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-73&#34;&gt;&lt;a href=&#34;#cb44-73&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;valread &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-74&#34;&gt;&lt;a href=&#34;#cb44-74&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (read)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-75&#34;&gt;&lt;a href=&#34;#cb44-75&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-76&#34;&gt;&lt;a href=&#34;#cb44-76&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-77&#34;&gt;&lt;a href=&#34;#cb44-77&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-78&#34;&gt;&lt;a href=&#34;#cb44-78&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Read the request&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-79&#34;&gt;&lt;a href=&#34;#cb44-79&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;char&lt;/span&gt; method&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;],&lt;/span&gt; uri&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;],&lt;/span&gt; version&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;BUFFER_SIZE&lt;span class=&#34;op&#34;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-80&#34;&gt;&lt;a href=&#34;#cb44-80&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        sscanf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;buffer&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;st&#34;&gt; &lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;st&#34;&gt; &lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; method&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; uri&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; version&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-81&#34;&gt;&lt;a href=&#34;#cb44-81&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;[&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%u&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;] &lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;st&#34;&gt; &lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s&lt;/span&gt;&lt;span class=&#34;st&#34;&gt; &lt;/span&gt;&lt;span class=&#34;sc&#34;&gt;%s\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; inet_ntoa&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_addr&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-82&#34;&gt;&lt;a href=&#34;#cb44-82&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;               ntohs&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;client_addr&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;sin_port&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt; method&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; version&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; uri&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-83&#34;&gt;&lt;a href=&#34;#cb44-83&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-84&#34;&gt;&lt;a href=&#34;#cb44-84&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;co&#34;&gt;// Write to the socket&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-85&#34;&gt;&lt;a href=&#34;#cb44-85&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt; valwrite &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; write&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; resp&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; strlen&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;resp&lt;span class=&#34;op&#34;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-86&#34;&gt;&lt;a href=&#34;#cb44-86&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;valwrite &lt;span class=&#34;op&#34;&gt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-87&#34;&gt;&lt;a href=&#34;#cb44-87&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            perror&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;webserver (write)&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-88&#34;&gt;&lt;a href=&#34;#cb44-88&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;continue&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-89&#34;&gt;&lt;a href=&#34;#cb44-89&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-90&#34;&gt;&lt;a href=&#34;#cb44-90&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-91&#34;&gt;&lt;a href=&#34;#cb44-91&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        close&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;newsockfd&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-92&#34;&gt;&lt;a href=&#34;#cb44-92&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-93&#34;&gt;&lt;a href=&#34;#cb44-93&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-94&#34;&gt;&lt;a href=&#34;#cb44-94&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb44-95&#34;&gt;&lt;a href=&#34;#cb44-95&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion / Next Steps&lt;/h2&gt;
&lt;p&gt;And that concludes our implementation of a simple HTTP server. We’ve
covered the basics of implementing a simple HTTP web server, doing so
we’ve also learned a bit about TCP/IP, HTTP, socket programming, and
system calls. The resulting code gives you a basis to start implementing
a more complex HTTP webserver that can handle multiple concurrent
connections, and that can serve html files from the filesystem based on
the path of the request.&lt;/p&gt;
&lt;h2 class=&#34;unnumbered&#34; id=&#34;references&#34;&gt;References&lt;/h2&gt;
&lt;div id=&#34;refs&#34; class=&#34;references csl-bib-body hanging-indent&#34;
data-entry-spacing=&#34;0&#34; role=&#34;list&#34;&gt;
&lt;div id=&#34;ref-ostep2018&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Arpaci-Dusseau, Remzi H., and Andrea C. Arpaci-Dusseau. 2018.
&lt;em&gt;Operating Systems: Three Easy Pieces&lt;/em&gt;. Arpaci-Dusseau Books. &lt;a
href=&#34;http://pages.cs.wisc.edu/~remzi/OSTEP/&#34;&gt;http://pages.cs.wisc.edu/~remzi/OSTEP/&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-github_ostep&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Github. 2020. &lt;span&gt;“Remzi-Arpacidusseau - Ostep Concurrency Webserver -
Github.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://github.com/remzi-arpacidusseau/ostep-projects/tree/master/concurrency-webserver&#34;&gt;https://github.com/remzi-arpacidusseau/ostep-projects/tree/master/concurrency-webserver&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-lpi1961&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Kerrisk, N. 2010. &lt;em&gt;The Linux Programming Interface: a Linux and UNIX
system programming handbook&lt;/em&gt;. No Starch Press.
&lt;/div&gt;
&lt;div id=&#34;ref-mozilla_webserver&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Mozilla. 2020. &lt;span&gt;“What Is a Webserver - Mozilla.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_web_server&#34;&gt;https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_web_server&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-glibc&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
&lt;span&gt;“The GNU C Library Reference Manual.”&lt;/span&gt; n.d. &lt;a
href=&#34;https://www.gnu.org/software/libc/manual/&#34;&gt;https://www.gnu.org/software/libc/manual/&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_webserver&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Wikipedia. 2020. &lt;span&gt;“Webserver - Wikipedia, The Free
Encyclopedia.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/Web_server&#34;&gt;https://en.wikipedia.org/wiki/Web_server&lt;/a&gt;.
&lt;/div&gt;
&lt;/div&gt;
&lt;section id=&#34;footnotes&#34; class=&#34;footnotes footnotes-end-of-document&#34;
role=&#34;doc-endnotes&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34;
data-cites=&#34;ostep2018&#34;&gt;Arpaci-Dusseau and Arpaci-Dusseau (&lt;a
href=&#34;#ref-ostep2018&#34; role=&#34;doc-biblioref&#34;&gt;2018&lt;/a&gt;)&lt;/span&gt;&lt;a
href=&#34;#fnref1&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://github.com/remzi-arpacidusseau/ostep-projects/tree/master/concurrency-webserver&#34;
class=&#34;uri&#34;&gt;https://github.com/remzi-arpacidusseau/ostep-projects/tree/master/concurrency-webserver&lt;/a&gt;&lt;a
href=&#34;#fnref2&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34; data-cites=&#34;github_ostep&#34;&gt;Github
(&lt;a href=&#34;#ref-github_ostep&#34; role=&#34;doc-biblioref&#34;&gt;2020&lt;/a&gt;)&lt;/span&gt;;
&lt;span class=&#34;citation&#34; data-cites=&#34;mozilla_webserver&#34;&gt;Mozilla (&lt;a
href=&#34;#ref-mozilla_webserver&#34; role=&#34;doc-biblioref&#34;&gt;2020&lt;/a&gt;)&lt;/span&gt;;
&lt;span class=&#34;citation&#34; data-cites=&#34;wiki_webserver&#34;&gt;Wikipedia (&lt;a
href=&#34;#ref-wiki_webserver&#34; role=&#34;doc-biblioref&#34;&gt;2020&lt;/a&gt;)&lt;/span&gt;&lt;a
href=&#34;#fnref3&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn4&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34;
data-cites=&#34;mozilla_webserver&#34;&gt;Mozilla (&lt;a href=&#34;#ref-mozilla_webserver&#34;
role=&#34;doc-biblioref&#34;&gt;2020&lt;/a&gt;)&lt;/span&gt;&lt;a href=&#34;#fnref4&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn5&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://tools.ietf.org/html/rfc2616&#34;
class=&#34;uri&#34;&gt;https://tools.ietf.org/html/rfc2616&lt;/a&gt;&lt;a href=&#34;#fnref5&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn6&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol&#34;
class=&#34;uri&#34;&gt;https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol&lt;/a&gt;&lt;a
href=&#34;#fnref6&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn7&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Application_layer&#34;
class=&#34;uri&#34;&gt;https://en.wikipedia.org/wiki/Application_layer&lt;/a&gt;&lt;a
href=&#34;#fnref7&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn8&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Internet_protocol_suite&#34;
class=&#34;uri&#34;&gt;https://en.wikipedia.org/wiki/Internet_protocol_suite&lt;/a&gt;&lt;a
href=&#34;#fnref8&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn9&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://github.com/remzi-arpacidusseau/ostep-projects/tree/master/concurrency-webserver#http-background&#34;
class=&#34;uri&#34;&gt;https://github.com/remzi-arpacidusseau/ostep-projects/tree/master/concurrency-webserver#http-background&lt;/a&gt;&lt;a
href=&#34;#fnref9&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn10&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/C_POSIX_library&#34;
class=&#34;uri&#34;&gt;https://en.wikipedia.org/wiki/C_POSIX_library&lt;/a&gt;&lt;a
href=&#34;#fnref10&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn11&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/File_descriptor&#34;
class=&#34;uri&#34;&gt;https://en.wikipedia.org/wiki/File_descriptor&lt;/a&gt;&lt;a
href=&#34;#fnref11&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn12&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://tools.ietf.org/html/draft-newman-network-byte-order-01&#34;
class=&#34;uri&#34;&gt;https://tools.ietf.org/html/draft-newman-network-byte-order-01&lt;/a&gt;&lt;a
href=&#34;#fnref12&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn13&#34;&gt;&lt;p&gt;The terms derive from Jonathan Swift’s 1726 satirical
novel Gulliver’s Travels, in which the terms refer to opposing political
factions who open their boiled eggs at opposite ends. [&lt;span
class=&#34;citation&#34; data-cites=&#34;lpi1961&#34;&gt;Kerrisk (&lt;a href=&#34;#ref-lpi1961&#34;
role=&#34;doc-biblioref&#34;&gt;2010&lt;/a&gt;)&lt;/span&gt;; ch. 59.2]&lt;a href=&#34;#fnref13&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn14&#34;&gt;&lt;p&gt;&lt;a href=&#34;http://www.cplusplus.com/forum/general/14828/&#34;
class=&#34;uri&#34;&gt;http://www.cplusplus.com/forum/general/14828/&lt;/a&gt;&lt;a
href=&#34;#fnref14&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn15&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34; data-cites=&#34;lpi1961&#34;&gt;Kerrisk (&lt;a
href=&#34;#ref-lpi1961&#34; role=&#34;doc-biblioref&#34;&gt;2010&lt;/a&gt;)&lt;/span&gt;; ch. 56.5&lt;a
href=&#34;#fnref15&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn16&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34; data-cites=&#34;lpi1961&#34;&gt;Kerrisk (&lt;a
href=&#34;#ref-lpi1961&#34; role=&#34;doc-biblioref&#34;&gt;2010&lt;/a&gt;)&lt;/span&gt;; ch. 56.5.2&lt;a
href=&#34;#fnref16&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn17&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34; data-cites=&#34;lpi1961&#34;&gt;Kerrisk (&lt;a
href=&#34;#ref-lpi1961&#34; role=&#34;doc-biblioref&#34;&gt;2010&lt;/a&gt;)&lt;/span&gt;; ch. 4,
ch. 56.1&lt;a href=&#34;#fnref17&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn18&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34; data-cites=&#34;lpi1961&#34;&gt;Kerrisk (&lt;a
href=&#34;#ref-lpi1961&#34; role=&#34;doc-biblioref&#34;&gt;2010&lt;/a&gt;)&lt;/span&gt;; ch. 4.7&lt;a
href=&#34;#fnref18&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn19&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34; data-cites=&#34;glibc&#34;&gt;&lt;span&gt;“The
GNU C Library Reference Manual”&lt;/span&gt; (&lt;a href=&#34;#ref-glibc&#34;
role=&#34;doc-biblioref&#34;&gt;n.d.&lt;/a&gt;)&lt;/span&gt;; ch. 11.1.2&lt;a href=&#34;#fnref19&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
    <item>
      <title>Securing Go HTTP webservers</title>
      <link>https://bruinsslot.jp/articles/go-secure-webserver/</link>
      <pubDate>Sun, 24 May 2020 08:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/go-secure-webserver/</guid>
      <description>&lt;h2 id=&#34;intro&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;For a project I am working on, I wanted to be able to expose the http
server implementation in Go to the internet. After listening to the Go
Time podcast and specifically the episode 101&lt;a href=&#34;#fn1&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref1&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;, I
wanted to go over the common measures you can employ to make your Go
HTTP webserver more secure. But before we begin:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;DISCLAIMER&lt;/strong&gt;: Web security is sensitive and vast
subject, and I want to make sure that you do own research on how to set
up a secure HTTP webserver. So don’t rely on this post solely, and
always do some rigorous testing. To make it a bit easier to find
resources I’ve added references and footnotes to the post. So I implore
you to read sources to get a better understanding of the subject.
Additionally, some measures taken here will not always be supported by
all clients, such as browsers. This is because in this post I tried to
use the latest versions and recommendations, and these are not always
supported.&lt;/p&gt;
&lt;p&gt;Since we’ve got that out of the way, let’s begin.&lt;/p&gt;
&lt;h2 id=&#34;simple&#34;&gt;Simple webserver&lt;/h2&gt;
&lt;p&gt;So, let’s start with creating a simple HTTP webserver, you’ve
probably come across this one several times.&lt;a href=&#34;#fn2&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref2&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb1&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb1-1&#34;&gt;&lt;a href=&#34;#cb1-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// server.go&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-2&#34;&gt;&lt;a href=&#34;#cb1-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;package&lt;/span&gt; main&lt;/span&gt;
&lt;span id=&#34;cb1-3&#34;&gt;&lt;a href=&#34;#cb1-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-4&#34;&gt;&lt;a href=&#34;#cb1-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-5&#34;&gt;&lt;a href=&#34;#cb1-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;log&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-6&#34;&gt;&lt;a href=&#34;#cb1-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;net/http&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-7&#34;&gt;&lt;a href=&#34;#cb1-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-8&#34;&gt;&lt;a href=&#34;#cb1-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-9&#34;&gt;&lt;a href=&#34;#cb1-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-10&#34;&gt;&lt;a href=&#34;#cb1-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandleFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;/&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-11&#34;&gt;&lt;a href=&#34;#cb1-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Write&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;hello, world&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-12&#34;&gt;&lt;a href=&#34;#cb1-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-13&#34;&gt;&lt;a href=&#34;#cb1-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-14&#34;&gt;&lt;a href=&#34;#cb1-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-15&#34;&gt;&lt;a href=&#34;#cb1-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s run it and check if it works:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ go run server.go
$ curl http://localhost
hello world&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cool, we have a starting point. Let’s add our first security
measures.&lt;/p&gt;
&lt;h2 id=&#34;timeouts-limits&#34;&gt;Timeouts / Limits&lt;/h2&gt;
&lt;p&gt;To make our HTTP webserver more secure we have the option to set some
timeouts and limits for it. This will make sure that connections aren’t
stuck, not making progress, or are stalling. To help us with setting
some defaults We will be checking out the article written by Filippo
Valsorda: &lt;em&gt;“So you want to expose Go on the Internet”&lt;/em&gt; &lt;span
class=&#34;citation&#34; data-cites=&#34;valsorda2016a&#34;&gt;(&lt;a
href=&#34;#ref-valsorda2016a&#34; role=&#34;doc-biblioref&#34;&gt;Valsorda 2016&lt;/a&gt;)&lt;/span&gt;
to guide us.&lt;/p&gt;
&lt;p&gt;When we implement those defaults, the end result will resemble
something like this:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb3&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb3-1&#34;&gt;&lt;a href=&#34;#cb3-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// server.go&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-2&#34;&gt;&lt;a href=&#34;#cb3-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;package&lt;/span&gt; main&lt;/span&gt;
&lt;span id=&#34;cb3-3&#34;&gt;&lt;a href=&#34;#cb3-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-4&#34;&gt;&lt;a href=&#34;#cb3-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-5&#34;&gt;&lt;a href=&#34;#cb3-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;log&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-6&#34;&gt;&lt;a href=&#34;#cb3-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;net/http&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-7&#34;&gt;&lt;a href=&#34;#cb3-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;time&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-8&#34;&gt;&lt;a href=&#34;#cb3-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-9&#34;&gt;&lt;a href=&#34;#cb3-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-10&#34;&gt;&lt;a href=&#34;#cb3-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-11&#34;&gt;&lt;a href=&#34;#cb3-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    srv &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-12&#34;&gt;&lt;a href=&#34;#cb3-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-13&#34;&gt;&lt;a href=&#34;#cb3-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-14&#34;&gt;&lt;a href=&#34;#cb3-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-15&#34;&gt;&lt;a href=&#34;#cb3-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;MaxBytesReader&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-16&#34;&gt;&lt;a href=&#34;#cb3-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Write&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;hello, world&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-17&#34;&gt;&lt;a href=&#34;#cb3-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-18&#34;&gt;&lt;a href=&#34;#cb3-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-19&#34;&gt;&lt;a href=&#34;#cb3-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        ReadTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;       &lt;span class=&#34;dv&#34;&gt;5&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-20&#34;&gt;&lt;a href=&#34;#cb3-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        WriteTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;      &lt;span class=&#34;dv&#34;&gt;10&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-21&#34;&gt;&lt;a href=&#34;#cb3-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        IdleTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;       &lt;span class=&#34;dv&#34;&gt;120&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-22&#34;&gt;&lt;a href=&#34;#cb3-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        ReadHeaderTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;5&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-23&#34;&gt;&lt;a href=&#34;#cb3-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        MaxHeaderBytes&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;    &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-24&#34;&gt;&lt;a href=&#34;#cb3-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-25&#34;&gt;&lt;a href=&#34;#cb3-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-26&#34;&gt;&lt;a href=&#34;#cb3-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb3-27&#34;&gt;&lt;a href=&#34;#cb3-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, let’s go over it and investigate what fields we need to set.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb4&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb4-1&#34;&gt;&lt;a href=&#34;#cb4-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;ReadTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;       &lt;span class=&#34;dv&#34;&gt;5&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb4-2&#34;&gt;&lt;a href=&#34;#cb4-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;WriteTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;      &lt;span class=&#34;dv&#34;&gt;10&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb4-3&#34;&gt;&lt;a href=&#34;#cb4-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;IdleTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;       &lt;span class=&#34;dv&#34;&gt;120&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb4-4&#34;&gt;&lt;a href=&#34;#cb4-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;ReadHeaderTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;5&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The timeout &lt;code&gt;ReadTimeout&lt;/code&gt; covers, from the server
perspective, when the connection is established to when the request from
the client is read. The timeout &lt;code&gt;WriteTimeout&lt;/code&gt; covers the
duration of the server writing its response. The timeout
&lt;code&gt;IdleTimeout&lt;/code&gt; covers the duration to wait for a new
connection when ‘keep-alive’ is enabled. And the
&lt;code&gt;ReadHeaderTimeout&lt;/code&gt; covers the time that the request header
(from the client) is completely read.&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref3&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;Accept                                                          New request
                                         ┌──────────ServeHTTP─────────────┐
┏━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━┓
┃ Wait ┃ TLS Handshake ┃ Request Headers ┃ Request Body ┃ Response ┃ Idle ┃
┗━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━┛
│                      └ReadHeaderTimeout┘              │          └──┬───┘
│                                        │              │       IdleTimeout
└──────────────ReadTimeout──────────────────────────────┘          │       
│                                        │                         │       
└┄┄┄┄┄┄┄┄┄┄┄┄┄┄WriteTimeout┄┄┄┄┄┄┄┄┄┄┄┄┄┄┴───────WriteTimeout──────┘       &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Above an overview of the timeouts in the request/response cycle, note
that when TLS is used, &lt;code&gt;WriteTimeout&lt;/code&gt; will include the TLS
handshake, as well as the header read as well.&lt;/p&gt;
&lt;p&gt;We continue with setting the field &lt;code&gt;MaxHeaderBytes&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb6&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb6-1&#34;&gt;&lt;a href=&#34;#cb6-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;MaxHeaderBytes&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;  &lt;span class=&#34;co&#34;&gt;// Resolves to 1048576, and equals 1MB&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This field makes sure that there is a maximum of bytes that the
server will read when parsing the headers. Note that it does not limit
the size of incoming request body. You’ll need to implement
&lt;code&gt;http.MaxBytesReader()&lt;/code&gt; in your handlers.&lt;a href=&#34;#fn4&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref4&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt; And
we can implement it as follows:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb7&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb7-1&#34;&gt;&lt;a href=&#34;#cb7-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-2&#34;&gt;&lt;a href=&#34;#cb7-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-3&#34;&gt;&lt;a href=&#34;#cb7-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;MaxBytesReader&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-4&#34;&gt;&lt;a href=&#34;#cb7-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        io&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;WriteString&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;hello, world&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-5&#34;&gt;&lt;a href=&#34;#cb7-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-6&#34;&gt;&lt;a href=&#34;#cb7-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Remember, that this is just an example, and you should think about
the specific timeouts, and limits that serve your use-case.&lt;/p&gt;
&lt;h2 id=&#34;tls&#34;&gt;TLS&lt;/h2&gt;
&lt;p&gt;To make our HTTP webserver more secure, and expose it to the internet
we do well to implement a secure communication between the client and
the server. And we do that by leveraging TLS, which stands for:
&lt;em&gt;Transport Layer Security&lt;/em&gt;. We use it to secure communication
between the webserver and the client over the internet. The protocol is
implemented at the application layer, directly on top of TCP, which
enables protocols ‘above’&lt;a href=&#34;#fn5&#34; class=&#34;footnote-ref&#34; id=&#34;fnref5&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt; it to continue working while
leveraging the added communication security. &lt;span class=&#34;citation&#34;
data-cites=&#34;grigorik2013&#34;&gt;(&lt;a href=&#34;#ref-grigorik2013&#34;
role=&#34;doc-biblioref&#34;&gt;Grigorik 2013, 48&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First, let’s explore what TLS exactly does, so we get a sense of why
we choose certain methods when coding an implementation in Go.&lt;/p&gt;
&lt;h3 id=&#34;tls_overview&#34;&gt;Overview&lt;/h3&gt;
&lt;p&gt;There are 3 main components to TLS:&lt;/p&gt;
&lt;ol type=&#34;1&#34;&gt;
&lt;li&gt;Encryption: hides the data being transferred from third
parties.&lt;/li&gt;
&lt;li&gt;Authentication: ensures that the parties exchanging information are
who they claim to be.&lt;/li&gt;
&lt;li&gt;Integrity: verifies that the data has not been forged or tampered
with.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We hide the data by encrypting it, and we do that by using
&lt;em&gt;symmetric cryptography&lt;/em&gt;.&lt;a href=&#34;#fn6&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref6&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt; This means that a
generated key is used for both to encrypt and decrypt the data. The keys
used for this are created for each connection, and are based on a shared
secret that was negotiated at the start of the session. &lt;span
class=&#34;citation&#34; data-cites=&#34;wiki_tls wiki_symm&#34;&gt;(&lt;a
href=&#34;#ref-wiki_tls&#34; role=&#34;doc-biblioref&#34;&gt;Wikipedia 2020f&lt;/a&gt;, &lt;a
href=&#34;#ref-wiki_symm&#34; role=&#34;doc-biblioref&#34;&gt;2020e&lt;/a&gt;)&lt;/span&gt; The chosen
‘ciphersuite’ determines details such as, which shared encryption keys,
or session keys, will be used for the session. &lt;span class=&#34;citation&#34;
data-cites=&#34;cloudflare_tls&#34;&gt;(&lt;a href=&#34;#ref-cloudflare_tls&#34;
role=&#34;doc-biblioref&#34;&gt;Cloudflare 2020&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We make sure that the identity of both the server and client is
correct by using &lt;em&gt;public-key cryptography&lt;/em&gt;. In contrast to
&lt;em&gt;symmetric cryptography&lt;/em&gt;, this employs &lt;em&gt;asymmetric
cryptography&lt;/em&gt; where a pair of keys are used: the public and private
key. Everyone with the public key is able to encrypt data, but only the
holder of the private key is able to decrypt it. In our case that will
be the server. &lt;span class=&#34;citation&#34;
data-cites=&#34;wiki_tls wiki_asymm&#34;&gt;(&lt;a href=&#34;#ref-wiki_tls&#34;
role=&#34;doc-biblioref&#34;&gt;Wikipedia 2020f&lt;/a&gt;, &lt;a href=&#34;#ref-wiki_asymm&#34;
role=&#34;doc-biblioref&#34;&gt;2020d&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This &lt;em&gt;public-key cryptography&lt;/em&gt; is used only during session
setup of the TLS tunnel. The server provides its public key to the
client, and then the client generates a &lt;em&gt;symmetric key&lt;/em&gt;, which it
encrypts with the server’s public key, and returns the encrypted
&lt;em&gt;symmetric key&lt;/em&gt;, with which the data between the client and
server is encrypted, to the server. Finally, the server can decrypt the
&lt;em&gt;symmetric key&lt;/em&gt; sent from the client with its own private key.
This &lt;em&gt;symmetric key&lt;/em&gt; is then used for all further encrypted
communication since now both the server and client have the key to
encrypt and decrypt data. &lt;span class=&#34;citation&#34;
data-cites=&#34;grigorik2013&#34;&gt;(&lt;a href=&#34;#ref-grigorik2013&#34;
role=&#34;doc-biblioref&#34;&gt;Grigorik 2013, 52&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;When the data is encrypted and authenticated, it will be signed with
a &lt;em&gt;message authentication code&lt;/em&gt; (MAC) to ensure its integrity.
&lt;span class=&#34;citation&#34; data-cites=&#34;cloudflare_tls&#34;&gt;(&lt;a
href=&#34;#ref-cloudflare_tls&#34; role=&#34;doc-biblioref&#34;&gt;Cloudflare
2020&lt;/a&gt;)&lt;/span&gt; The MAC algorithm is a one-way cryptographic hash
function, in a way a sort of checksum. The keys in order to generate the
MAC are negotiated between the client and server. When a TLS record is
sent, a MAC value is generated and appended to that message. The
receiver is then able to compute and verify the MAC value to ensure that
the message hasn’t been changed or altered. &lt;span class=&#34;citation&#34;
data-cites=&#34;grigorik2013&#34;&gt;(&lt;a href=&#34;#ref-grigorik2013&#34;
role=&#34;doc-biblioref&#34;&gt;Grigorik 2013, 49&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;OK, so how is this all implemented? A TLS connection between the
server and client will be established by the so-called “TLS Handshake”.
This “handshake” creates the agreement on the version of the TLS
protocol, the ciphersuite, and certificate verification. In the
following diagram we’ll go over the steps of how such a “handshake” is
realised.&lt;a href=&#34;#fn7&#34; class=&#34;footnote-ref&#34; id=&#34;fnref7&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;ascii nohighlight&#34;&gt;&lt;code&gt;                                                     
        ┌─┐                             ┌─┐          
        └┬┘                             ╞ │          
        ▔▔▔                             └─┘          
       client                          server        
┏━━━━━━━━━━━━━━━━━━━┓                                
┃ SYN               ┃ ――――――――▶ ┏━━━━━━━━━━━━━━━━━━━┓
┗━━━━━━━━━━━━━━━━━━━┛           ┃ SYN ACK           ┃
┏━━━━━━━━━━━━━━━━━━━┓ ◀―――――――― ┗━━━━━━━━━━━━━━━━━━━┛
┃ ACK               ┃ ――――――――▶ ┏━━━━━━━━━━━━━━━━━━━┓
┣━━━━━━━━━━━━━━━━━━━┫           ┃ ServerHello       ┃
┃ ClientHello       ┃           ┣━━━━━━━━━━━━━━━━━━━┫
┗━━━━━━━━━━━━━━━━━━━┛           ┃ Certificate       ┃
┏━━━━━━━━━━━━━━━━━━━┓ ◀―――――――― ┣━━━━━━━━━━━━━━━━━━━┫
┃ ClientKeyExchange ┃           ┃ ServerHelloDone   ┃
┣━━━━━━━━━━━━━━━━━━━┫           ┗━━━━━━━━━━━━━━━━━━━┛
┃ ChangeCipherSpec  ┃ ――――――――▶ ┏━━━━━━━━━━━━━━━━━━━┓
┣━━━━━━━━━━━━━━━━━━━┫           ┃ ChangeCipherSpec  ┃
┃ Finished          ┃           ┣━━━━━━━━━━━━━━━━━━━┫
┗━━━━━━━━━━━━━━━━━━━┛           ┃ Finished          ┃
┏━━━━━━━━━━━━━━━━━━━┓ ◀―――――――― ┗━━━━━━━━━━━━━━━━━━━┛
┃ Application Data  ┃           ┏━━━━━━━━━━━━━━━━━━━┓
┗━━━━━━━━━━━━━━━━━━━┛ ――――――――▶ ┃ Application Data  ┃
                                ┗━━━━━━━━━━━━━━━━━━━┛&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;SYN&lt;/strong&gt;, &lt;strong&gt;SYN ACK&lt;/strong&gt;, &lt;strong&gt;ACK&lt;/strong&gt;
- Start with a TCP connection, and a three-way handshake. After this TCP
connection, the TLS Handshake begins.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ClientHello&lt;/strong&gt; - Client sends a
&lt;code&gt;ClientHello&lt;/code&gt; message and it will include a number of
specifications in plain text, such as version of TLS protocol, list of
supported ciphersuites (ciphers and hash functions).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ServerHello&lt;/strong&gt;, &lt;strong&gt;Certificate&lt;/strong&gt;,
&lt;strong&gt;ServerHelloDone&lt;/strong&gt; - Server selects the version, and the
ciphersuite. It then presents its certificate. This contains: the domain
name, the trusted certificate authority (CA), and the public encryption
key, and sends its response back to the client.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ClientKeyExchange&lt;/strong&gt;,
&lt;strong&gt;ChangeCipherSpec&lt;/strong&gt;, &lt;strong&gt;Finished&lt;/strong&gt; - Client
confirms the validity of the certificate. Then the client generates a
new symmetric key, and encrypts that key with the server’s public
key.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ChangeCipherSpec&lt;/strong&gt;, &lt;strong&gt;Finished&lt;/strong&gt; - The
server decrypts the symmetric key sent by the client, checks the
integrity of the message by verifying the MAC, and returns an encrypted
“Finished” message back to the client.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Application Data&lt;/strong&gt; - The client decrypts the message
with the symmetric key it generated earlier, verifies the MAC, and if
everything checks out application data can be sent.&lt;/p&gt;
&lt;p&gt;So, this gives us a bit of a basic understanding of how TLS works. In
the next section we’ll implement it into our HTTP webserver.&lt;/p&gt;
&lt;h3 id=&#34;tls_implementation&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;We’ll be using the &lt;code&gt;crypto/tls&lt;/code&gt; package for implementing
TLS for our HTTP webserver.&lt;a href=&#34;#fn8&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref8&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;8&lt;/sup&gt;&lt;/a&gt; We also take note of the
&lt;a href=&#34;https://ssl-config.mozilla.org/&#34;&gt;Mozilla SSL Configuration
Generator&lt;/a&gt;, which allows us to implement the “Modern” recommended
configuration made by Mozilla. &lt;span class=&#34;citation&#34;
data-cites=&#34;mozilla_tls&#34;&gt;(&lt;a href=&#34;#ref-mozilla_tls&#34;
role=&#34;doc-biblioref&#34;&gt;Mozilla 2020a&lt;/a&gt;)&lt;/span&gt; When implemented, our
code will look something like this:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb9&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb9-1&#34;&gt;&lt;a href=&#34;#cb9-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// server.go&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-2&#34;&gt;&lt;a href=&#34;#cb9-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;package&lt;/span&gt; main&lt;/span&gt;
&lt;span id=&#34;cb9-3&#34;&gt;&lt;a href=&#34;#cb9-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-4&#34;&gt;&lt;a href=&#34;#cb9-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-5&#34;&gt;&lt;a href=&#34;#cb9-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/tls&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-6&#34;&gt;&lt;a href=&#34;#cb9-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;flag&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-7&#34;&gt;&lt;a href=&#34;#cb9-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;log&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-8&#34;&gt;&lt;a href=&#34;#cb9-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;net/http&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-9&#34;&gt;&lt;a href=&#34;#cb9-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;time&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-10&#34;&gt;&lt;a href=&#34;#cb9-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-11&#34;&gt;&lt;a href=&#34;#cb9-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-12&#34;&gt;&lt;a href=&#34;#cb9-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-13&#34;&gt;&lt;a href=&#34;#cb9-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flgCert &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;cert&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;cert.pem&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;path to cert&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-14&#34;&gt;&lt;a href=&#34;#cb9-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flgKey &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;key&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;key.pem&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;path to key&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-15&#34;&gt;&lt;a href=&#34;#cb9-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Parse&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-16&#34;&gt;&lt;a href=&#34;#cb9-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-17&#34;&gt;&lt;a href=&#34;#cb9-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    cert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;LoadX509KeyPair&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;flgCert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgKey&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-18&#34;&gt;&lt;a href=&#34;#cb9-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-19&#34;&gt;&lt;a href=&#34;#cb9-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-20&#34;&gt;&lt;a href=&#34;#cb9-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-21&#34;&gt;&lt;a href=&#34;#cb9-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-22&#34;&gt;&lt;a href=&#34;#cb9-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    tlsConfig &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Config&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-23&#34;&gt;&lt;a href=&#34;#cb9-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Certificates&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;             &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificate&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;cert&lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-24&#34;&gt;&lt;a href=&#34;#cb9-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        CipherSuites&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;             &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-25&#34;&gt;&lt;a href=&#34;#cb9-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        PreferServerCipherSuites&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-26&#34;&gt;&lt;a href=&#34;#cb9-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        MinVersion&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;               tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;VersionTLS13&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-27&#34;&gt;&lt;a href=&#34;#cb9-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        CurvePreferences&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;CurveID&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-28&#34;&gt;&lt;a href=&#34;#cb9-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;CurveP256&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-29&#34;&gt;&lt;a href=&#34;#cb9-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;X25519&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-30&#34;&gt;&lt;a href=&#34;#cb9-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-31&#34;&gt;&lt;a href=&#34;#cb9-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-32&#34;&gt;&lt;a href=&#34;#cb9-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-33&#34;&gt;&lt;a href=&#34;#cb9-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;go&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-34&#34;&gt;&lt;a href=&#34;#cb9-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        srv &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-35&#34;&gt;&lt;a href=&#34;#cb9-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-36&#34;&gt;&lt;a href=&#34;#cb9-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-37&#34;&gt;&lt;a href=&#34;#cb9-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Redirect&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-38&#34;&gt;&lt;a href=&#34;#cb9-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                    w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-39&#34;&gt;&lt;a href=&#34;#cb9-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                    &lt;span class=&#34;st&#34;&gt;&#34;https://&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Host&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;URL&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;RequestURI&lt;span class=&#34;op&#34;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-40&#34;&gt;&lt;a href=&#34;#cb9-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                    http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;StatusMovedPermanently&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-41&#34;&gt;&lt;a href=&#34;#cb9-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                &lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-42&#34;&gt;&lt;a href=&#34;#cb9-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;op&#34;&gt;}),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-43&#34;&gt;&lt;a href=&#34;#cb9-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-44&#34;&gt;&lt;a href=&#34;#cb9-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-45&#34;&gt;&lt;a href=&#34;#cb9-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-46&#34;&gt;&lt;a href=&#34;#cb9-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-47&#34;&gt;&lt;a href=&#34;#cb9-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    srv &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-48&#34;&gt;&lt;a href=&#34;#cb9-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:https&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-49&#34;&gt;&lt;a href=&#34;#cb9-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-50&#34;&gt;&lt;a href=&#34;#cb9-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-51&#34;&gt;&lt;a href=&#34;#cb9-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;MaxBytesReader&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-52&#34;&gt;&lt;a href=&#34;#cb9-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Write&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;hello, world&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-53&#34;&gt;&lt;a href=&#34;#cb9-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-54&#34;&gt;&lt;a href=&#34;#cb9-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-55&#34;&gt;&lt;a href=&#34;#cb9-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        TLSConfig&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; tlsConfig&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-56&#34;&gt;&lt;a href=&#34;#cb9-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        TLSNextProto&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-57&#34;&gt;&lt;a href=&#34;#cb9-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;kw&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Conn&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Handler&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-58&#34;&gt;&lt;a href=&#34;#cb9-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-59&#34;&gt;&lt;a href=&#34;#cb9-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        ReadTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;    &lt;span class=&#34;dv&#34;&gt;5&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-60&#34;&gt;&lt;a href=&#34;#cb9-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        WriteTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;   &lt;span class=&#34;dv&#34;&gt;10&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-61&#34;&gt;&lt;a href=&#34;#cb9-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        IdleTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;    &lt;span class=&#34;dv&#34;&gt;120&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-62&#34;&gt;&lt;a href=&#34;#cb9-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        MaxHeaderBytes&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-63&#34;&gt;&lt;a href=&#34;#cb9-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-64&#34;&gt;&lt;a href=&#34;#cb9-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-65&#34;&gt;&lt;a href=&#34;#cb9-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServeTLS&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb9-66&#34;&gt;&lt;a href=&#34;#cb9-66&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again, let’s go over the code and what investigate what we’ve
changed.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb10&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb10-1&#34;&gt;&lt;a href=&#34;#cb10-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;flgCert &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;cert&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;cert.pem&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;path to cert&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-2&#34;&gt;&lt;a href=&#34;#cb10-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;flgKey &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;key&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;key.pem&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;path to key&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-3&#34;&gt;&lt;a href=&#34;#cb10-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Parse&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we’re parsing some command line arguments that we will use later
on. Eventually, we’ll create self-signed certificates, whose paths we
pass into our program here.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb11&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb11-1&#34;&gt;&lt;a href=&#34;#cb11-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;cert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;LoadX509KeyPair&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;flgCert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgKey&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-2&#34;&gt;&lt;a href=&#34;#cb11-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-3&#34;&gt;&lt;a href=&#34;#cb11-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-4&#34;&gt;&lt;a href=&#34;#cb11-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;LoadX509KeyPair()&lt;/code&gt; function reads and parses a
public/private key pair from a pair of files. Both files must be ‘PEM’
(Privacy-Enhanced Mail) encoded.&lt;a href=&#34;#fn9&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref9&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;9&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb12&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb12-1&#34;&gt;&lt;a href=&#34;#cb12-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;tlsConfig &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Config&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb12-2&#34;&gt;&lt;a href=&#34;#cb12-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;co&#34;&gt;// ...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb12-3&#34;&gt;&lt;a href=&#34;#cb12-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we create the &lt;code&gt;tls.Config&lt;/code&gt; struct that we use to
configure our TLS functionality for our HTTP webserver.&lt;a href=&#34;#fn10&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref10&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb13&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb13-1&#34;&gt;&lt;a href=&#34;#cb13-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;Certificates&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificate&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;cert&lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To the &lt;code&gt;Certificates&lt;/code&gt; field we add the certificates we’ve
loaded with the parsed public/private key pair that we provided.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb14&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb14-1&#34;&gt;&lt;a href=&#34;#cb14-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;CipherSuites&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The field &lt;code&gt;CipherSuites&lt;/code&gt; is a list of ciphersuites that
the server will support. This is up to TLS version 1.2, when using
version 1.3 this isn’t configurable. Because we want to use the most up
to date version we keep it empty, which results in a default list of
ciphersuites to be used with a preference order based on hardware
performance.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb15&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb15-1&#34;&gt;&lt;a href=&#34;#cb15-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;PreferServerCipherSuites&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This boolean controls the server’s preferred ciphersuite to use, as
provided by the &lt;code&gt;CipherSuites&lt;/code&gt;, when false it will select the
client’s preferred ciphersuite. Setting this will ensure that safer and
faster ciphersuites are used. &lt;span class=&#34;citation&#34;
data-cites=&#34;valsorda2016a&#34;&gt;(&lt;a href=&#34;#ref-valsorda2016a&#34;
role=&#34;doc-biblioref&#34;&gt;Valsorda 2016&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb16&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb16-1&#34;&gt;&lt;a href=&#34;#cb16-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;MinVersion&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;VersionTLS13&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we can specify the minimal TLS version the server is going to
use. We’ll select version recommended by the “Modern” configuration from
Mozilla. &lt;span class=&#34;citation&#34; data-cites=&#34;mozilla_tls&#34;&gt;(&lt;a
href=&#34;#ref-mozilla_tls&#34; role=&#34;doc-biblioref&#34;&gt;Mozilla
2020a&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb17&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb17-1&#34;&gt;&lt;a href=&#34;#cb17-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;CurvePreferences&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;CurveID&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb17-2&#34;&gt;&lt;a href=&#34;#cb17-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;CurveP256&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb17-3&#34;&gt;&lt;a href=&#34;#cb17-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;X25519&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb17-4&#34;&gt;&lt;a href=&#34;#cb17-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The array &lt;code&gt;CurvePreferences&lt;/code&gt; contain the elliptic curves
that will be used in an ECDHE handshake. We again use the Mozilla
recommendation, however without &lt;code&gt;tls.CurveP384&lt;/code&gt;, because a
client using &lt;code&gt;tls.CurveP384&lt;/code&gt; would cause up to a second of
CPU to be consumed on the server. &lt;span class=&#34;citation&#34;
data-cites=&#34;valsorda2016a&#34;&gt;(&lt;a href=&#34;#ref-valsorda2016a&#34;
role=&#34;doc-biblioref&#34;&gt;Valsorda 2016&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb18&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb18-1&#34;&gt;&lt;a href=&#34;#cb18-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;cf&#34;&gt;go&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-2&#34;&gt;&lt;a href=&#34;#cb18-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    srv &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-3&#34;&gt;&lt;a href=&#34;#cb18-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-4&#34;&gt;&lt;a href=&#34;#cb18-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-5&#34;&gt;&lt;a href=&#34;#cb18-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Redirect&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-6&#34;&gt;&lt;a href=&#34;#cb18-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-7&#34;&gt;&lt;a href=&#34;#cb18-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                &lt;span class=&#34;st&#34;&gt;&#34;https://&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Host&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;URL&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;RequestURI&lt;span class=&#34;op&#34;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-8&#34;&gt;&lt;a href=&#34;#cb18-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;StatusMovedPermanently&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-9&#34;&gt;&lt;a href=&#34;#cb18-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-10&#34;&gt;&lt;a href=&#34;#cb18-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-11&#34;&gt;&lt;a href=&#34;#cb18-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-12&#34;&gt;&lt;a href=&#34;#cb18-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-13&#34;&gt;&lt;a href=&#34;#cb18-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we create, and run an additional &lt;code&gt;http.Server&lt;/code&gt; in a
goroutine that will redirect all ‘http’ traffic to our ‘https’ port.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb19&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb19-1&#34;&gt;&lt;a href=&#34;#cb19-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:https&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For our ‘main’ &lt;code&gt;http.Server&lt;/code&gt; we update the port number and
set it to &lt;code&gt;:https&lt;/code&gt; to resemble that we’re communicating on
port 443.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb20&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb20-1&#34;&gt;&lt;a href=&#34;#cb20-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;TLSConfig&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; tlsConfig&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we load our TLS configuration we’ve defined before.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb21&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb21-1&#34;&gt;&lt;a href=&#34;#cb21-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;TLSNextProto&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb21-2&#34;&gt;&lt;a href=&#34;#cb21-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Conn&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Handler&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb21-3&#34;&gt;&lt;a href=&#34;#cb21-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Setting &lt;code&gt;TLSNextProto&lt;/code&gt; to an empty map will disable HTTP/2
for this server. If you want to enable HTTP/2 set it to &lt;code&gt;nil&lt;/code&gt;
or remove the field. Since Go 1.6 it is enabled by default. &lt;span
class=&#34;citation&#34; data-cites=&#34;valsorda2016a&#34;&gt;(&lt;a
href=&#34;#ref-valsorda2016a&#34; role=&#34;doc-biblioref&#34;&gt;Valsorda
2016&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb22&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb22-1&#34;&gt;&lt;a href=&#34;#cb22-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServeTLS&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because we’re now using TLS, we start the server with
&lt;code&gt;ListenAndServeTLS()&lt;/code&gt;. It will then listen on the
&lt;code&gt;serv.Addr&lt;/code&gt; for incoming TLS connections. Because we already
set the paths of the key and cert in the &lt;code&gt;tlsConfig&lt;/code&gt;, we can
set &lt;code&gt;certFile&lt;/code&gt; and &lt;code&gt;keyFile&lt;/code&gt; arguments to empty
strings.&lt;/p&gt;
&lt;p&gt;We’ve added TLS functionality to our HTTP webserver, but we’re still
missing one part of the TLS components: the certificate.&lt;/p&gt;
&lt;h3 id=&#34;tls_cert&#34;&gt;Certificates&lt;/h3&gt;
&lt;p&gt;We’re almost ready to test out the program, but we need have the
&lt;code&gt;cert&lt;/code&gt; and &lt;code&gt;key&lt;/code&gt; file. As was mentioned before
with the 3 main components TLS consists of we need to ensure the
authentication between the server and the client. Meaning that the
parties exchanging information, are who they claim to be. This is
employed by using &lt;em&gt;public-key cryptography&lt;/em&gt;, with the creation of
a public and private key. Where everyone with the public key is able to
encrypt data, but only the holder of the private key is able to decrypt
it.&lt;/p&gt;
&lt;p&gt;So what methods are there to trust someone?&lt;/p&gt;
&lt;ol type=&#34;1&#34;&gt;
&lt;li&gt;Manually specified certificates, in which you manually import any
certificate you trust.&lt;/li&gt;
&lt;li&gt;A Certificate Authority (CA), where a third party generates the
certificate that both parties trust.&lt;/li&gt;
&lt;li&gt;The operating system also contains a list with well-known
certificate authorities that it trusts.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The most common solution is to make use of a ‘Certificate Authority’.
However, first we’re going to create a self-signed certificate, because
using a CA to generate a certificate will come with some costs, because
you’ll be using a service to verify, audit, and revoke when a
certificate is misused or compromised. And it’ll give us a bit more
information on how such a thing is generated. &lt;span class=&#34;citation&#34;
data-cites=&#34;grigorik2013&#34;&gt;(&lt;a href=&#34;#ref-grigorik2013&#34;
role=&#34;doc-biblioref&#34;&gt;Grigorik 2013, 59&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;By checking out the “Modern” configuration again from Mozilla, we can
see that it states that the certificate type should be ‘ECDSA (P-256)’&lt;a
href=&#34;#fn11&#34; class=&#34;footnote-ref&#34; id=&#34;fnref11&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;11&lt;/sup&gt;&lt;/a&gt;, which stand for elliptical curve
digital signature algorithm, and that the maximum certificate lifespan
should be 90 days. &lt;span class=&#34;citation&#34; data-cites=&#34;mozilla_tls&#34;&gt;(&lt;a
href=&#34;#ref-mozilla_tls&#34; role=&#34;doc-biblioref&#34;&gt;Mozilla
2020a&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We’ll be using the &lt;code&gt;openssl req&lt;/code&gt; command for this from the
commandline, which is a certificate request and certificate generating
utility.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;# -X509
#   Output a self-signed certificate instead of a certificate request.
#
# -nodes
#   No DES, private key not protected by a passphrase, thus it won&#39;t be
#   encrypted.
#
# -newkey ec
#   Create a new certificate request and a new EC private key, usable for
#   both ECDSA or ECDH algorithms
#
# -pkeyopt ec_paramgen_curve:prime256v1
#   Set the public key algorithm ec_paramgen_curve to prime256v1
#
# -keyout key.pem
#   Filename to write the private key to.
#
# -out cert.pem
#   Specifies output filename to write to.
#
# -days 90
#   When using -X509 this specifies the number of days to certify for.

$ openssl req -x509 -nodes -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
    -keyout key.pem -out cert.pem -days 90&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will output both the key and cert files in the PEM format,
exactly what we needed for &lt;code&gt;tls.Load509KeyPair()&lt;/code&gt; function.
The ‘X509’ is the format for public-key certificates, an X.509
certificate contains a public key and an identity (a hostname, or an
organization, or an individual) &lt;span class=&#34;citation&#34;
data-cites=&#34;wiki_x509&#34;&gt;(&lt;a href=&#34;#ref-wiki_x509&#34;
role=&#34;doc-biblioref&#34;&gt;Wikipedia 2020g&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We can then inspect the content of the certificate by again using
&lt;code&gt;openssl&lt;/code&gt;. By using the &lt;code&gt;openssl x509&lt;/code&gt; command we
can look at the contents of a ‘X509’ certificate.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;# -text
#   Prints out the certificate in text form.
#
# -in
#   Specifies the input filename to a certificate from. 
#
# -noout
#   Prevents output of the encoded version of the actual certificate.

$ openssl x509 -text -in cert.pem -noout

Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            6b:41:90:1d:ad:c2:bc:0b:9b:61:f5:14:f8:06:92:70:49:d7:99:81
        Signature Algorithm: ecdsa-with-SHA256
        Issuer: C = NL, ST = Some-State, L = Amsterdam, O = Internet Widgets Pty Ltd
        Validity
            Not Before: Apr 15 18:24:08 2020 GMT
            Not After : Jul 14 18:24:08 2020 GMT
        Subject: C = NL, ST = Some-State, L = Amsterdam, O = Internet Widgets Pty Ltd
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:a6:fa:3c:86:99:a9:30:04:65:7f:00:7a:18:e2:
                    bc:4f:b2:69:45:75:b5:a0:06:37:2c:08:d9:2e:d5:
                    bc:a5:e5:14:fb:0a:20:28:b6:c8:e7:30:78:4d:be:
                    7f:5f:12:2c:6e:0a:76:4d:e9:2b:38:c8:9c:cf:d2:
                    db:2b:22:9d:1f
                ASN1 OID: prime256v1
                NIST CURVE: P-256
        X509v3 extensions:
            X509v3 Subject Key Identifier:
                75:BB:CA:93:7A:CF:FB:CF:E5:0A:FB:1C:32:9E:C8:A9:61:D0:76:3F
            X509v3 Authority Key Identifier:
                keyid:75:BB:CA:93:7A:CF:FB:CF:E5:0A:FB:1C:32:9E:C8:A9:61:D0:76:3F

            X509v3 Basic Constraints: critical
                CA:TRUE
    Signature Algorithm: ecdsa-with-SHA256
         30:45:02:21:00:b4:5b:81:65:bd:6e:48:a8:f4:fb:d5:47:1a:
         a0:e2:44:ec:a0:90:7f:b0:9c:de:ba:64:33:d6:98:c3:97:60:
         ba:02:20:26:41:74:a9:bc:c9:78:8b:0e:e3:12:c6:fd:ba:10:
         93:87:cb:8f:4b:15:99:e3:6e:89:d2:f6:b2:e0:48:bb:fc&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we can see some important information such as the issuer, its
validity, and the public key. With this clients knows how to recognize
it when this server has signed a message with the corresponding private
key.&lt;/p&gt;
&lt;h3 id=&#34;tls_test&#34;&gt;Testing it out&lt;/h3&gt;
&lt;p&gt;Now, that we’ve got the certificate ready, let’s test out our
program.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ go run server.go -cert=cert.pem -key=key.pem&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When our server is running, we’ll be able to test it with
&lt;code&gt;curl&lt;/code&gt;. Note that we’re going to add the
&lt;code&gt;--insecure&lt;/code&gt; flag to our command. This is because we’ve
self-signed the certificate and &lt;code&gt;curl&lt;/code&gt; rightfully doesn’t
trust that. We’re also adding the &lt;code&gt;--verbose&lt;/code&gt; flag to output
more information about the request.&lt;a href=&#34;#fn12&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref12&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;12&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ curl --verbose --insecure https://localhost

*   Trying ::1:443...
* Connected to localhost (::1) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=NL; ST=Some-State; O=Internet Widgets Pty Ltd
*  start date: Apr  3 09:20:12 2020 GMT
*  expire date: Jul  2 09:20:12 2020 GMT
*  issuer: C=NL; ST=Some-State; O=Internet Widgets Pty Ltd
*  SSL certificate verify result: self signed certificate (18), continuing anyway.
&gt; GET / HTTP/1.1
&gt; Host: localhost:443
&gt; User-Agent: curl/7.52.1
&gt; Accept: */*
&gt;
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* Mark bundle as not supporting multiuse
&lt; HTTP/1.1 200 OK
&lt; Strict-Transport-Security: max-age=63072000
&lt; Date: Fri, 03 Apr 2020 09:20:15 GMT
&lt; Content-Length: 14
&lt; Content-Type: text/plain; charset=utf-8
&lt;
hello, world
* Connection #0 to host localhost left intact&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;OK, that’s pretty cool. We’re able to see exactly what the
request/response cycle is like, including the ‘TLS Handshake’ that we’ve
explored in the ‘TLS’ section. You can however see the following line:
&lt;code&gt;SSL certificate verify result: self signed certificate (18), continuing anyway&lt;/code&gt;.
So is there however a possibility to make &lt;code&gt;curl&lt;/code&gt; trust our
certificate without skipping and/or ignoring the certificate
verification? We can with &lt;a
href=&#34;https://github.com/FiloSottile/mkcert&#34;&gt;&lt;code&gt;mkcert&lt;/code&gt;&lt;/a&gt;,
and it allows us to create locally-trusted development certificates. It
automatically creates and installs a local CA in the system root store,
and generates locally-trusted certificates. So let’s create one for
&lt;code&gt;localhost&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ mkcert -install
$ mkcert -ecdsa -key-file=key.pem -cert-file=cert.pem localhost&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, run restart the server, and make another request with
&lt;code&gt;curl&lt;/code&gt; without the &lt;code&gt;--insecure&lt;/code&gt; flag.&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ curl --verbose https://localhost

*   Trying ::1:443...
* Connected to localhost (127.0.0.1) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: O=mkcert development certificate; OU=jp@aztlan
*  start date: Jun  1 00:00:00 2019 GMT
*  expire date: Apr 15 18:58:07 2030 GMT
*  subjectAltName: host &#34;localhost&#34; matched cert&#39;s &#34;localhost&#34;
*  issuer: O=mkcert development CA; OU=jp@aztlan; CN=mkcert jp@aztlan
*  SSL certificate verify ok.
&gt; GET / HTTP/1.1
&gt; Host: localhost:443
&gt; User-Agent: curl/7.52.1
&gt; Accept: */*
&gt;
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* Mark bundle as not supporting multiuse
&lt; HTTP/1.1 200 OK
&lt; Date: Wed, 15 Apr 2020 19:07:29 GMT
&lt; Content-Length: 13
&lt; Content-Type: text/plain; charset=utf-8
&lt;
hello, world
* Connection #0 to host localhost left intact&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, we aren’t able to actually use this in production, since we
still used a self-signed certificate. Considering getting a certificate
from a CA has some costs associated with it, we’ll create our
certificate with ‘Let’s Encrypt’.&lt;/p&gt;
&lt;h2 id=&#34;letsencrypt&#34;&gt;Let’s Encrypt&lt;/h2&gt;
&lt;p&gt;‘&lt;a href=&#34;https://letsencrypt.org/about/&#34;&gt;Let’s Encrypt&lt;/a&gt;’ allows
us to create free TLS certificates from nonprofit Certificate Authority.
And we’ll be able to implement it in our program with the
&lt;code&gt;golang.org/x/crypto/acme/autocert&lt;/code&gt; package.&lt;a href=&#34;#fn13&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref13&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;13&lt;/sup&gt;&lt;/a&gt;
This package automatically generates, and when necessary, renews your
certificate. Let’s update our code and implement the
&lt;code&gt;autocert&lt;/code&gt; functionality.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb29&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb29-1&#34;&gt;&lt;a href=&#34;#cb29-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// server.go&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-2&#34;&gt;&lt;a href=&#34;#cb29-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;package&lt;/span&gt; main&lt;/span&gt;
&lt;span id=&#34;cb29-3&#34;&gt;&lt;a href=&#34;#cb29-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-4&#34;&gt;&lt;a href=&#34;#cb29-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-5&#34;&gt;&lt;a href=&#34;#cb29-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/tls&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-6&#34;&gt;&lt;a href=&#34;#cb29-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;flag&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-7&#34;&gt;&lt;a href=&#34;#cb29-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;log&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-8&#34;&gt;&lt;a href=&#34;#cb29-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;net/http&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-9&#34;&gt;&lt;a href=&#34;#cb29-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;time&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-10&#34;&gt;&lt;a href=&#34;#cb29-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-11&#34;&gt;&lt;a href=&#34;#cb29-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;golang.org/x/crypto/acme/autocert&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-12&#34;&gt;&lt;a href=&#34;#cb29-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-13&#34;&gt;&lt;a href=&#34;#cb29-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-14&#34;&gt;&lt;a href=&#34;#cb29-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-15&#34;&gt;&lt;a href=&#34;#cb29-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flgProd &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Bool&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;prod&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;run server in production&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-16&#34;&gt;&lt;a href=&#34;#cb29-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flgHost &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;host&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;host name of the server&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-17&#34;&gt;&lt;a href=&#34;#cb29-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flgCert &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;cert&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;cert.pem&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;path to cert&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-18&#34;&gt;&lt;a href=&#34;#cb29-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flgKey &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;key&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;key.pem&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;path to key&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-19&#34;&gt;&lt;a href=&#34;#cb29-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Parse&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-20&#34;&gt;&lt;a href=&#34;#cb29-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-21&#34;&gt;&lt;a href=&#34;#cb29-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    tlsConfig &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Config&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-22&#34;&gt;&lt;a href=&#34;#cb29-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        CipherSuites&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;             &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-23&#34;&gt;&lt;a href=&#34;#cb29-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        PreferServerCipherSuites&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-24&#34;&gt;&lt;a href=&#34;#cb29-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        MinVersion&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;               tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;VersionTLS13&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-25&#34;&gt;&lt;a href=&#34;#cb29-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        CurvePreferences&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;CurveID&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-26&#34;&gt;&lt;a href=&#34;#cb29-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;CurveP256&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-27&#34;&gt;&lt;a href=&#34;#cb29-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;X25519&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-28&#34;&gt;&lt;a href=&#34;#cb29-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-29&#34;&gt;&lt;a href=&#34;#cb29-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-30&#34;&gt;&lt;a href=&#34;#cb29-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-31&#34;&gt;&lt;a href=&#34;#cb29-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgProd &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-32&#34;&gt;&lt;a href=&#34;#cb29-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgHost &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-33&#34;&gt;&lt;a href=&#34;#cb29-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;please specify a host&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-34&#34;&gt;&lt;a href=&#34;#cb29-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-35&#34;&gt;&lt;a href=&#34;#cb29-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-36&#34;&gt;&lt;a href=&#34;#cb29-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        m &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Manager&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-37&#34;&gt;&lt;a href=&#34;#cb29-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            Prompt&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;     autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;AcceptTOS&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-38&#34;&gt;&lt;a href=&#34;#cb29-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            HostPolicy&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HostWhitelist&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;flgHost&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-39&#34;&gt;&lt;a href=&#34;#cb29-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            Cache&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;      autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;DirCache&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;certs&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-40&#34;&gt;&lt;a href=&#34;#cb29-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-41&#34;&gt;&lt;a href=&#34;#cb29-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-42&#34;&gt;&lt;a href=&#34;#cb29-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        tlsConfig&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;GetCertificate &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; m&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;GetCertificate&lt;/span&gt;
&lt;span id=&#34;cb29-43&#34;&gt;&lt;a href=&#34;#cb29-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-44&#34;&gt;&lt;a href=&#34;#cb29-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;go&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-45&#34;&gt;&lt;a href=&#34;#cb29-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            h &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; m&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HTTPHandler&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-46&#34;&gt;&lt;a href=&#34;#cb29-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; h&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-47&#34;&gt;&lt;a href=&#34;#cb29-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-48&#34;&gt;&lt;a href=&#34;#cb29-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;cf&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-49&#34;&gt;&lt;a href=&#34;#cb29-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        cert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;LoadX509KeyPair&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;flgCert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgKey&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-50&#34;&gt;&lt;a href=&#34;#cb29-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-51&#34;&gt;&lt;a href=&#34;#cb29-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-52&#34;&gt;&lt;a href=&#34;#cb29-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-53&#34;&gt;&lt;a href=&#34;#cb29-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-54&#34;&gt;&lt;a href=&#34;#cb29-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        tlsConfig&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificates &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificate&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;cert&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-55&#34;&gt;&lt;a href=&#34;#cb29-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-56&#34;&gt;&lt;a href=&#34;#cb29-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;go&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-57&#34;&gt;&lt;a href=&#34;#cb29-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            srv &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-58&#34;&gt;&lt;a href=&#34;#cb29-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-59&#34;&gt;&lt;a href=&#34;#cb29-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-60&#34;&gt;&lt;a href=&#34;#cb29-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                    http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Redirect&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-61&#34;&gt;&lt;a href=&#34;#cb29-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                        w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;https://&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Host&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;URL&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;RequestURI&lt;span class=&#34;op&#34;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-62&#34;&gt;&lt;a href=&#34;#cb29-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                        http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;StatusMovedPermanently&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-63&#34;&gt;&lt;a href=&#34;#cb29-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                    &lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-64&#34;&gt;&lt;a href=&#34;#cb29-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                &lt;span class=&#34;op&#34;&gt;}),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-65&#34;&gt;&lt;a href=&#34;#cb29-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-66&#34;&gt;&lt;a href=&#34;#cb29-66&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-67&#34;&gt;&lt;a href=&#34;#cb29-67&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-68&#34;&gt;&lt;a href=&#34;#cb29-68&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-69&#34;&gt;&lt;a href=&#34;#cb29-69&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-70&#34;&gt;&lt;a href=&#34;#cb29-70&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    srv &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-71&#34;&gt;&lt;a href=&#34;#cb29-71&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:https&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-72&#34;&gt;&lt;a href=&#34;#cb29-72&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-73&#34;&gt;&lt;a href=&#34;#cb29-73&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-74&#34;&gt;&lt;a href=&#34;#cb29-74&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;MaxBytesReader&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-75&#34;&gt;&lt;a href=&#34;#cb29-75&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Write&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;hello, world&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-76&#34;&gt;&lt;a href=&#34;#cb29-76&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-77&#34;&gt;&lt;a href=&#34;#cb29-77&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-78&#34;&gt;&lt;a href=&#34;#cb29-78&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        TLSConfig&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; tlsConfig&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-79&#34;&gt;&lt;a href=&#34;#cb29-79&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        TLSNextProto&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-80&#34;&gt;&lt;a href=&#34;#cb29-80&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;kw&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Conn&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Handler&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-81&#34;&gt;&lt;a href=&#34;#cb29-81&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-82&#34;&gt;&lt;a href=&#34;#cb29-82&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        ReadTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;    &lt;span class=&#34;dv&#34;&gt;5&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-83&#34;&gt;&lt;a href=&#34;#cb29-83&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        WriteTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;   &lt;span class=&#34;dv&#34;&gt;10&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-84&#34;&gt;&lt;a href=&#34;#cb29-84&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        IdleTimeout&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;    &lt;span class=&#34;dv&#34;&gt;120&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt; time&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Second&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-85&#34;&gt;&lt;a href=&#34;#cb29-85&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        MaxHeaderBytes&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-86&#34;&gt;&lt;a href=&#34;#cb29-86&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-87&#34;&gt;&lt;a href=&#34;#cb29-87&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-88&#34;&gt;&lt;a href=&#34;#cb29-88&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServeTLS&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb29-89&#34;&gt;&lt;a href=&#34;#cb29-89&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again, let’s go over the code and explore what we’ve changed.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb30&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb30-1&#34;&gt;&lt;a href=&#34;#cb30-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;flgProd &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Bool&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;prod&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;run server in production&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb30-2&#34;&gt;&lt;a href=&#34;#cb30-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;flgHost &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; flag&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;String&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;host&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;host name of the server&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We’ve added two new flags, &lt;code&gt;flgProd&lt;/code&gt; and
&lt;code&gt;flgHost&lt;/code&gt;. The flag &lt;code&gt;flgProd&lt;/code&gt; allows us to signal
to the program, by setting the command line flag, that the program is
running in production, and should be using the ‘Let’s Encrypt’
certificate created by &lt;code&gt;autocert&lt;/code&gt;. The flag
&lt;code&gt;flgHost&lt;/code&gt; is necessary for &lt;code&gt;autocert&lt;/code&gt; to let it
know for which host we’re generating a certificate.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb31&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb31-1&#34;&gt;&lt;a href=&#34;#cb31-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgProd &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-2&#34;&gt;&lt;a href=&#34;#cb31-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgHost &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;&#34;&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-3&#34;&gt;&lt;a href=&#34;#cb31-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;please specify a host&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-4&#34;&gt;&lt;a href=&#34;#cb31-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-5&#34;&gt;&lt;a href=&#34;#cb31-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-6&#34;&gt;&lt;a href=&#34;#cb31-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    m &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Manager&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-7&#34;&gt;&lt;a href=&#34;#cb31-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Prompt&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;     autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;AcceptTOS&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-8&#34;&gt;&lt;a href=&#34;#cb31-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        HostPolicy&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HostWhitelist&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;flgHost&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-9&#34;&gt;&lt;a href=&#34;#cb31-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        Cache&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;      autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;DirCache&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;certs&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-10&#34;&gt;&lt;a href=&#34;#cb31-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-11&#34;&gt;&lt;a href=&#34;#cb31-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-12&#34;&gt;&lt;a href=&#34;#cb31-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    tlsConfig&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;GetCertificate &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; m&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;GetCertificate&lt;/span&gt;
&lt;span id=&#34;cb31-13&#34;&gt;&lt;a href=&#34;#cb31-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-14&#34;&gt;&lt;a href=&#34;#cb31-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;go&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-15&#34;&gt;&lt;a href=&#34;#cb31-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        h &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; m&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HTTPHandler&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-16&#34;&gt;&lt;a href=&#34;#cb31-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; h&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-17&#34;&gt;&lt;a href=&#34;#cb31-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-18&#34;&gt;&lt;a href=&#34;#cb31-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;cf&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-19&#34;&gt;&lt;a href=&#34;#cb31-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    cert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;LoadX509KeyPair&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;flgCert&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;flgKey&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-20&#34;&gt;&lt;a href=&#34;#cb31-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-21&#34;&gt;&lt;a href=&#34;#cb31-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-22&#34;&gt;&lt;a href=&#34;#cb31-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-23&#34;&gt;&lt;a href=&#34;#cb31-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-24&#34;&gt;&lt;a href=&#34;#cb31-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    tlsConfig&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificates &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificate&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;cert&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-25&#34;&gt;&lt;a href=&#34;#cb31-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-26&#34;&gt;&lt;a href=&#34;#cb31-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;go&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-27&#34;&gt;&lt;a href=&#34;#cb31-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        srv &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;&amp;&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Server&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-28&#34;&gt;&lt;a href=&#34;#cb31-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            Addr&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-29&#34;&gt;&lt;a href=&#34;#cb31-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-30&#34;&gt;&lt;a href=&#34;#cb31-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Redirect&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-31&#34;&gt;&lt;a href=&#34;#cb31-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                    w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;https://&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Host&lt;span class=&#34;op&#34;&gt;+&lt;/span&gt;r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;URL&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;RequestURI&lt;span class=&#34;op&#34;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-32&#34;&gt;&lt;a href=&#34;#cb31-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                    http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;StatusMovedPermanently&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-33&#34;&gt;&lt;a href=&#34;#cb31-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;                &lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-34&#34;&gt;&lt;a href=&#34;#cb31-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;op&#34;&gt;}),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-35&#34;&gt;&lt;a href=&#34;#cb31-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-36&#34;&gt;&lt;a href=&#34;#cb31-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;srv&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-37&#34;&gt;&lt;a href=&#34;#cb31-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb31-38&#34;&gt;&lt;a href=&#34;#cb31-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We’ve removed the &lt;code&gt;Certificates&lt;/code&gt; field from
&lt;code&gt;tlsConfig&lt;/code&gt;. And with this conditional we choose either by
generating a certificate with &lt;code&gt;autocert&lt;/code&gt;, or using our
self-signed certificate.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb32&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb32-1&#34;&gt;&lt;a href=&#34;#cb32-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;m &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Manager&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb32-2&#34;&gt;&lt;a href=&#34;#cb32-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    Prompt&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;     autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;AcceptTOS&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb32-3&#34;&gt;&lt;a href=&#34;#cb32-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    HostPolicy&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HostWhitelist&lt;span class=&#34;op&#34;&gt;(*&lt;/span&gt;flgHost&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb32-4&#34;&gt;&lt;a href=&#34;#cb32-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    Cache&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt;      autocert&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;DirCache&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;certs&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb32-5&#34;&gt;&lt;a href=&#34;#cb32-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The field &lt;code&gt;Prompt&lt;/code&gt; specifies a function to accept a CA’s
Terms of Service, here we choose &lt;code&gt;autocert.AcceptTOS&lt;/code&gt; which
always accept the terms. The field &lt;code&gt;HostPolicy&lt;/code&gt; controls
which domains the &lt;code&gt;Manager&lt;/code&gt; will attempt to retrieve new
certificates for. It accepts a &lt;code&gt;HostPolicy&lt;/code&gt;, which specifies
which host names the &lt;code&gt;Manager&lt;/code&gt; is allowed to respond to. We
create that by calling &lt;code&gt;HostWhiteList()&lt;/code&gt; with the host name
for our server. The field &lt;code&gt;Cache&lt;/code&gt; is needed to write our key
and certificate to, so that we can keep reusing it with server restarts
for the days that the certificate is valid. We also do this because
there is a rate-limit for requesting certificates from Let’s Encrypt.&lt;a
href=&#34;#fn14&#34; class=&#34;footnote-ref&#34; id=&#34;fnref14&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;14&lt;/sup&gt;&lt;/a&gt; We set the directory to
&lt;code&gt;&#34;certs&#34;&lt;/code&gt; in this example.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb33&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb33-1&#34;&gt;&lt;a href=&#34;#cb33-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;tlsConfig&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;GetCertificate &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; m&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;GetCertificate&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We specify the &lt;code&gt;GetCertificate&lt;/code&gt; field of
&lt;code&gt;tls.Config&lt;/code&gt; to use the &lt;code&gt;GetCertificate&lt;/code&gt; function
of the &lt;code&gt;autocert.Manager&lt;/code&gt;, because this implements the
&lt;code&gt;tls.Config.GetCertificate&lt;/code&gt; hook.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb34&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb34-1&#34;&gt;&lt;a href=&#34;#cb34-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;cf&#34;&gt;go&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-2&#34;&gt;&lt;a href=&#34;#cb34-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    h &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; m&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HTTPHandler&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-3&#34;&gt;&lt;a href=&#34;#cb34-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ListenAndServe&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;:http&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; h&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb34-4&#34;&gt;&lt;a href=&#34;#cb34-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Additionally, we will use the &lt;code&gt;Manager.HTTPHandler&lt;/code&gt; from
&lt;code&gt;autocert&lt;/code&gt; that implements a handler fallback for requests
received on port 80, and redirects it to the ‘https’ port.&lt;a
href=&#34;#fn15&#34; class=&#34;footnote-ref&#34; id=&#34;fnref15&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;15&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb35&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb35-1&#34;&gt;&lt;a href=&#34;#cb35-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;tlsConfig&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificates &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;tls&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Certificate&lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;cert&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because we removed the &lt;code&gt;Certificates&lt;/code&gt; field from
&lt;code&gt;tlsConfig&lt;/code&gt;, and we’re using the
&lt;code&gt;autocert.Manager.GetCertificate&lt;/code&gt; function when using Let’s
Encrypt to get our certificate when we are using Let’s Encrypt. We need
to specify our &lt;code&gt;tlsConfig.Certificates&lt;/code&gt; field, when we aren’t
using ‘Let’s Encrypt’ and using our self-signed certificate.&lt;/p&gt;
&lt;p&gt;You can run it locally, but the certificate won’t be valid. Since,
we’re running it locally and using &lt;code&gt;localhost&lt;/code&gt;, ‘Let’s
Encrypt’ isn’t able to create certificates for this because nobody
uniquely owns &lt;code&gt;localhost&lt;/code&gt;. So local development deployments
we continue using our self-signed certificates. &lt;span class=&#34;citation&#34;
data-cites=&#34;letsencrypt_localhost&#34;&gt;(See &lt;a
href=&#34;#ref-letsencrypt_localhost&#34; role=&#34;doc-biblioref&#34;&gt;Let’s Encrypt
2020&lt;/a&gt; for some alternate solutions)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;OK, now if you have a production environment and domain name that
points to your production environment, you’ll be able to run the server
as follows:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ go run server.go -prod=true -host=yourawesomedomain.org&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;secure_headers&#34;&gt;Secure headers&lt;/h2&gt;
&lt;p&gt;In order to make our server more secure it is advised that we add
some secure headers to our server.&lt;a href=&#34;#fn16&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref16&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;16&lt;/sup&gt;&lt;/a&gt; This is done in order
to mitigate several vulnerabilities. &lt;span class=&#34;citation&#34;
data-cites=&#34;owasp_headers&#34;&gt;(&lt;a href=&#34;#ref-owasp_headers&#34;
role=&#34;doc-biblioref&#34;&gt;OWASP 2020b&lt;/a&gt;)&lt;/span&gt;. To our
&lt;code&gt;Handler&lt;/code&gt; we will add these response headers:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb37&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb37-1&#34;&gt;&lt;a href=&#34;#cb37-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;Handler&lt;span class=&#34;op&#34;&gt;:&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;HandlerFunc&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-2&#34;&gt;&lt;a href=&#34;#cb37-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;ResponseWriter&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r &lt;span class=&#34;op&#34;&gt;*&lt;/span&gt;http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Request&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-3&#34;&gt;&lt;a href=&#34;#cb37-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Strict-Transport-Security&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;max-age=63072000; includeSubdomains&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-4&#34;&gt;&lt;a href=&#34;#cb37-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Content-Security-Policy&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;default-src &#39;self&#39;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-5&#34;&gt;&lt;a href=&#34;#cb37-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;X-XSS-Protection&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;1; mode=block&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-6&#34;&gt;&lt;a href=&#34;#cb37-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;X-Frame-Options&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;DENY&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-7&#34;&gt;&lt;a href=&#34;#cb37-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Referrer-Policy&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;strict-origin-when-cross-origin&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-8&#34;&gt;&lt;a href=&#34;#cb37-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;X-Content-Type-Options&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;nosniff&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-9&#34;&gt;&lt;a href=&#34;#cb37-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Content-Type&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;text/plain&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-10&#34;&gt;&lt;a href=&#34;#cb37-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-11&#34;&gt;&lt;a href=&#34;#cb37-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; http&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;MaxBytesReader&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;w&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Body&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;&lt;&lt;&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-12&#34;&gt;&lt;a href=&#34;#cb37-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Write&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;hello, world!&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-13&#34;&gt;&lt;a href=&#34;#cb37-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb37-14&#34;&gt;&lt;a href=&#34;#cb37-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s go over the code again, and check what headers we’ve added,
what they protect us from, and how we set it.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb38&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb38-1&#34;&gt;&lt;a href=&#34;#cb38-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Strict-Transport-Security&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;max-age=63072000; includeSubdomains&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we set the ‘HSTS’ header (HTTP Strict Transport Security). This
will tell that clients should automatically interact with the server
using HTTPS connections. By setting the response header we specify the
period of time the client should only access the server in a secure
fashion. It protects against ‘protocol downgrade attacks’, and ‘cookie
hijacking’. &lt;span class=&#34;citation&#34;
data-cites=&#34;wiki_hsts owasp_headers&#34;&gt;(&lt;a href=&#34;#ref-wiki_hsts&#34;
role=&#34;doc-biblioref&#34;&gt;Wikipedia 2020c&lt;/a&gt;; &lt;a href=&#34;#ref-owasp_headers&#34;
role=&#34;doc-biblioref&#34;&gt;OWASP 2020b&lt;/a&gt;)&lt;/span&gt; The value
&lt;code&gt;max-age&lt;/code&gt; specifies the time the client should remember that
the site should be accessed by HTTPS. The value
&lt;code&gt;includeSubdomains&lt;/code&gt; implies that this rule applies to all
subdomains as well.&lt;a href=&#34;#fn17&#34; class=&#34;footnote-ref&#34; id=&#34;fnref17&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;17&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb39&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb39-1&#34;&gt;&lt;a href=&#34;#cb39-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Content-Security-Policy&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;default-src &#39;self&#39;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The ‘CSP’ header (Content Security Policy), allows you to define a
policy of what resources the client is allowed to load/run. It specifies
the domains the client should consider valid sources. Let’s say you have
a web application that allows content to be loaded within the web
application that originates from somewhere else. With CSP you’re able to
white list origins for scripts, images, fonts, stylesheets, etc. It
protects against certain ‘cross-site scripting attacks’ (XSS), which
exploit the clients’ trust of the content received from the server.
Malicious scripts can be thus be executed by the client because it
trusts the source of the content, even when it not from where it seems
to be from. The policy &lt;code&gt;&#34;default-src &#39;self&#39;&#34;&lt;/code&gt; defines that
all content should come from the site’s own origin, this excludes
subdomains.&lt;a href=&#34;#fn18&#34; class=&#34;footnote-ref&#34; id=&#34;fnref18&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;18&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb40&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb40-1&#34;&gt;&lt;a href=&#34;#cb40-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;X-XSS-Protection&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;1; mode=block&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The ‘X-XSS-Protection’ header stops pages from loading when they
detect XSS (Cross-site scripting) attacks. This is where the attacker
causes a page to load some malicious JavaScript, and does so by sending
the malicious payload as part of the request. With this header specify
to load or block the page from loading. The setting
&lt;code&gt;&#34;1; mode=block;&#34;&lt;/code&gt; will completely block the page from
loading. This is a special feature header for the Chrome and Internet
Explorer browsers, and seems to be unnecessary for API’s and should be
covered by a restrictive Content Security Policy.&lt;a href=&#34;#fn19&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref19&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;19&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb41&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb41-1&#34;&gt;&lt;a href=&#34;#cb41-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;X-Frame-Options&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;DENY&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The ‘X-Frame-Options’ header let’s the browser know whether it is
allowed to render a page in a &lt;code&gt;&lt;embed&gt;&lt;/code&gt;,
&lt;code&gt;&lt;frame&gt;&lt;/code&gt;, &lt;code&gt;&lt;iframe&gt;&lt;/code&gt; or
&lt;code&gt;&lt;object&gt;&lt;/code&gt; tag. This is used to avoid ‘click-jacking’
such that the content isn’t embedded into other sites. Here we set it to
&lt;code&gt;&#34;DENY&#34;&lt;/code&gt;, which blocks the content from being embedded in
other pages.&lt;a href=&#34;#fn20&#34; class=&#34;footnote-ref&#34; id=&#34;fnref20&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;20&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb42&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb42-1&#34;&gt;&lt;a href=&#34;#cb42-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Referrer-Policy&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;strict-origin-when-cross-origin&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The ‘Referrer-Policy’ header lets us define what information is being
sent to external sites/resources in the &lt;code&gt;Referer&lt;/code&gt; request
header. When a client accesses an url from hyperlink, or a webpage loads
an external resource, the browser adds the header &lt;code&gt;Referer&lt;/code&gt;
(yes that is intentionally misspelled), to let the destination know the
origin of that request. Imagine that your webpage has a link to an
external site, and that external site receives the &lt;code&gt;Referer&lt;/code&gt;
header with information that should only be used internally. With this
header we can control what is being sent to the destination. In this
example we will set it to &lt;code&gt;strict-origin-when-cross-origin&lt;/code&gt;,
which sends the full referer to the sources on the same origin, and url
without path to external origin destinations, and send no header to
less-secure destinations (HTTPS→HTTP).&lt;a href=&#34;#fn21&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref21&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;21&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb43&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb43-1&#34;&gt;&lt;a href=&#34;#cb43-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;X-Content-Type-Options&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;nosniff&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The ‘X-Content-Type-Options’ header indicates the MIME types set by
the &lt;code&gt;&#34;Content-Type&#34;&lt;/code&gt; header, which should not be changed and
be followed. It prevents browsers from interpreting files as something
else than what was specified the &lt;code&gt;&#34;Content-Type&#34;&lt;/code&gt; header. It
opts out of ‘Mime type sniffing’: guessing the correct Mime type by
looking at the bytes of the resource done by browser. Without this
header, browsers can incorrectly detect files as scripts and
stylesheets, leading to XSS attacks. Setting the header to
&lt;code&gt;&#34;nosniff&#34;&lt;/code&gt; indicates that browsers should prevent
incorrectly detecting non-scripts as scripts.&lt;a href=&#34;#fn22&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref22&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;22&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb44&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb44-1&#34;&gt;&lt;a href=&#34;#cb44-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;w&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Header&lt;span class=&#34;op&#34;&gt;().&lt;/span&gt;Add&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;Content-Type&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;st&#34;&gt;&#34;text/plain; charset=UTF-8&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to make ‘X-Content-Type-Options’ to work correctly, we need
to set the ‘Content-Type’ to the correct MIME type. For this example
we’ll set it to &lt;code&gt;&#34;text/plain; charset=UTF-8&#34;&lt;/code&gt;. This tells the
client that the type is &lt;code&gt;text&lt;/code&gt;, the subtype is
&lt;code&gt;plain&lt;/code&gt;, and that the character encoding is utf-8.&lt;a
href=&#34;#fn23&#34; class=&#34;footnote-ref&#34; id=&#34;fnref23&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;23&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this section we’ve set up some response headers that will mitigate
some common vulnerabilities.&lt;a href=&#34;#fn24&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref24&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;24&lt;/sup&gt;&lt;/a&gt; For brevity, I’ve
added the response header to the &lt;code&gt;Handler&lt;/code&gt; for a single
response. In an actual implementation you would likely add this to a
sort of middleware for your handlers.&lt;a href=&#34;#fn25&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref25&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;25&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Again, we can test out our HTTP webserver:&lt;/p&gt;
&lt;pre class=&#34;text&#34;&gt;&lt;code&gt;$ go run server.go -cert=cert.pem -key=key.pem
$ curl --verbose --insecure https://localhost

*   Trying ::1:443...
* Connected to localhost (::1) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: O=mkcert development certificate; OU=jp@aztlan
*  start date: Jun  1 00:00:00 2019 GMT
*  expire date: Apr 15 18:58:07 2030 GMT
*  subjectAltName: host &#34;localhost&#34; matched cert&#39;s &#34;localhost&#34;
*  issuer: O=mkcert development CA; OU=jp@aztlan; CN=mkcert jp@aztlan
*  SSL certificate verify ok.
&gt; GET / HTTP/1.1
&gt; Host: localhost
&gt; User-Agent: curl/7.52.1
&gt; Accept: */*
&gt;
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* Mark bundle as not supporting multiuse
&lt; HTTP/1.1 200 OK
&lt; Content-Security-Policy: default-src &#39;self&#39;
&lt; Content-Type: text/plain
&lt; Referrer-Policy: strict-origin-when-cross-origin
&lt; Strict-Transport-Security: max-age=63072000; includeSubdomains
&lt; X-Content-Type-Options: nosniff
&lt; X-Frame-Options: DENY
&lt; X-Xss-Protection: 1; mode=block
&lt; Date: Sun, 24 May 2020 14:21:49 GMT
&lt; Content-Length: 13
&lt;
hello, world
* Connection #0 to host localhost left intact&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And, now you can see that we’ve implemented the secure headers.&lt;/p&gt;
&lt;h2 id=&#34;cors_csrf&#34;&gt;CORS / CSRF&lt;/h2&gt;
&lt;p&gt;In this post we’ve added timeouts, a cryptographic protocol, and
secure headers to our HTTP webserver. And depending on the size,
utility, and what the server is going to be used for, you likely want to
add additional security measures such as CORS, and CSRF. Let’s check out
what they are, and investigate at what point we want to implement those
measures.&lt;/p&gt;
&lt;h3 id=&#34;cors&#34;&gt;CORS&lt;/h3&gt;
&lt;p&gt;CORS&lt;a href=&#34;#fn26&#34; class=&#34;footnote-ref&#34; id=&#34;fnref26&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;26&lt;/sup&gt;&lt;/a&gt; (Cross-Origin Resource Sharing)
specifies which cross-domain requests for a server are allowed from a
webbrowser. Meaning: are request to, for instance, an api allowed from
somewhere else than from the domain of the original api. One would
likely disable CORS header, when cross-domain calls are not supported or
expected. &lt;span class=&#34;citation&#34; data-cites=&#34;owasp_rest_security&#34;&gt;(&lt;a
href=&#34;#ref-owasp_rest_security&#34; role=&#34;doc-biblioref&#34;&gt;OWASP
2020c&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;CORS are disabled by default by your browser, meaning you can’t
access resources from different origins. As a server you specify to the
client which domains (origins), are allowed to make requests. It allows
the client’s browser to determine if and when cross-domain requests
should be allowed. Suppose a user visits &lt;code&gt;example.com&lt;/code&gt; and
the page attempts a cross-origin request to fetch the user’s data from
&lt;code&gt;service.example.com&lt;/code&gt;. The server
(&lt;code&gt;service.example.com&lt;/code&gt;) can then reply that requests from all
domains are allowed. So requests from the origin
&lt;code&gt;example.com&lt;/code&gt; (and any other for that matter), are allowed to
be made against the server ‘service.example.com’. &lt;span class=&#34;citation&#34;
data-cites=&#34;wiki_cors&#34;&gt;(&lt;a href=&#34;#ref-wiki_cors&#34;
role=&#34;doc-biblioref&#34;&gt;Wikipedia 2020a&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;You can set CORS with the &lt;code&gt;Access-Control&lt;/code&gt; headers &lt;span
class=&#34;citation&#34; data-cites=&#34;mozilla_cors&#34;&gt;(&lt;a href=&#34;#ref-mozilla_cors&#34;
role=&#34;doc-biblioref&#34;&gt;Mozilla 2020b&lt;/a&gt;)&lt;/span&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; - specifies either a single
origin, which tells browsers to allow that origin to access the
resource; or else — for requests without credentials — the
&lt;code&gt;*&lt;/code&gt; wildcard, to tell browsers to allow any origin to access
the resource.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Expose-Headers&lt;/code&gt; - lets a server whitelist
headers that browsers are allowed to access.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Max-Age&lt;/code&gt; - indicates how long the results
of a preflight request can be cached.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Credentials&lt;/code&gt; - indicates whether
the response to the request can be exposed when the credentials flag is
true.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Methods&lt;/code&gt; - specifies the method or
methods allowed when accessing the resource.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Headers&lt;/code&gt; - is used in response to a
preflight request to indicate which HTTP headers can be used when making
the actual request.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;csrf&#34;&gt;CSRF&lt;/h3&gt;
&lt;p&gt;CSRF&lt;a href=&#34;#fn27&#34; class=&#34;footnote-ref&#34; id=&#34;fnref27&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;27&lt;/sup&gt;&lt;/a&gt; (Cross Site Request Forgery) is to
execute an undesired action on the server when the user is
authenticated. So executing requests on your behalf from somewhere else.
Unlike cross-site scripting (XSS), which exploits the trust a user has
for a particular site, CSRF exploits the trust that a site has in a
user’s browser. &lt;span class=&#34;citation&#34; data-cites=&#34;wiki_csrf&#34;&gt;(&lt;a
href=&#34;#ref-wiki_csrf&#34; role=&#34;doc-biblioref&#34;&gt;Wikipedia
2020b&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Imagine being authenticated at &lt;code&gt;myexamplebank.com&lt;/code&gt;, and an
attacker is able to get your browser to send a request to
&lt;code&gt;myexamplebank.com&lt;/code&gt;. By either letting you, for instance,
click a link or visit a page that has instruction to send a request to
&lt;code&gt;myexamplebank.com&lt;/code&gt;. Now, the request is sent by the attacker
with the instructions to transfer money from your account to his, by
including cookies that are associated with
&lt;code&gt;myexamplebank.com&lt;/code&gt;. And when the cookies contain
authentication data, the attacker is able to execute the transfer.&lt;/p&gt;
&lt;p&gt;In order to mitigate this we need to make sure that HTTP methods such
as &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;HEAD&lt;/code&gt;, &lt;code&gt;OPTIONS&lt;/code&gt; are not
allowed to change any server side state. And that the HTTP methods
&lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;PATCH&lt;/code&gt;,
&lt;code&gt;DELETE&lt;/code&gt; use a token based mitigation protocol. This method
is the most popular, and recommended method for protecting your web
application from CSRF attacks. This method works by requiring that
requests that are able to change server side state to include a token.
The token is generated server side once per user session, or for each
request. It should be unique per user session, secret, and
unpredictable. This will prevent CSRF attacks because an attack will not
be able to create a valid request without this token. &lt;span
class=&#34;citation&#34; data-cites=&#34;owasp_csrf&#34;&gt;(&lt;a href=&#34;#ref-owasp_csrf&#34;
role=&#34;doc-biblioref&#34;&gt;OWASP 2020a&lt;/a&gt;)&lt;/span&gt; We can implement these
token in Go by using the package &lt;a
href=&#34;https://github.com/gorilla/csrf&#34;&gt;&lt;code&gt;gorilla/mux&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In this posts we’ve explored how we can make our Go based HTTP
webserver more secure by implementing timeouts, TLS, and secure headers.
This gives us a good starting-point, and as your application grows, and
depending on its application and utility, additional security measures
should be implemented. To reiterate what was mentioned in the
introduction, we do well to check, and continue checking for
best-practices, and there are some very good checklists&lt;a href=&#34;#fn28&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref28&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;28&lt;/sup&gt;&lt;/a&gt;
available that can help you make your HTTP webservers and web
applications more secure.&lt;/p&gt;
&lt;h2 class=&#34;unnumbered&#34; id=&#34;references&#34;&gt;References&lt;/h2&gt;
&lt;div id=&#34;refs&#34; class=&#34;references csl-bib-body hanging-indent&#34;
data-entry-spacing=&#34;0&#34; role=&#34;list&#34;&gt;
&lt;div id=&#34;ref-cloudflare_tls&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Cloudflare. 2020. &lt;span&gt;“What Is Transport Layer Security.”&lt;/span&gt; 2020.
&lt;a
href=&#34;https://www.cloudflare.com/learning/ssl/transport-layer-security-tls/&#34;&gt;https://www.cloudflare.com/learning/ssl/transport-layer-security-tls/&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-grigorik2013&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Grigorik, Ilya. 2013. &lt;em&gt;High-Performance Browser Networking&lt;/em&gt;.
O’Reilly Media. &lt;a href=&#34;https://hpbn.co/&#34;&gt;https://hpbn.co/&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-letsencrypt_localhost&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Let’s Encrypt. 2020. &lt;span&gt;“Certificates for Localhost.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://letsencrypt.org/docs/certificates-for-localhost/&#34;&gt;https://letsencrypt.org/docs/certificates-for-localhost/&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-mozilla_tls&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Mozilla. 2020a. &lt;span&gt;“Security/Server Side TLS.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://wiki.mozilla.org/Security/Server_Side_TLS&#34;&gt;https://wiki.mozilla.org/Security/Server_Side_TLS&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-mozilla_cors&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020b. &lt;span&gt;“Web Technology for Developers - CORS.”&lt;/span&gt; 2020.
&lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS&#34;&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-owasp_csrf&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
OWASP. 2020a. &lt;span&gt;“Cross-Site Request Forgery (CSRF) Prevention Cheat
Sheet.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html&#34;&gt;https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-owasp_headers&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020b. &lt;span&gt;“OWASP Secure Headers Project.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://www.owasp.org/index.php/OWASP_Secure_Headers_Project#tab=Headers&#34;&gt;https://www.owasp.org/index.php/OWASP_Secure_Headers_Project#tab=Headers&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-owasp_rest_security&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020c. &lt;span&gt;“REST Security Cheat Sheet.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/REST_Security_Cheat_Sheet.md&#34;&gt;https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/REST_Security_Cheat_Sheet.md&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-valsorda2016a&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Valsorda, Filippo. 2016. &lt;span&gt;“So You Want to Expose Go on the
Internet.”&lt;/span&gt; 2016. &lt;a
href=&#34;https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/&#34;&gt;https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_cors&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Wikipedia. 2020a. &lt;span&gt;“Cross-Origin Resource Sharing.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/Cross-origin_resource_sharing&#34;&gt;https://en.wikipedia.org/wiki/Cross-origin_resource_sharing&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_csrf&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020b. &lt;span&gt;“Cross-Site Request Forgery.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/Cross-site_request_forgery&#34;&gt;https://en.wikipedia.org/wiki/Cross-site_request_forgery&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_hsts&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020c. &lt;span&gt;“HTTP Strict Transport Security.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security&#34;&gt;https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_asymm&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020d. &lt;span&gt;“Public-Key Cryptography.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/Public-key_cryptography&#34;&gt;https://en.wikipedia.org/wiki/Public-key_cryptography&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_symm&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020e. &lt;span&gt;“Symmetric-Key Algorithm.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/Symmetric-key_algorithm&#34;&gt;https://en.wikipedia.org/wiki/Symmetric-key_algorithm&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_tls&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020f. &lt;span&gt;“Transport Layer Security.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/Transport_Layer_Security&#34;&gt;https://en.wikipedia.org/wiki/Transport_Layer_Security&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_x509&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2020g. &lt;span&gt;“X.509.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/X.509&#34;&gt;https://en.wikipedia.org/wiki/X.509&lt;/a&gt;.
&lt;/div&gt;
&lt;/div&gt;
&lt;section id=&#34;footnotes&#34; class=&#34;footnotes footnotes-end-of-document&#34;
role=&#34;doc-endnotes&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://changelog.com/gotime/101&#34;&gt;Go Time -
101&lt;/a&gt;&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/doc/articles/wiki/&#34;&gt;Go
Documentation: Writing Web Applications&lt;/a&gt;&lt;a href=&#34;#fnref2&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;Additional information on timeouts: &lt;a
href=&#34;https://ieftimov.com/post/make-resilient-golang-net-http-servers-using-timeouts-deadlines-context-cancellation/&#34;&gt;Ilija
Eftimov - Make resilient Go net/http servers using timeouts, deadlines
and context cancellation&lt;/a&gt;, &lt;a
href=&#34;https://blog.simon-frey.eu/go-as-in-golang-standard-net-http-config-will-break-your-production/&#34;&gt;Simon
Frey - Standard net/http config will break your production
environment&lt;/a&gt;&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn4&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://godoc.org/net/http#MaxBytesReader&#34;&gt;Go
Documentation: net/http&lt;/a&gt;&lt;a href=&#34;#fnref4&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn5&#34;&gt;&lt;p&gt;More information on other application protocols: &lt;a
href=&#34;https://en.wikipedia.org/wiki/Application_layer&#34;&gt;Wikipedia -
Application Layer&lt;/a&gt;&lt;a href=&#34;#fnref5&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn6&#34;&gt;&lt;p&gt;Some more information on session keys used in TLS: &lt;a
href=&#34;https://www.cloudflare.com/learning/ssl/what-is-a-session-key/&#34;&gt;Cloudflare
- What Is a Session Key?&lt;/a&gt;&lt;a href=&#34;#fnref6&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn7&#34;&gt;&lt;p&gt;In depth look at the TLS handshake: &lt;a
href=&#34;https://www.cloudflare.com/learning/ssl/what-happens-in-a-tls-handshake/&#34;&gt;Cloudflare
- What Happens in a TLS Handshake?&lt;/a&gt;&lt;a href=&#34;#fnref7&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn8&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/pkg/crypto/tls/&#34;&gt;Go
Documentation: TLS&lt;/a&gt;&lt;a href=&#34;#fnref8&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn9&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail&#34;&gt;Wikipedia -
Privacy-Enhanced Mail&lt;/a&gt;&lt;a href=&#34;#fnref9&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn10&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/pkg/crypto/tls/#Config&#34;&gt;Go
Documentation: TLS - Config&lt;/a&gt;&lt;a href=&#34;#fnref10&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn11&#34;&gt;&lt;p&gt;More information on ECDSA: &lt;a
href=&#34;https://blog.cloudflare.com/ecdsa-the-digital-signature-algorithm-of-a-better-internet/&#34;&gt;Cloudflare
- ECDSA: The digital signature algorithm of a better internet&lt;/a&gt;&lt;a
href=&#34;#fnref11&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn12&#34;&gt;&lt;p&gt;You might need to update &lt;code&gt;curl&lt;/code&gt; and
&lt;code&gt;openssl&lt;/code&gt; to be able to use TLS version 1.3. Here are some
links that can help you install/upgrade them on a linux system. cURL &lt;a
href=&#34;https://linuxhint.com/install-curl-linux/&#34;&gt;1&lt;/a&gt;, openssl &lt;a
href=&#34;https://www.howtoforge.com/tutorial/how-to-install-openssl-from-source-on-linux/&#34;&gt;1&lt;/a&gt;,
&lt;a
href=&#34;https://www.tecmint.com/install-openssl-from-source-in-centos-ubuntu/&#34;&gt;2&lt;/a&gt;.
Note for cURL it is important to &lt;code&gt;./configure --with-ssl&lt;/code&gt;.&lt;a
href=&#34;#fnref12&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn13&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://godoc.org/golang.org/x/crypto/acme/autocert&#34;&gt;Go
Documentation: autocert&lt;/a&gt;&lt;a href=&#34;#fnref13&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn14&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://letsencrypt.org/docs/rate-limits/&#34;&gt;Let’s Encrypt - Rate
Limits&lt;/a&gt;&lt;a href=&#34;#fnref14&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn15&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://godoc.org/golang.org/x/crypto/acme/autocert#Manager.HTTPHandler&#34;&gt;Go
Documentation: autocert - Manager.HTTPHandler&lt;/a&gt;&lt;a href=&#34;#fnref15&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn16&#34;&gt;&lt;p&gt;Some information about secure headers: &lt;a
href=&#34;https://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project&#34;&gt;OWASP
- Secure headers project&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers&lt;/a&gt;&lt;a
href=&#34;#fnref16&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn17&#34;&gt;&lt;p&gt;More information on HSTS: &lt;a
href=&#34;https://infosec.mozilla.org/guidelines/web_security.html#http-strict-transport-security&#34;&gt;Mozilla
- Web Security - HSTS&lt;/a&gt;, &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security&#34;&gt;Mozilla
- Web technology for developers - HSTS&lt;/a&gt; &lt;a
href=&#34;https://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project#hsts&#34;&gt;OWASP
- Secure Headers - HSTS&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html#hsts&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers - HSTS&lt;/a&gt;&lt;a
href=&#34;#fnref17&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn18&#34;&gt;&lt;p&gt;More information on CSP: &lt;a
href=&#34;https://infosec.mozilla.org/guidelines/web_security.html#content-security-policy&#34;&gt;Mozilla
- Web Security - HSTS&lt;/a&gt;, &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy&#34;&gt;Mozilla
- Web technology for developers - CSP 1&lt;/a&gt;, &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP&#34;&gt;Mozilla -
Web technology for developers - CSP 2&lt;/a&gt;, &lt;a
href=&#34;https://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project#csp&#34;&gt;OWASP
- Secure Headers - CSP&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html#csp&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers - CSP&lt;/a&gt;&lt;a
href=&#34;#fnref18&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn19&#34;&gt;&lt;p&gt;More information on XSS: &lt;a
href=&#34;https://infosec.mozilla.org/guidelines/web_security.html#x-xss-protection&#34;&gt;Mozilla
- Web Security - XSS&lt;/a&gt;, &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection&#34;&gt;Mozilla
- Web technology for developers - XSS&lt;/a&gt;, &lt;a
href=&#34;ihttps://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project#xxxsp&#34;&gt;OWASP
- Secure Headers - XSS&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html#x-xss-protection&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers - XSS&lt;/a&gt;&lt;a
href=&#34;#fnref19&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn20&#34;&gt;&lt;p&gt;More information on X-Frame: &lt;a
href=&#34;https://infosec.mozilla.org/guidelines/web_security.html#x-frame-options&#34;&gt;Mozilla
- Web Security - XFO&lt;/a&gt;, &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options&#34;&gt;Mozilla
- Web technology for developers - XFO&lt;/a&gt;, &lt;a
href=&#34;https://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project#xfo&#34;&gt;OWASP
- Secure Headers - XFO&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html#x-frame-options&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers - XFO&lt;/a&gt;&lt;a
href=&#34;#fnref20&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn21&#34;&gt;&lt;p&gt;More information on Referrer Policy: &lt;a
href=&#34;https://infosec.mozilla.org/guidelines/web_security.html#referrer-policy&#34;&gt;Mozilla
- Web Security- Referrer Policy&lt;/a&gt;, &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy&#34;&gt;Mozilla
- Web technology for developers- Referrer Policy&lt;/a&gt;, &lt;a
href=&#34;https://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project#rp&#34;&gt;OWASP
- Secure Headers - Referrer Policy&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html#referrer-policy&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers - Referrer
Policy&lt;/a&gt;&lt;a href=&#34;#fnref21&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn22&#34;&gt;&lt;p&gt;More information on X-Content-Type-Options: &lt;a
href=&#34;https://infosec.mozilla.org/guidelines/web_security.html#x-content-type-options&#34;&gt;Mozilla
- Web Security - XTCO&lt;/a&gt;, &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options&#34;&gt;Mozilla
- Web technology for developers - XTCO&lt;/a&gt;, &lt;a
href=&#34;https://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project#xcto&#34;&gt;OWASP
- Secure Headers - XTCO&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html#x-content-type-options&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers - XTCO&lt;/a&gt;&lt;a
href=&#34;#fnref22&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn23&#34;&gt;&lt;p&gt;More information on Content-Type: &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type&#34;&gt;Mozilla
- Web Technology - Content-Type&lt;/a&gt;&lt;a href=&#34;#fnref23&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn24&#34;&gt;&lt;p&gt;Some information about secure headers: &lt;a
href=&#34;https://wiki.owasp.org/index.php/OWASP_Secure_Headers_Project&#34;&gt;OWASP
- Secure headers project&lt;/a&gt;, &lt;a
href=&#34;https://blog.appcanary.com/2017/http-security-headers.html&#34;&gt;Appcanary
- Everything you need to know about HTTP security headers&lt;/a&gt;&lt;a
href=&#34;#fnref24&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn25&#34;&gt;&lt;p&gt;Some examples of response headers middleware
implementations: &lt;a href=&#34;https://github.com/unrolled/secure&#34;&gt;Github -
unrolled/secure&lt;/a&gt;, &lt;a href=&#34;https://youtu.be/rWBSMsLG8po?t=1314&#34;&gt;Mat
Reyer - How I Write HTTP Web Services after Eight Years&lt;/a&gt;,&lt;a
href=&#34;#fnref25&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn26&#34;&gt;&lt;p&gt;More information on CORS: &lt;a
href=&#34;https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS&#34;&gt;Mozilla -
Web technology for developers - Cross-Origin Resource Sharing
(CORS)&lt;/a&gt;, &lt;a
href=&#34;https://en.wikipedia.org/wiki/Cross-origin_resource_sharing&#34;&gt;Wikipedia
- Cross-origin resource sharing&lt;/a&gt;&lt;a href=&#34;#fnref26&#34;
class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn27&#34;&gt;&lt;p&gt;More information on CSRF: &lt;a
href=&#34;https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html&#34;&gt;OWASP
- Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet&lt;/a&gt;, &lt;a
href=&#34;https://security.stackexchange.com/a/166798&#34;&gt;StackExchange -
Should I use CSRF protection on Rest API endpoints?&lt;/a&gt;&lt;a
href=&#34;#fnref27&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn28&#34;&gt;&lt;p&gt;Some useful security checklists/guidelines: &lt;a
href=&#34;https://infosec.mozilla.org/guidelines/web_security.html&#34;&gt;Mozilla
- Web security&lt;/a&gt;, &lt;a
href=&#34;https://github.com/shieldfy/API-Security-Checklist&#34;&gt;Shieldfy - API
Security Checklist&lt;/a&gt;, &lt;a
href=&#34;https://github.com/0xRadi/OWASP-Web-Checklist&#34;&gt;OWASP - Web
Checklist&lt;/a&gt;, &lt;a
href=&#34;https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html&#34;&gt;OWASP
- REST Security Cheatsheet&lt;/a&gt;, &lt;a
href=&#34;https://cheatsheetseries.owasp.org/&#34;&gt;OWASP - Cheat Sheet Series
Project&lt;/a&gt;&lt;a href=&#34;#fnref28&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
    <item>
      <title>Encrypt data with a password in Go</title>
      <link>https://bruinsslot.jp/articles/golang-crypto/</link>
      <pubDate>Tue, 25 Feb 2020 08:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/golang-crypto/</guid>
      <description>&lt;h2 id=&#34;intro&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;When we’re encrypting data, typically we will create a random key
that is able to decrypt that data. In some specific cases one wants to
use a user specified key to decrypt that data like a password. However,
the key that is used for cryptographic algorithms typically needs to be
at least 32 bytes. But, it is likely that our password won’t make that
criteria, so we need to have a solution for that. Recently, I needed
such a method, and in this post I’ll lay out what I’ve done in order to
solve it. But before we get into the nitty-gritty.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;DISCLAIMER&lt;/strong&gt;: I’m not an expert at encryption, I’ve
mentioned the sources that I’ve used to come to the solutions provided
in this post. I implore you read/watch those sources to better
understand it. And, as such if there are any errors in the post/code
please let me know or leave a comment so I can update it, so that there
is no perpetuation of wrong methods/techniques.&lt;/p&gt;
&lt;p&gt;OK, since we’ve got that out of the way, let’s begin!&lt;/p&gt;
&lt;h2 id=&#34;encrypt&#34;&gt;Encrypt&lt;/h2&gt;
&lt;p&gt;Let’s first start with encrypting our data. We’ll start with creating
the &lt;code&gt;Encrypt&lt;/code&gt; function that will accept a &lt;code&gt;key&lt;/code&gt;
and a &lt;code&gt;data&lt;/code&gt; argument. Based on that we will encrypt the data
that can be decrypted using the &lt;code&gt;key&lt;/code&gt;. First, we will
generate that key by using 32 random bytes, later on we’ll replace that
with our password. Below, shows the code that is able to encrypt our
data, provided by a generated key.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb1&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb1-1&#34;&gt;&lt;a href=&#34;#cb1-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-2&#34;&gt;&lt;a href=&#34;#cb1-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/aes&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-3&#34;&gt;&lt;a href=&#34;#cb1-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/cipher&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-4&#34;&gt;&lt;a href=&#34;#cb1-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/rand&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-5&#34;&gt;&lt;a href=&#34;#cb1-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-6&#34;&gt;&lt;a href=&#34;#cb1-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-7&#34;&gt;&lt;a href=&#34;#cb1-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Encrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-8&#34;&gt;&lt;a href=&#34;#cb1-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    blockCipher&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; aes&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewCipher&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-9&#34;&gt;&lt;a href=&#34;#cb1-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-10&#34;&gt;&lt;a href=&#34;#cb1-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb1-11&#34;&gt;&lt;a href=&#34;#cb1-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-12&#34;&gt;&lt;a href=&#34;#cb1-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-13&#34;&gt;&lt;a href=&#34;#cb1-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    gcm&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; cipher&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewGCM&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;blockCipher&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-14&#34;&gt;&lt;a href=&#34;#cb1-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-15&#34;&gt;&lt;a href=&#34;#cb1-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb1-16&#34;&gt;&lt;a href=&#34;#cb1-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-17&#34;&gt;&lt;a href=&#34;#cb1-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-18&#34;&gt;&lt;a href=&#34;#cb1-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    nonce &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-19&#34;&gt;&lt;a href=&#34;#cb1-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-20&#34;&gt;&lt;a href=&#34;#cb1-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb1-21&#34;&gt;&lt;a href=&#34;#cb1-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-22&#34;&gt;&lt;a href=&#34;#cb1-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-23&#34;&gt;&lt;a href=&#34;#cb1-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    ciphertext &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Seal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-24&#34;&gt;&lt;a href=&#34;#cb1-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-25&#34;&gt;&lt;a href=&#34;#cb1-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb1-26&#34;&gt;&lt;a href=&#34;#cb1-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, let’s go over the code, and inspect what we’re doing.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb2&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb2-1&#34;&gt;&lt;a href=&#34;#cb2-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Encrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First, we start by creating our &lt;code&gt;Encrypt&lt;/code&gt; function, and it
will accept a &lt;code&gt;key&lt;/code&gt; and a &lt;code&gt;data&lt;/code&gt; argument. We’ll
be using a &lt;code&gt;byte&lt;/code&gt; slice instead of an &lt;code&gt;io.Reader&lt;/code&gt;
as the &lt;code&gt;data&lt;/code&gt; argument. While using &lt;code&gt;io.Reader&lt;/code&gt;
would allow us to use the &lt;code&gt;Encrypt&lt;/code&gt; function with every other
type that implements the &lt;code&gt;io.Reader&lt;/code&gt; interface. &lt;span
class=&#34;citation&#34; data-cites=&#34;ryer2015&#34;&gt;(&lt;a href=&#34;#ref-ryer2015&#34;
role=&#34;doc-biblioref&#34;&gt;Ryer 2015&lt;/a&gt;)&lt;/span&gt; It is however because of the
nature of &lt;code&gt;io.Reader&lt;/code&gt;, being a stream of data, that when we
want to decrypt the &lt;code&gt;ciphertext&lt;/code&gt;, we need to see it in its
entirety. A solution would be to break the stream into discrete chunks,
however this would add significant complexity to the problem. &lt;a
href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;span class=&#34;citation&#34;
data-cites=&#34;isom2015&#34;&gt;(&lt;a href=&#34;#ref-isom2015&#34; role=&#34;doc-biblioref&#34;&gt;Isom
2015&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb3&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb3-1&#34;&gt;&lt;a href=&#34;#cb3-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;blockCipher&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; aes&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewCipher&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We’re initializing the block cipher based on the &lt;code&gt;key&lt;/code&gt;
that we provided. Here we’re using the &lt;code&gt;crypto/aes&lt;/code&gt;&lt;a
href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; package that implements the AES&lt;a
href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;a href=&#34;#fn4&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref4&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt; (Advanced Encryption
Standard) encryption algorithm. AES is a symmetric-key encryption
algorithm, that will be secure enough for modern use cases.
Additionally, AES uses hardware acceleration on most platforms, so it’ll
be pretty fast to use. &lt;span class=&#34;citation&#34;
data-cites=&#34;tankersley2016&#34;&gt;(&lt;a href=&#34;#ref-tankersley2016&#34;
role=&#34;doc-biblioref&#34;&gt;Tankersley 2016&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb4&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb4-1&#34;&gt;&lt;a href=&#34;#cb4-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;gcm&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; cipher&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewGCM&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;blockCipher&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we’re wrapping the block cipher, with a specific &lt;em&gt;mode&lt;/em&gt;.
We do this because we shouldn’t use a &lt;code&gt;cipher.Block&lt;/code&gt;
interface directly. This is because the block cipher only encrypts 16
bytes of data, nothing more. So if you would call
&lt;code&gt;blockCiper.Encrypt()&lt;/code&gt; it would only encrypt the first 16
bytes. Thus we need something on top of that, and wrap the block cipher,
and those are called &lt;em&gt;modes&lt;/em&gt;. Again we have several
&lt;em&gt;modes&lt;/em&gt; to choose from, and here we’re going to use the
&lt;em&gt;Galois Counter Mode&lt;/em&gt; (GCM)&lt;a href=&#34;#fn5&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref5&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt;, with a standard nonce
length.&lt;/p&gt;
&lt;p&gt;Only GCM provides authenticated encryption, and it implements the
&lt;code&gt;cipher.AEAD&lt;/code&gt; interface (Authenticated Encryption with
Associated Data)&lt;a href=&#34;#fn6&#34; class=&#34;footnote-ref&#34; id=&#34;fnref6&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt;. Authenticated encryption means that
not only is your data going to be confidential, secret, and encrypted,
it’s also now going to be tamper proof. If someone alters the
&lt;code&gt;ciphertext&lt;/code&gt; you will not then be able to validly decrypt it.
When you’re using authenticated encryption and someone messes with your
data it just fails to decrypt. &lt;span class=&#34;citation&#34;
data-cites=&#34;tankersley2016 isom2015&#34;&gt;(&lt;a href=&#34;#ref-tankersley2016&#34;
role=&#34;doc-biblioref&#34;&gt;Tankersley 2016&lt;/a&gt;; &lt;a href=&#34;#ref-isom2015&#34;
role=&#34;doc-biblioref&#34;&gt;Isom 2015&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb5&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb5-1&#34;&gt;&lt;a href=&#34;#cb5-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;nonce &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb5-2&#34;&gt;&lt;a href=&#34;#cb5-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb5-3&#34;&gt;&lt;a href=&#34;#cb5-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb5-4&#34;&gt;&lt;a href=&#34;#cb5-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Before we can encrypt our bytes we need to generate a randomized
&lt;code&gt;nonce&lt;/code&gt;, and its length is specified by the GCM. The
&lt;code&gt;nonce&lt;/code&gt; stands for: &lt;em&gt;number once used&lt;/em&gt;, and it’s a
piece of data that should not be repeated and only used once in
combination with any particular key. Meaning: don’t repeat the
combination of a &lt;code&gt;key&lt;/code&gt; and a &lt;code&gt;nonce&lt;/code&gt; more than
once. But, how do you keep track of that? If we use sufficiently large
numbers for a &lt;code&gt;nonce&lt;/code&gt; we should probably be fine for this
use-case. &lt;span class=&#34;citation&#34; data-cites=&#34;isom2015 viega2003&#34;&gt;(&lt;a
href=&#34;#ref-isom2015&#34; role=&#34;doc-biblioref&#34;&gt;Isom 2015&lt;/a&gt;; &lt;a
href=&#34;#ref-viega2003&#34; role=&#34;doc-biblioref&#34;&gt;Viega and Messier 2003,
134–35&lt;/a&gt;)&lt;/span&gt; We do that by using Go’s &lt;code&gt;crypto/rand&lt;/code&gt;
package to read randomized bytes into the &lt;code&gt;nonce&lt;/code&gt; byte
slice.&lt;a href=&#34;#fn7&#34; class=&#34;footnote-ref&#34; id=&#34;fnref7&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb6&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb6-1&#34;&gt;&lt;a href=&#34;#cb6-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;encryptedData &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Seal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;nonce&lt;/code&gt; that we’re going to use for encrypting our
data, is also needed to decrypt it. So we need to be able to refer to it
while decrypting, and one of the strategies is to add it to the
encrypted data. In this example we will prepend the &lt;code&gt;nonce&lt;/code&gt;
to the encrypted data. We do that by passing in the &lt;code&gt;nonce&lt;/code&gt;
as the first argument &lt;code&gt;dst&lt;/code&gt; of the &lt;code&gt;Seal&lt;/code&gt;
function, and as such the encrypted data will be appended to it.&lt;a
href=&#34;#fn8&#34; class=&#34;footnote-ref&#34; id=&#34;fnref8&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;8&lt;/sup&gt;&lt;/a&gt; We can do this because the
&lt;code&gt;nonce&lt;/code&gt; doesn’t have to be secret, it just has to be unique.
&lt;span class=&#34;citation&#34; data-cites=&#34;tankersley2016&#34;&gt;(&lt;a
href=&#34;#ref-tankersley2016&#34; role=&#34;doc-biblioref&#34;&gt;Tankersley
2016&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&#34;decrypt&#34;&gt;Decrypt&lt;/h2&gt;
&lt;p&gt;Now, we’re able to encrypt our data, and let’s implement the
&lt;code&gt;Decrypt&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb7&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb7-1&#34;&gt;&lt;a href=&#34;#cb7-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-2&#34;&gt;&lt;a href=&#34;#cb7-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/aes&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-3&#34;&gt;&lt;a href=&#34;#cb7-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/cipher&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-4&#34;&gt;&lt;a href=&#34;#cb7-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-5&#34;&gt;&lt;a href=&#34;#cb7-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-6&#34;&gt;&lt;a href=&#34;#cb7-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Decrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-7&#34;&gt;&lt;a href=&#34;#cb7-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    blockCipher&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; aes&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewCipher&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-8&#34;&gt;&lt;a href=&#34;#cb7-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-9&#34;&gt;&lt;a href=&#34;#cb7-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb7-10&#34;&gt;&lt;a href=&#34;#cb7-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-11&#34;&gt;&lt;a href=&#34;#cb7-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-12&#34;&gt;&lt;a href=&#34;#cb7-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    gcm&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; cipher&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewGCM&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;blockCipher&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-13&#34;&gt;&lt;a href=&#34;#cb7-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-14&#34;&gt;&lt;a href=&#34;#cb7-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb7-15&#34;&gt;&lt;a href=&#34;#cb7-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-16&#34;&gt;&lt;a href=&#34;#cb7-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-17&#34;&gt;&lt;a href=&#34;#cb7-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[:&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;()],&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;():]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-18&#34;&gt;&lt;a href=&#34;#cb7-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-19&#34;&gt;&lt;a href=&#34;#cb7-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Open&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-20&#34;&gt;&lt;a href=&#34;#cb7-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-21&#34;&gt;&lt;a href=&#34;#cb7-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb7-22&#34;&gt;&lt;a href=&#34;#cb7-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-23&#34;&gt;&lt;a href=&#34;#cb7-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-24&#34;&gt;&lt;a href=&#34;#cb7-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb7-25&#34;&gt;&lt;a href=&#34;#cb7-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again let’s go over the code and check what it does. It is largely
the same code as the &lt;code&gt;Encrypt&lt;/code&gt; function, so let’s inspect the
parts that differ.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb8&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb8-1&#34;&gt;&lt;a href=&#34;#cb8-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[:&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;()],&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;():]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Remember from the last section, that we prepended the
&lt;code&gt;nonce&lt;/code&gt; to the &lt;code&gt;data&lt;/code&gt; using &lt;code&gt;gcm.Seal&lt;/code&gt;
to create the &lt;code&gt;ciphertext&lt;/code&gt;? Now we need to split those parts
so we can use them independently. And we’re creating those part by
slicing the &lt;code&gt;data&lt;/code&gt; based on the size of the
&lt;code&gt;nonce&lt;/code&gt; that &lt;code&gt;gcm&lt;/code&gt; provides.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb9&#34;&gt;&lt;pre class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb9-1&#34;&gt;&lt;a href=&#34;#cb9-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Open&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, we’re using &lt;code&gt;gcm.Open&lt;/code&gt; to decrypt the
&lt;code&gt;ciphertext&lt;/code&gt; into &lt;code&gt;plaintext&lt;/code&gt;. &lt;a href=&#34;#fn9&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref9&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;9&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;key&#34;&gt;Key&lt;/h2&gt;
&lt;p&gt;We’ve been passing in a &lt;code&gt;key&lt;/code&gt; to both the
&lt;code&gt;Encrypt&lt;/code&gt; and &lt;code&gt;Decrypt&lt;/code&gt; functions, but we have yet
to make it, so let’s do that.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb10&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb10-1&#34;&gt;&lt;a href=&#34;#cb10-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-2&#34;&gt;&lt;a href=&#34;#cb10-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/rand&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-3&#34;&gt;&lt;a href=&#34;#cb10-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-4&#34;&gt;&lt;a href=&#34;#cb10-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-5&#34;&gt;&lt;a href=&#34;#cb10-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; GenerateKey&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-6&#34;&gt;&lt;a href=&#34;#cb10-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    key &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-7&#34;&gt;&lt;a href=&#34;#cb10-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-8&#34;&gt;&lt;a href=&#34;#cb10-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-9&#34;&gt;&lt;a href=&#34;#cb10-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-10&#34;&gt;&lt;a href=&#34;#cb10-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb10-11&#34;&gt;&lt;a href=&#34;#cb10-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-12&#34;&gt;&lt;a href=&#34;#cb10-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-13&#34;&gt;&lt;a href=&#34;#cb10-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb10-14&#34;&gt;&lt;a href=&#34;#cb10-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we’re generating a random &lt;code&gt;key&lt;/code&gt; using Go’s
&lt;code&gt;crypto/rand&lt;/code&gt; package. For AES we need a &lt;code&gt;key&lt;/code&gt;
that has the length of 32 bytes, so we make a byte slice of size 32.
Then we let &lt;code&gt;rand.Read()&lt;/code&gt; fill the slice with random bytes.&lt;a
href=&#34;#fn10&#34; class=&#34;footnote-ref&#34; id=&#34;fnref10&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now we have enough to encrypt and decrypt some data, so let’s put it
all together and test it out:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb11&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb11-1&#34;&gt;&lt;a href=&#34;#cb11-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// crypto.go&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-2&#34;&gt;&lt;a href=&#34;#cb11-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;package&lt;/span&gt; main&lt;/span&gt;
&lt;span id=&#34;cb11-3&#34;&gt;&lt;a href=&#34;#cb11-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-4&#34;&gt;&lt;a href=&#34;#cb11-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-5&#34;&gt;&lt;a href=&#34;#cb11-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/aes&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-6&#34;&gt;&lt;a href=&#34;#cb11-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/cipher&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-7&#34;&gt;&lt;a href=&#34;#cb11-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/rand&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-8&#34;&gt;&lt;a href=&#34;#cb11-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;encoding/hex&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-9&#34;&gt;&lt;a href=&#34;#cb11-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;fmt&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-10&#34;&gt;&lt;a href=&#34;#cb11-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;log&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-11&#34;&gt;&lt;a href=&#34;#cb11-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-12&#34;&gt;&lt;a href=&#34;#cb11-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-13&#34;&gt;&lt;a href=&#34;#cb11-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Encrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-14&#34;&gt;&lt;a href=&#34;#cb11-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    blockCipher&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; aes&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewCipher&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-15&#34;&gt;&lt;a href=&#34;#cb11-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-16&#34;&gt;&lt;a href=&#34;#cb11-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb11-17&#34;&gt;&lt;a href=&#34;#cb11-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-18&#34;&gt;&lt;a href=&#34;#cb11-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-19&#34;&gt;&lt;a href=&#34;#cb11-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    gcm&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; cipher&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewGCM&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;blockCipher&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-20&#34;&gt;&lt;a href=&#34;#cb11-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-21&#34;&gt;&lt;a href=&#34;#cb11-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb11-22&#34;&gt;&lt;a href=&#34;#cb11-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-23&#34;&gt;&lt;a href=&#34;#cb11-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-24&#34;&gt;&lt;a href=&#34;#cb11-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    nonce &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-25&#34;&gt;&lt;a href=&#34;#cb11-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-26&#34;&gt;&lt;a href=&#34;#cb11-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb11-27&#34;&gt;&lt;a href=&#34;#cb11-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-28&#34;&gt;&lt;a href=&#34;#cb11-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-29&#34;&gt;&lt;a href=&#34;#cb11-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    ciphertext &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Seal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-30&#34;&gt;&lt;a href=&#34;#cb11-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-31&#34;&gt;&lt;a href=&#34;#cb11-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-32&#34;&gt;&lt;a href=&#34;#cb11-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-33&#34;&gt;&lt;a href=&#34;#cb11-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-34&#34;&gt;&lt;a href=&#34;#cb11-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Decrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-35&#34;&gt;&lt;a href=&#34;#cb11-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    blockCipher&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; aes&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewCipher&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-36&#34;&gt;&lt;a href=&#34;#cb11-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-37&#34;&gt;&lt;a href=&#34;#cb11-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb11-38&#34;&gt;&lt;a href=&#34;#cb11-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-39&#34;&gt;&lt;a href=&#34;#cb11-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-40&#34;&gt;&lt;a href=&#34;#cb11-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    gcm&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; cipher&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewGCM&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;blockCipher&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-41&#34;&gt;&lt;a href=&#34;#cb11-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-42&#34;&gt;&lt;a href=&#34;#cb11-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb11-43&#34;&gt;&lt;a href=&#34;#cb11-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-44&#34;&gt;&lt;a href=&#34;#cb11-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-45&#34;&gt;&lt;a href=&#34;#cb11-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[:&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;()],&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;():]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-46&#34;&gt;&lt;a href=&#34;#cb11-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-47&#34;&gt;&lt;a href=&#34;#cb11-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Open&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-48&#34;&gt;&lt;a href=&#34;#cb11-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-49&#34;&gt;&lt;a href=&#34;#cb11-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb11-50&#34;&gt;&lt;a href=&#34;#cb11-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-51&#34;&gt;&lt;a href=&#34;#cb11-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-52&#34;&gt;&lt;a href=&#34;#cb11-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-53&#34;&gt;&lt;a href=&#34;#cb11-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-54&#34;&gt;&lt;a href=&#34;#cb11-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-55&#34;&gt;&lt;a href=&#34;#cb11-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; GenerateKey&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-56&#34;&gt;&lt;a href=&#34;#cb11-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    key &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-57&#34;&gt;&lt;a href=&#34;#cb11-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-58&#34;&gt;&lt;a href=&#34;#cb11-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-59&#34;&gt;&lt;a href=&#34;#cb11-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-60&#34;&gt;&lt;a href=&#34;#cb11-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb11-61&#34;&gt;&lt;a href=&#34;#cb11-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-62&#34;&gt;&lt;a href=&#34;#cb11-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-63&#34;&gt;&lt;a href=&#34;#cb11-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-64&#34;&gt;&lt;a href=&#34;#cb11-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-65&#34;&gt;&lt;a href=&#34;#cb11-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-66&#34;&gt;&lt;a href=&#34;#cb11-66&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-67&#34;&gt;&lt;a href=&#34;#cb11-67&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    data &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;our super secret text&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-68&#34;&gt;&lt;a href=&#34;#cb11-68&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-69&#34;&gt;&lt;a href=&#34;#cb11-69&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; GenerateKey&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-70&#34;&gt;&lt;a href=&#34;#cb11-70&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-71&#34;&gt;&lt;a href=&#34;#cb11-71&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-72&#34;&gt;&lt;a href=&#34;#cb11-72&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-73&#34;&gt;&lt;a href=&#34;#cb11-73&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-74&#34;&gt;&lt;a href=&#34;#cb11-74&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; Encrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-75&#34;&gt;&lt;a href=&#34;#cb11-75&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-76&#34;&gt;&lt;a href=&#34;#cb11-76&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-77&#34;&gt;&lt;a href=&#34;#cb11-77&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-78&#34;&gt;&lt;a href=&#34;#cb11-78&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-79&#34;&gt;&lt;a href=&#34;#cb11-79&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    fmt&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;ciphertext: %s&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; hex&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;EncodeToString&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;ciphertext&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-80&#34;&gt;&lt;a href=&#34;#cb11-80&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-81&#34;&gt;&lt;a href=&#34;#cb11-81&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; Decrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-82&#34;&gt;&lt;a href=&#34;#cb11-82&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-83&#34;&gt;&lt;a href=&#34;#cb11-83&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-84&#34;&gt;&lt;a href=&#34;#cb11-84&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-85&#34;&gt;&lt;a href=&#34;#cb11-85&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-86&#34;&gt;&lt;a href=&#34;#cb11-86&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    fmt&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;plaintext: %s&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; plaintext&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb11-87&#34;&gt;&lt;a href=&#34;#cb11-87&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And, we can run this example with:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb12&#34;&gt;&lt;pre
class=&#34;sourceCode bash&#34;&gt;&lt;code class=&#34;sourceCode bash&#34;&gt;&lt;span id=&#34;cb12-1&#34;&gt;&lt;a href=&#34;#cb12-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; go run crypto.go&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, we have enough to encrypt and decrypt our &lt;code&gt;data&lt;/code&gt; with
a randomized key. This is cool and now we have a &lt;code&gt;key&lt;/code&gt; that
allows us to encrypt and decrypt our &lt;code&gt;data&lt;/code&gt;. But that means
that the &lt;code&gt;key&lt;/code&gt; now becomes our password and weren’t able to
choose it ourselves, additionally it has a length of 32 bytes.&lt;/p&gt;
&lt;p&gt;But, as mentioned in the start of the post, we want to be able to
encrypt and decrypt the data by providing our own &lt;code&gt;key&lt;/code&gt;
namely a password that we’ve chosen to use. We will be doing that in the
following section.&lt;/p&gt;
&lt;h2 id=&#34;password&#34;&gt;Password&lt;/h2&gt;
&lt;p&gt;Now, the &lt;code&gt;aes.NewCipher()&lt;/code&gt; needs a 16, 24, or a 32 byte
&lt;code&gt;key&lt;/code&gt;, and in this example we are using a 32 byte
&lt;code&gt;key&lt;/code&gt;. However, our password likely isn’t going to be 32
bytes. So we need to transform our password to a suitable
&lt;code&gt;key&lt;/code&gt;. And we do that by using a &lt;em&gt;key derivation
function&lt;/em&gt; (KDF)&lt;a href=&#34;#fn11&#34; class=&#34;footnote-ref&#34; id=&#34;fnref11&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;11&lt;/sup&gt;&lt;/a&gt; to ‘stretch’ the password to make
it a suitable cryptographic key. This key-stretching&lt;a href=&#34;#fn12&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref12&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;12&lt;/sup&gt;&lt;/a&gt;
characterizes itself by being slow. This is done in order to make it
that, an attacker needs to spend a lot of resources to attempt to brute
force an attack the on the password. We have several options for KDF’s:
Argon2&lt;a href=&#34;#fn13&#34; class=&#34;footnote-ref&#34; id=&#34;fnref13&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;13&lt;/sup&gt;&lt;/a&gt;, scrypt&lt;a href=&#34;#fn14&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref14&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;14&lt;/sup&gt;&lt;/a&gt;,
bcrypt&lt;a href=&#34;#fn15&#34; class=&#34;footnote-ref&#34; id=&#34;fnref15&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;15&lt;/sup&gt;&lt;/a&gt;, and pbkdf2&lt;a href=&#34;#fn16&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref16&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;16&lt;/sup&gt;&lt;/a&gt;.
Choosing one depends on several factors, but mainly how safe it is. &lt;a
href=&#34;#fn17&#34; class=&#34;footnote-ref&#34; id=&#34;fnref17&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;17&lt;/sup&gt;&lt;/a&gt; &lt;a href=&#34;#fn18&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref18&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;18&lt;/sup&gt;&lt;/a&gt;
&lt;a href=&#34;#fn19&#34; class=&#34;footnote-ref&#34; id=&#34;fnref19&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;19&lt;/sup&gt;&lt;/a&gt; &lt;a href=&#34;#fn20&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref20&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;20&lt;/sup&gt;&lt;/a&gt;
&lt;a href=&#34;#fn21&#34; class=&#34;footnote-ref&#34; id=&#34;fnref21&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;21&lt;/sup&gt;&lt;/a&gt; &lt;a href=&#34;#fn22&#34;
class=&#34;footnote-ref&#34; id=&#34;fnref22&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;22&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Typically in a KDF we have a &lt;code&gt;password&lt;/code&gt;, a
&lt;code&gt;salt&lt;/code&gt;, and an &lt;code&gt;iterations&lt;/code&gt; argument. The
&lt;code&gt;salt&lt;/code&gt;&lt;a href=&#34;#fn23&#34; class=&#34;footnote-ref&#34; id=&#34;fnref23&#34;
role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;23&lt;/sup&gt;&lt;/a&gt; is used to prevent an attacker from
just storing password/key pairs, and prevents an attacker from
precomputing a dictionary of derived keys, as a different salt yields a
different output. Each password has to be checked with the
&lt;code&gt;salt&lt;/code&gt; used to derive the key. &lt;span class=&#34;citation&#34;
data-cites=&#34;isom2015 wiki_kdf&#34;&gt;(&lt;a href=&#34;#ref-isom2015&#34;
role=&#34;doc-biblioref&#34;&gt;Isom 2015&lt;/a&gt;; &lt;a href=&#34;#ref-wiki_kdf&#34;
role=&#34;doc-biblioref&#34;&gt;Wikipedia 2020&lt;/a&gt;)&lt;/span&gt; The &lt;code&gt;salt&lt;/code&gt; is
related to the &lt;code&gt;nonce&lt;/code&gt; in that it also needs to be randomly
generated. And as with the &lt;code&gt;nonce&lt;/code&gt;, the &lt;code&gt;salt&lt;/code&gt;
doesn’t need to be secret, it needs to be unique. The
&lt;code&gt;iterations&lt;/code&gt; argument or the &lt;em&gt;difficulty parameter&lt;/em&gt;,
signifies how many times to repeat the process. This is because, even
with &lt;code&gt;salt&lt;/code&gt;, a &lt;em&gt;dictionary attack&lt;/em&gt; is still possible,
but with the &lt;code&gt;iterations&lt;/code&gt; count, it will slow down the time
it takes to compute a &lt;code&gt;key&lt;/code&gt; from a password. &lt;span
class=&#34;citation&#34; data-cites=&#34;viega2003&#34;&gt;(&lt;a href=&#34;#ref-viega2003&#34;
role=&#34;doc-biblioref&#34;&gt;Viega and Messier 2003, 141–42&lt;/a&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this example we’ll be using &lt;em&gt;scrypt&lt;/em&gt;, so let’s see how we
can implement that into our program.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb13&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb13-1&#34;&gt;&lt;a href=&#34;#cb13-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-2&#34;&gt;&lt;a href=&#34;#cb13-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/rand&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-3&#34;&gt;&lt;a href=&#34;#cb13-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-4&#34;&gt;&lt;a href=&#34;#cb13-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;golang.org/x/crypto/scrypt&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-5&#34;&gt;&lt;a href=&#34;#cb13-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-6&#34;&gt;&lt;a href=&#34;#cb13-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-7&#34;&gt;&lt;a href=&#34;#cb13-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; DeriveKey&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-8&#34;&gt;&lt;a href=&#34;#cb13-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; salt &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-9&#34;&gt;&lt;a href=&#34;#cb13-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        salt &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-10&#34;&gt;&lt;a href=&#34;#cb13-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;salt&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-11&#34;&gt;&lt;a href=&#34;#cb13-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb13-12&#34;&gt;&lt;a href=&#34;#cb13-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-13&#34;&gt;&lt;a href=&#34;#cb13-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-14&#34;&gt;&lt;a href=&#34;#cb13-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-15&#34;&gt;&lt;a href=&#34;#cb13-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; scrypt&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Key&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1048576&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-16&#34;&gt;&lt;a href=&#34;#cb13-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-17&#34;&gt;&lt;a href=&#34;#cb13-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb13-18&#34;&gt;&lt;a href=&#34;#cb13-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-19&#34;&gt;&lt;a href=&#34;#cb13-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-20&#34;&gt;&lt;a href=&#34;#cb13-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb13-21&#34;&gt;&lt;a href=&#34;#cb13-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again let’s go over the code and see what it does.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb14&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb14-1&#34;&gt;&lt;a href=&#34;#cb14-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; DeriveKey&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we accept the password as a slice of bytes as the argument, and
we return the resulting &lt;code&gt;key&lt;/code&gt;, and &lt;code&gt;salt&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb15&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb15-1&#34;&gt;&lt;a href=&#34;#cb15-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;salt &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb15-2&#34;&gt;&lt;a href=&#34;#cb15-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;salt&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb15-3&#34;&gt;&lt;a href=&#34;#cb15-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb15-4&#34;&gt;&lt;a href=&#34;#cb15-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Just like our &lt;code&gt;Encrypt&lt;/code&gt; function, we’ll be creating the
&lt;code&gt;salt&lt;/code&gt; with 32 random bytes.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb16&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb16-1&#34;&gt;&lt;a href=&#34;#cb16-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; scrypt&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Key&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1048576&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we’re using the &lt;code&gt;scrypt&lt;/code&gt; package from
&lt;code&gt;golang.org/x/&lt;/code&gt; library. &lt;a href=&#34;#fn24&#34; class=&#34;footnote-ref&#34;
id=&#34;fnref24&#34; role=&#34;doc-noteref&#34;&gt;&lt;sup&gt;24&lt;/sup&gt;&lt;/a&gt; From the documentation
we can read that the &lt;code&gt;Key&lt;/code&gt; function accepts the following
arguments:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb17&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb17-1&#34;&gt;&lt;a href=&#34;#cb17-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Key&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; N&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; r&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; p&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; keyLen &lt;span class=&#34;dt&#34;&gt;int&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The arguments &lt;code&gt;password&lt;/code&gt; and &lt;code&gt;salt&lt;/code&gt; speak for
themselves. &lt;code&gt;N&lt;/code&gt; is the number of iterations. In a
presentation given by C. Percival it is recommended that for interactive
logins
&lt;math display=&#34;inline&#34; xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;mn&gt;16384&lt;/mn&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;16384&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
(&lt;math display=&#34;inline&#34; xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;14&lt;/mn&gt;&lt;/msup&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;2^{14}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;)
iterations, and for file encryption
&lt;math display=&#34;inline&#34; xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;mn&gt;1048576&lt;/mn&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;1048576&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;
(&lt;math display=&#34;inline&#34; xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;20&lt;/mn&gt;&lt;/msup&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;2^{20}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;)
iterations are used. &lt;span class=&#34;citation&#34;
data-cites=&#34;percival2005b percival2005a isom2015&#34;&gt;(&lt;a
href=&#34;#ref-percival2005b&#34; role=&#34;doc-biblioref&#34;&gt;Percival 2005a&lt;/a&gt;, &lt;a
href=&#34;#ref-percival2005a&#34; role=&#34;doc-biblioref&#34;&gt;2005b&lt;/a&gt;; &lt;a
href=&#34;#ref-isom2015&#34; role=&#34;doc-biblioref&#34;&gt;Isom 2015&lt;/a&gt;)&lt;/span&gt; The
arguments &lt;code&gt;r&lt;/code&gt; and &lt;code&gt;p&lt;/code&gt; must satisfy
&lt;math display=&#34;inline&#34; xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;*&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo&gt;&lt;&lt;/mo&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;r * p &lt; 2^{30}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;,
if it doesn’t satisfy the limits, the function returns a
&lt;code&gt;nil&lt;/code&gt; byte slice and an error. &lt;span class=&#34;citation&#34;
data-cites=&#34;godoc_crypto_scrypt&#34;&gt;(&lt;a href=&#34;#ref-godoc_crypto_scrypt&#34;
role=&#34;doc-biblioref&#34;&gt;Golang Documentation 2020&lt;/a&gt;)&lt;/span&gt;. The
&lt;code&gt;r&lt;/code&gt; argument defines the relative memory cost parameter it
controls the blocksize in the underlying hash, the recommended value is
8. The &lt;code&gt;p&lt;/code&gt; argument is the relative CPU cost parameter and
the recommended value for this is 1. &lt;span class=&#34;citation&#34;
data-cites=&#34;isom2015 percival2005b&#34;&gt;(&lt;a href=&#34;#ref-isom2015&#34;
role=&#34;doc-biblioref&#34;&gt;Isom 2015&lt;/a&gt;; &lt;a href=&#34;#ref-percival2005b&#34;
role=&#34;doc-biblioref&#34;&gt;Percival 2005a&lt;/a&gt;)&lt;/span&gt; The &lt;code&gt;keyLen&lt;/code&gt;
argument defines the length of the bytes that are returned as key, as
discussed this will be 32 bytes.&lt;/p&gt;
&lt;h2 id=&#34;result&#34;&gt;Result&lt;/h2&gt;
&lt;p&gt;Now that we’ve created our &lt;code&gt;DeriveKey&lt;/code&gt; function we need to
update our code to support it. So let’s do that, it should resemble the
code below:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb18&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb18-1&#34;&gt;&lt;a href=&#34;#cb18-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;// scrypt.go&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-2&#34;&gt;&lt;a href=&#34;#cb18-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;package&lt;/span&gt; main&lt;/span&gt;
&lt;span id=&#34;cb18-3&#34;&gt;&lt;a href=&#34;#cb18-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-4&#34;&gt;&lt;a href=&#34;#cb18-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-5&#34;&gt;&lt;a href=&#34;#cb18-5&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/aes&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-6&#34;&gt;&lt;a href=&#34;#cb18-6&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/cipher&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-7&#34;&gt;&lt;a href=&#34;#cb18-7&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/rand&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-8&#34;&gt;&lt;a href=&#34;#cb18-8&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;crypto/sha256&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-9&#34;&gt;&lt;a href=&#34;#cb18-9&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;encoding/hex&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-10&#34;&gt;&lt;a href=&#34;#cb18-10&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;fmt&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-11&#34;&gt;&lt;a href=&#34;#cb18-11&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;log&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-12&#34;&gt;&lt;a href=&#34;#cb18-12&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-13&#34;&gt;&lt;a href=&#34;#cb18-13&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;st&#34;&gt;&#34;golang.org/x/crypto/scrypt&#34;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-14&#34;&gt;&lt;a href=&#34;#cb18-14&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-15&#34;&gt;&lt;a href=&#34;#cb18-15&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-16&#34;&gt;&lt;a href=&#34;#cb18-16&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Encrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-17&#34;&gt;&lt;a href=&#34;#cb18-17&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; DeriveKey&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-18&#34;&gt;&lt;a href=&#34;#cb18-18&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-19&#34;&gt;&lt;a href=&#34;#cb18-19&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-20&#34;&gt;&lt;a href=&#34;#cb18-20&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-21&#34;&gt;&lt;a href=&#34;#cb18-21&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-22&#34;&gt;&lt;a href=&#34;#cb18-22&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    blockCipher&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; aes&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewCipher&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-23&#34;&gt;&lt;a href=&#34;#cb18-23&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-24&#34;&gt;&lt;a href=&#34;#cb18-24&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-25&#34;&gt;&lt;a href=&#34;#cb18-25&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-26&#34;&gt;&lt;a href=&#34;#cb18-26&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-27&#34;&gt;&lt;a href=&#34;#cb18-27&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    gcm&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; cipher&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewGCM&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;blockCipher&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-28&#34;&gt;&lt;a href=&#34;#cb18-28&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-29&#34;&gt;&lt;a href=&#34;#cb18-29&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-30&#34;&gt;&lt;a href=&#34;#cb18-30&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-31&#34;&gt;&lt;a href=&#34;#cb18-31&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-32&#34;&gt;&lt;a href=&#34;#cb18-32&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    nonce &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-33&#34;&gt;&lt;a href=&#34;#cb18-33&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-34&#34;&gt;&lt;a href=&#34;#cb18-34&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-35&#34;&gt;&lt;a href=&#34;#cb18-35&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-36&#34;&gt;&lt;a href=&#34;#cb18-36&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-37&#34;&gt;&lt;a href=&#34;#cb18-37&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    ciphertext &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Seal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-38&#34;&gt;&lt;a href=&#34;#cb18-38&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-39&#34;&gt;&lt;a href=&#34;#cb18-39&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    ciphertext &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;...)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-40&#34;&gt;&lt;a href=&#34;#cb18-40&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-41&#34;&gt;&lt;a href=&#34;#cb18-41&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-42&#34;&gt;&lt;a href=&#34;#cb18-42&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-43&#34;&gt;&lt;a href=&#34;#cb18-43&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-44&#34;&gt;&lt;a href=&#34;#cb18-44&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; Decrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-45&#34;&gt;&lt;a href=&#34;#cb18-45&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;bu&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;data&lt;span class=&#34;op&#34;&gt;)-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;:],&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[:&lt;/span&gt;&lt;span class=&#34;bu&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;data&lt;span class=&#34;op&#34;&gt;)-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-46&#34;&gt;&lt;a href=&#34;#cb18-46&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-47&#34;&gt;&lt;a href=&#34;#cb18-47&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; DeriveKey&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-48&#34;&gt;&lt;a href=&#34;#cb18-48&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-49&#34;&gt;&lt;a href=&#34;#cb18-49&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-50&#34;&gt;&lt;a href=&#34;#cb18-50&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-51&#34;&gt;&lt;a href=&#34;#cb18-51&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-52&#34;&gt;&lt;a href=&#34;#cb18-52&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    blockCipher&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; aes&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewCipher&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-53&#34;&gt;&lt;a href=&#34;#cb18-53&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-54&#34;&gt;&lt;a href=&#34;#cb18-54&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-55&#34;&gt;&lt;a href=&#34;#cb18-55&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-56&#34;&gt;&lt;a href=&#34;#cb18-56&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-57&#34;&gt;&lt;a href=&#34;#cb18-57&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    gcm&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; cipher&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NewGCM&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;blockCipher&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-58&#34;&gt;&lt;a href=&#34;#cb18-58&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-59&#34;&gt;&lt;a href=&#34;#cb18-59&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-60&#34;&gt;&lt;a href=&#34;#cb18-60&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-61&#34;&gt;&lt;a href=&#34;#cb18-61&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-62&#34;&gt;&lt;a href=&#34;#cb18-62&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[:&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;()],&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;NonceSize&lt;span class=&#34;op&#34;&gt;():]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-63&#34;&gt;&lt;a href=&#34;#cb18-63&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-64&#34;&gt;&lt;a href=&#34;#cb18-64&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; gcm&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Open&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; nonce&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-65&#34;&gt;&lt;a href=&#34;#cb18-65&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-66&#34;&gt;&lt;a href=&#34;#cb18-66&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-67&#34;&gt;&lt;a href=&#34;#cb18-67&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-68&#34;&gt;&lt;a href=&#34;#cb18-68&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-69&#34;&gt;&lt;a href=&#34;#cb18-69&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-70&#34;&gt;&lt;a href=&#34;#cb18-70&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-71&#34;&gt;&lt;a href=&#34;#cb18-71&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-72&#34;&gt;&lt;a href=&#34;#cb18-72&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; DeriveKey&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-73&#34;&gt;&lt;a href=&#34;#cb18-73&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; salt &lt;span class=&#34;op&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-74&#34;&gt;&lt;a href=&#34;#cb18-74&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        salt &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-75&#34;&gt;&lt;a href=&#34;#cb18-75&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; rand&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Read&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;salt&lt;span class=&#34;op&#34;&gt;);&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-76&#34;&gt;&lt;a href=&#34;#cb18-76&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;            &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-77&#34;&gt;&lt;a href=&#34;#cb18-77&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-78&#34;&gt;&lt;a href=&#34;#cb18-78&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-79&#34;&gt;&lt;a href=&#34;#cb18-79&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-80&#34;&gt;&lt;a href=&#34;#cb18-80&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; scrypt&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Key&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1048576&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-81&#34;&gt;&lt;a href=&#34;#cb18-81&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-82&#34;&gt;&lt;a href=&#34;#cb18-82&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err&lt;/span&gt;
&lt;span id=&#34;cb18-83&#34;&gt;&lt;a href=&#34;#cb18-83&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-84&#34;&gt;&lt;a href=&#34;#cb18-84&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-85&#34;&gt;&lt;a href=&#34;#cb18-85&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;return&lt;/span&gt; key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-86&#34;&gt;&lt;a href=&#34;#cb18-86&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-87&#34;&gt;&lt;a href=&#34;#cb18-87&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-88&#34;&gt;&lt;a href=&#34;#cb18-88&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;kw&#34;&gt;func&lt;/span&gt; main&lt;span class=&#34;op&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-89&#34;&gt;&lt;a href=&#34;#cb18-89&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;kw&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-90&#34;&gt;&lt;a href=&#34;#cb18-90&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        password &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;mysecretpassword&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-91&#34;&gt;&lt;a href=&#34;#cb18-91&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        data     &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;dt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;our super secret text&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-92&#34;&gt;&lt;a href=&#34;#cb18-92&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-93&#34;&gt;&lt;a href=&#34;#cb18-93&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-94&#34;&gt;&lt;a href=&#34;#cb18-94&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; Encrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-95&#34;&gt;&lt;a href=&#34;#cb18-95&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-96&#34;&gt;&lt;a href=&#34;#cb18-96&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-97&#34;&gt;&lt;a href=&#34;#cb18-97&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-98&#34;&gt;&lt;a href=&#34;#cb18-98&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-99&#34;&gt;&lt;a href=&#34;#cb18-99&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    fmt&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;ciphertext: %s&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; hex&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;EncodeToString&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;ciphertext&lt;span class=&#34;op&#34;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-100&#34;&gt;&lt;a href=&#34;#cb18-100&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-101&#34;&gt;&lt;a href=&#34;#cb18-101&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    plaintext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; Decrypt&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;password&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; ciphertext&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-102&#34;&gt;&lt;a href=&#34;#cb18-102&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;cf&#34;&gt;if&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt; &lt;span class=&#34;op&#34;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-103&#34;&gt;&lt;a href=&#34;#cb18-103&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;        log&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Fatal&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;err&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-104&#34;&gt;&lt;a href=&#34;#cb18-104&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    &lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-105&#34;&gt;&lt;a href=&#34;#cb18-105&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-106&#34;&gt;&lt;a href=&#34;#cb18-106&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;    fmt&lt;span class=&#34;op&#34;&gt;.&lt;/span&gt;Printf&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;plaintext: %s&lt;/span&gt;&lt;span class=&#34;ch&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;st&#34;&gt;&#34;&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; plaintext&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb18-107&#34;&gt;&lt;a href=&#34;#cb18-107&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;op&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And, we’re able to run and test it:&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb19&#34;&gt;&lt;pre
class=&#34;sourceCode bash&#34;&gt;&lt;code class=&#34;sourceCode bash&#34;&gt;&lt;span id=&#34;cb19-1&#34;&gt;&lt;a href=&#34;#cb19-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;co&#34;&gt;# First we need to get the scrypt package&lt;/span&gt;&lt;/span&gt;
&lt;span id=&#34;cb19-2&#34;&gt;&lt;a href=&#34;#cb19-2&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; go get &lt;span class=&#34;at&#34;&gt;-u&lt;/span&gt; golang.org/x/crypto/scrypt&lt;/span&gt;
&lt;span id=&#34;cb19-3&#34;&gt;&lt;a href=&#34;#cb19-3&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&#34;cb19-4&#34;&gt;&lt;a href=&#34;#cb19-4&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;&lt;span class=&#34;ex&#34;&gt;$&lt;/span&gt; go run scrypt.go&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We’ve updated some parts, so let’s go over it.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb20&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb20-1&#34;&gt;&lt;a href=&#34;#cb20-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; DeriveKey&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;ot&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the &lt;code&gt;Encrypt&lt;/code&gt; function we create our key by passing in
our password, which is contained in the &lt;code&gt;key&lt;/code&gt; argument. We
pass in &lt;code&gt;nil&lt;/code&gt; as the salt argument, that is because we want
to create the &lt;code&gt;salt&lt;/code&gt; since it is the first time we encrypt
our &lt;code&gt;data&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb21&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb21-1&#34;&gt;&lt;a href=&#34;#cb21-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;ciphertext &lt;span class=&#34;op&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bu&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;ciphertext&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;...)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Additionally, in the &lt;code&gt;Encrypt&lt;/code&gt; function, we append the
&lt;code&gt;salt&lt;/code&gt; to our &lt;code&gt;ciphertext&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb22&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb22-1&#34;&gt;&lt;a href=&#34;#cb22-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;salt&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; data &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;bu&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;data&lt;span class=&#34;op&#34;&gt;)-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;:],&lt;/span&gt; data&lt;span class=&#34;op&#34;&gt;[:&lt;/span&gt;&lt;span class=&#34;bu&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;data&lt;span class=&#34;op&#34;&gt;)-&lt;/span&gt;&lt;span class=&#34;dv&#34;&gt;32&lt;/span&gt;&lt;span class=&#34;op&#34;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And, because we append the &lt;code&gt;salt&lt;/code&gt; to the
&lt;code&gt;ciphertext&lt;/code&gt;, we need to split and slice it in the
&lt;code&gt;Decrypt&lt;/code&gt; function, because we’re going to use it in the
&lt;code&gt;DeriveKey&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34; id=&#34;cb23&#34;&gt;&lt;pre
class=&#34;sourceCode go&#34;&gt;&lt;code class=&#34;sourceCode go&#34;&gt;&lt;span id=&#34;cb23-1&#34;&gt;&lt;a href=&#34;#cb23-1&#34; aria-hidden=&#34;true&#34; tabindex=&#34;-1&#34;&gt;&lt;/a&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; _&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; err &lt;span class=&#34;op&#34;&gt;:=&lt;/span&gt; DeriveKey&lt;span class=&#34;op&#34;&gt;(&lt;/span&gt;key&lt;span class=&#34;op&#34;&gt;,&lt;/span&gt; salt&lt;span class=&#34;op&#34;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see here we pass in the salt to the &lt;code&gt;DeriveKey&lt;/code&gt;
function and we’ll be able to retrieve the &lt;code&gt;key&lt;/code&gt; that we used
in order to encrypt our data.&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;With that, we’ve created two ways in order to encrypt and decrypt our
data in Go. First we’ve encrypted our data by using the AES encryption
algorithm, for which we’ve created a randomized key to be used for
decrypting our data. Subsequently, we’ve updated our code to support
using a password as our key. We’ve done that by &lt;em&gt;key-stretching&lt;/em&gt;
our password using a &lt;em&gt;key derivation function&lt;/em&gt;, and we’ve used
&lt;em&gt;scrypt&lt;/em&gt; to achieve that. Hopefully, you found this post useful,
and again I advice you to read and watch the sources that I’ve listed,
and check out other sources to get a good overview on how to correctly
and securely encrypt your data, and if you have any suggestions let me
know.&lt;/p&gt;
&lt;h2 class=&#34;unnumbered&#34; id=&#34;references&#34;&gt;References&lt;/h2&gt;
&lt;div id=&#34;refs&#34; class=&#34;references csl-bib-body hanging-indent&#34;
data-entry-spacing=&#34;0&#34; role=&#34;list&#34;&gt;
&lt;div id=&#34;ref-godoc_crypto_scrypt&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Golang Documentation. 2020. &lt;span&gt;“Package Scrypt.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://godoc.org/golang.org/x/crypto/scrypt&#34;&gt;https://godoc.org/golang.org/x/crypto/scrypt&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-isom2015&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Isom, Kyle. 2015. &lt;em&gt;Practical Cryptography with Go&lt;/em&gt;. Leanpub. &lt;a
href=&#34;https://leanpub.com/gocrypto/read&#34;&gt;https://leanpub.com/gocrypto/read&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-percival2005b&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Percival, C. 2005a. &lt;span&gt;“scrypt: A new key derivation
function.”&lt;/span&gt; &lt;a
href=&#34;https://www.bsdcan.org/2009/schedule/attachments/86_scrypt_slides.pdf&#34;&gt;https://www.bsdcan.org/2009/schedule/attachments/86_scrypt_slides.pdf&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-percival2005a&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
———. 2005b. &lt;span&gt;“Strong Key Derivation Via Sequential Memory-Hard
Functions.”&lt;/span&gt; &lt;a
href=&#34;https://www.tarsnap.com/scrypt/scrypt.pdf&#34;&gt;https://www.tarsnap.com/scrypt/scrypt.pdf&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-ryer2015&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Ryer, M. 2015. &lt;span&gt;“Io.Reader in Depth.”&lt;/span&gt; 2015. &lt;a
href=&#34;https://medium.com/@matryer/golang-advent-calendar-day-seventeen-io-reader-in-depth-6f744bb4320b&#34;&gt;https://medium.com/@matryer/golang-advent-calendar-day-seventeen-io-reader-in-depth-6f744bb4320b&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-tankersley2016&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Tankersley, G. 2016. &lt;span&gt;“Go for Crypto Developers.”&lt;/span&gt; &lt;a
href=&#34;https://youtu.be/2r_KMzXB74w&#34;&gt;https://youtu.be/2r_KMzXB74w&lt;/a&gt;.
&lt;/div&gt;
&lt;div id=&#34;ref-viega2003&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Viega, J., and M. Messier. 2003. &lt;em&gt;Secure Programming Cookbook for C
and C++&lt;/em&gt;. O’Reilly Media.
&lt;/div&gt;
&lt;div id=&#34;ref-wiki_kdf&#34; class=&#34;csl-entry&#34; role=&#34;listitem&#34;&gt;
Wikipedia. 2020. &lt;span&gt;“Key Derivation Function - Wikipedia, The Free
Encyclopedia.”&lt;/span&gt; 2020. &lt;a
href=&#34;https://en.wikipedia.org/wiki/Key_derivation_function&#34;&gt;https://en.wikipedia.org/wiki/Key_derivation_function&lt;/a&gt;.
&lt;/div&gt;
&lt;/div&gt;
&lt;section id=&#34;footnotes&#34; class=&#34;footnotes footnotes-end-of-document&#34;
role=&#34;doc-endnotes&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;Some references on how to implement encryption with
streams: &lt;a
href=&#34;https://www.reddit.com/r/golang/comments/87wcik/how_do_you_encrypt_large_ioreader_streams/dwp2k0g?utm_source=share&amp;utm_medium=web2x&#34;&gt;Reddit
Comment&lt;/a&gt;; &lt;a
href=&#34;https://medium.com/blend-engineering/encrypting-streams-in-go-6cff6062a107&#34;&gt;Michael
Turner - Encrypting Streams in Go&lt;/a&gt;; &lt;a
href=&#34;https://github.com/minio/sio/issues/26#issuecomment-378012290&#34;&gt;Minio
- Github Issue&lt;/a&gt;&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/pkg/crypto/aes/&#34;&gt;Go
Documentation: AES&lt;/a&gt;&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Advanced_Encryption_Standard&#34;&gt;Wikipedia:
AES&lt;/a&gt;&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn4&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://www.youtube.com/watch?v=O4xNJsjtN6E&#34;&gt;Computerphile - AES
Explained&lt;/a&gt;&lt;a href=&#34;#fnref4&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn5&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Galois/Counter_Mode&#34;&gt;Wikipedia:
GCM&lt;/a&gt;&lt;a href=&#34;#fnref5&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn6&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/pkg/crypto/cipher/#AEAD&#34;&gt;Go
Documentation: AEAD&lt;/a&gt;&lt;a href=&#34;#fnref6&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn7&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/pkg/crypto/rand/#Read&#34;&gt;Go
Documentation: rand.Read&lt;/a&gt;&lt;a href=&#34;#fnref7&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn8&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/src/crypto/cipher/gcm.go&#34;&gt;Go
Source Code: GCM&lt;/a&gt;&lt;a href=&#34;#fnref8&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn9&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/src/crypto/cipher/gcm.go&#34;&gt;Go
Source Code: GCM&lt;/a&gt;&lt;a href=&#34;#fnref9&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn10&#34;&gt;&lt;p&gt;&lt;a href=&#34;https://golang.org/pkg/crypto/rand/#Read&#34;&gt;Go
Documentation: rand.Read&lt;/a&gt;&lt;a href=&#34;#fnref10&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn11&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Key_derivation_function&#34;&gt;Wikipedia:
KDF&lt;/a&gt;&lt;a href=&#34;#fnref11&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn12&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Key_stretching&#34;&gt;Wikipedia: Key
Stretching&lt;/a&gt;&lt;a href=&#34;#fnref12&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn13&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Argon2&#34;&gt;Wikipedia: Argon2&lt;/a&gt;&lt;a
href=&#34;#fnref13&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn14&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Scrypt&#34;&gt;Wikipedia: scrypt&lt;/a&gt;&lt;a
href=&#34;#fnref14&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn15&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Bcrypt&#34;&gt;Wikipedia: bcrypt&lt;/a&gt;&lt;a
href=&#34;#fnref15&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn16&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/PBKDF2&#34;&gt;Wikipedia: pbkdf2&lt;/a&gt;&lt;a
href=&#34;#fnref16&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn17&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://medium.com/@mpreziuso/password-hashing-pbkdf2-scrypt-bcrypt-and-argon2-e25aaf41598e&#34;&gt;Michele
Prezioso - Password Hashing: PBKDF2, Scrypt, Bcrypt&lt;/a&gt;&lt;a
href=&#34;#fnref17&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn18&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://medium.com/@mpreziuso/password-hashing-pbkdf2-scrypt-bcrypt-and-argon2-e25aaf41598e&#34;&gt;Michele
Prezioso - Password Hashing: Scrypt, Bcrypt and ARGON2&lt;/a&gt;&lt;a
href=&#34;#fnref18&#34; class=&#34;footnote-back&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn19&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://crypto.stackexchange.com/questions/35275/whats-the-difference-between-pbkdf-and-sha-and-why-use-them-together/61306#61306&#34;&gt;StackExchange
- What’s the difference between PBKDF and SHA and why use them
together?&lt;/a&gt;&lt;a href=&#34;#fnref19&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn20&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://github.com/bitwarden/jslib/issues/52&#34;&gt;Github - Bitwarden
Issue 52&lt;/a&gt;&lt;a href=&#34;#fnref20&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn21&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://github.com/bitwarden/server/issues/589&#34;&gt;Github - Bitwarden
Issue 589&lt;/a&gt;&lt;a href=&#34;#fnref21&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn22&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/PBKDF2#Alternatives_to_PBKDF2&#34;&gt;Wikipedia:
pbkdf2 alternatives&lt;/a&gt;&lt;a href=&#34;#fnref22&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn23&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://en.wikipedia.org/wiki/Salt_(cryptography)&#34;&gt;Wikipedia: Salt
(cryptography)&lt;/a&gt;&lt;a href=&#34;#fnref23&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn24&#34;&gt;&lt;p&gt;&lt;a
href=&#34;https://godoc.org/golang.org/x/crypto/scrypt&#34;&gt;Go Documentation:
scrypt&lt;/a&gt;&lt;a href=&#34;#fnref24&#34; class=&#34;footnote-back&#34;
role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
    <item>
      <title>How to profile Go applications inside a docker container</title>
      <link>https://bruinsslot.jp/articles/profiling-golang-docker-2/</link>
      <pubDate>Thu, 12 Dec 2019 12:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/profiling-golang-docker-2/</guid>
      <description>&lt;h2 id=&#34;intro&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In this post I’ll give a quick overview of several methods you can use for
profiling/debugging Go applications that are running in a docker container. To
get a more in-depth overview of the several methods, I’ve added the source
links you can reference at the end of the post. I really advise you to read
the sources listed, and read up on how to use and interpret the created
profiles in order to debug/optimize your application. This post aims to help
you to create those profiles when using a Docker container to run your Go
applications. The main idea is that we are going to leverage the remote
profiling capabilities that is supplied by the &lt;code&gt;net/http&lt;/code&gt; package.&lt;/p&gt;
&lt;h2 id=&#34;application&#34;&gt;Application&lt;/h2&gt;
&lt;p&gt;We will be using &lt;code&gt;pprof&lt;/code&gt; to profile your Go applications, you’ll typically use
&lt;code&gt;pprof&lt;/code&gt; when you’re able to access the binary of your application locally. When
you’re using a docker container you’d need to have the &lt;code&gt;go tool&lt;/code&gt; installed in
the container to be able to profile it. However there is another way for
profiling Go applications running in a docker container. For this we need to
create an HTTP webserver, which will enable the &lt;code&gt;pprof&lt;/code&gt; HTTP endpoints. From
these endpoints we’re able to download profiles of the running application.&lt;/p&gt;
&lt;p&gt;So first we need to create an HTTP webserver, that we will run in a goroutine.
We can then import the &lt;code&gt;net/http/pprof&lt;/code&gt; package, which automatically enables
the &lt;code&gt;pprof&lt;/code&gt; HTTP endpoints of the webserver we’ve created, and it will install
the handlers under the &lt;code&gt;http://localhost:6060/debug/pprof&lt;/code&gt; URL.&lt;/p&gt;
&lt;p&gt;So we can add the following to our code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// main.go
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;package&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; (
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;fmt&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;math/rand&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;net/http&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;net/http/pprof&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// Add the following
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;go&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ListenAndServe&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;:6060&#34;&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;nil&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// Some sample code
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rand&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Intn&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#a6e22e&#34;&gt;fmt&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Printf&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;Fibonacci sequence for %d:\n&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&lt;=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt;; &lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;++&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#a6e22e&#34;&gt;fmt&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Println&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;i&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt;) &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt; &lt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;		&lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;	&lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;) &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;fibonacci&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;n&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When you are using a different router, we can do the following to make the
&lt;code&gt;pprof&lt;/code&gt; HTTP endpoints work. Below is an example how you would do this if
you’re using &lt;a href=&#34;https://github.com/gorilla/mux&#34;&gt;gorilla/mux&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// main.go
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;package&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; (
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;net/http/pprof&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;expvar&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;github.com/gorilla/mux&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;mux&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;NewRouter&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Index&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/cmdline&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Cmdline&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/profile&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Profile&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/symbol&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Symbol&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/trace&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Trace&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/goroutine&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;goroutine&#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/heap&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;heap&#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/threadcreate&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;threadcreate&#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/block&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;block&#34;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/vars&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;DefaultServeMux&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ListenAndServe&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;:6060&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;docker&#34;&gt;Docker&lt;/h2&gt;
&lt;p&gt;For the docker container we’ll create the following &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# Dockerfile
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;FROM golang:1.13
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;WORKDIR /go/src/app
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;COPY . .
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;RUN go get -d -v ./...
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;RUN go install -v ./...
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;CMD [&#34;app&#34;]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, we can build and start our Docker container:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker build -t pprof-app .
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker run -p 6060:6060 -it --rm --name my-running-app pprof-app
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you’ve updated the files you’ll be able to access the endpoints by using
your browser and to go to
&lt;a href=&#34;http://localhost:8888/tree&#34;&gt;http://localhost:6060/debug/pprof/&lt;/a&gt;, or you can
use &lt;code&gt;go tool pprof&lt;/code&gt;. We’ll explore a few of the methods we can profile the
application using &lt;code&gt;go tool pprof&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;pprof&#34;&gt;pprof&lt;/h2&gt;
&lt;p&gt;Now, that we’ve set everything up to use &lt;code&gt;pprof&lt;/code&gt;, we’ll profile our application
for cpu, and heap. Additionally we’ll also trace the application.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CPU&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When we want to start to profile our application to determine where an
application spends time, we first need to make a CPU profile. And we have
several methods to do so. Below I’ll give the common ones. The gist of it
is that we’re fetching the profile over HTTP, on the endpoints that we’ve
created in our application.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Go to &lt;a href=&#34;http://localhost:6060/debug/pprof/profile&#34;&gt;http://localhost:6060/debug/pprof/profile&lt;/a&gt;, and after 30 seconds
you’ll automatically download the profile, or&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;go tool pprof&lt;/code&gt;. It will tell you where the profile is being stored,
and will open up an interactive terminal that you can use to interpret
the results. Like so: &lt;code&gt;go tool pprof http://localhost:6060/debug/pprof/profile&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After that we can interpret the results, and for this we again have several
options, and tools we can use to interpret the results You can read more about
additional ways to interpret the results at the sites I’ve listed in the
“Sources” section at the end of the post.&lt;/p&gt;
&lt;p&gt;Here I’ll show you a few of them. One of them is that we’re able to inspect
the results in an automatically created web interface.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof -http=&#34;:8080&#34; [path-to-profile-dump]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will open up a webbrowser, in which we can inspect, for instance, the
flamegraph of the created profile.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://bruinsslot.jp/img/golang-cpu-profile.png&#34; alt=&#34;golang-cpu-profile&#34;&gt;&lt;/p&gt;
&lt;p&gt;The horizontal axis represents the total number of samples collected. So the
larger the area, the more time has been spent executing the associated
function. The vertical axis represents the depth of the call stack. So the
higher the peak, the deeper the call stack. Colors don’t represent anything
specific; they’re just there to make a visual contrast.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Another method is that we can use the command line based interactive interface
by using the &lt;code&gt;go tool&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof [path-to-profile-dump]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can also instantly go to the web interface, and step over the whole download
profile step, by using the following command:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof -http=&#34;:8080&#34; http://localhost:6060/debug/pprof/profile
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One thing to remember is: if you’re making cpu profiles that takes a certain
amount of time you need to specify that your server is able to handle those
long request times. You typically want to add the following to your
&lt;code&gt;http.Server&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-golang&#34; data-lang=&#34;golang&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;srv&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&amp;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Server&lt;/span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;WriteTimeout&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;30&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;time&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Second&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;heap&#34;&gt;Heap&lt;/h3&gt;
&lt;p&gt;In order to get a heap profile, so that we can inspect the
memory usage of the application, we again can issue the following commands:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Go to &lt;a href=&#34;http://localhost:6060/debug/pprof/heap&#34;&gt;http://localhost:6060/debug/pprof/heap&lt;/a&gt;, and we will automatically
download the heap profile, or&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using the &lt;code&gt;go tool&lt;/code&gt;, it will tell you were the profile is being stored, and
it opens up an interactive terminal that you can use to interpret the
results:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# Heap profile
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;#
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# optional flags:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;#   –alloc_space tells you how many megabytes have been allocated.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;#   –inuse_space tells you know how many are still in use.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof http://localhost:6060/debug/pprof/heap
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And, just like with the CPU profile, we’re able to interpret it with &lt;code&gt;go tool pprof&lt;/code&gt;, and the web interface:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# Go tool
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof [path-to-profile-dump]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# Web interface
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof -http=&#34;:8080&#34; [path-to-profile-dump]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# Create profile and start web interface
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof -http=&#34;:8080&#34; http://localhost:6060/debug/pprof/heap
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img src=&#34;https://bruinsslot.jp/img/golang-heap-profile.png&#34; alt=&#34;golang-heap-profile&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;trace&#34;&gt;Trace&lt;/h3&gt;
&lt;p&gt;To trace the runtime activities/events of an application, we can do the
following. First, we need to download the trace file, and we can use curl for
this. Specify for how many seconds you want to create a trace and make sure to
create some load in that time frame. Once downloaded you can use the &lt;code&gt;go tool&lt;/code&gt;
to start up a web interface in which you can inspect the results.&lt;/p&gt;
&lt;p&gt;The trace tool will capture execution events like the creation, blocking,
unblocking of goroutines, garbage collection events, processor starting and
stopping, enter, exit, and block syscalls.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Issue the following commands:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ curl http://localhost:6060/debug/pprof/trace?seconds=20 &gt; trace.out
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool trace trace.out
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will open up your webbrowser, and you’ll be able to interpret the results
of your trace with the provided web interface.&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We’ve explored some methods that allows us to profile our Go applications when
it is run in a Docker container. There’s a lot more information out there,
that can help you optimize your applications using these methods. I’ve listed
several of those below in the “Sources” section. Be sure to check them out they
have some great additional information that can help you.&lt;/p&gt;
&lt;h2 id=&#34;sources&#34;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://golang.org/doc/diagnostics.html&#34;&gt;golang.org: Diagnostics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://golang.org/pkg/runtime/pprof/&#34;&gt;golang.org: Package runtime/pprof&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://golang.org/pkg/net/http/pprof/&#34;&gt;golang.org: Package net/http/pprof&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://blog.golang.org/profiling-go-programs&#34;&gt;golang.org: Profiling go programs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/google/pprof/blob/master/doc/README.md&#34;&gt;github.com: Go tool pprof&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://jvns.ca/blog/2017/09/24/profiling-go-with-pprof/&#34;&gt;Julia Evans: Profiling Go with pprof&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://gist.github.com/Jimmy-Xu/85fb01cd7620454c6d65&#34;&gt;Jimmy-Xu: pprof in docker daemon&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://about.sourcegraph.com/go/an-introduction-to-go-tool-trace-rhys-hiltner&#34;&gt;Rhys Hiltner: An introduction to go tool trace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://youtu.be/mmqDlbWk_XA&#34;&gt;Rhys Hiltner: Go’s execution tracer (youtube)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://blog.ralch.com/tutorial/golang-performance-and-memory-analysis/&#34;&gt;Svet Ralchev: Performance and memory analysis of Golang programs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/bradfitz/talk-yapc-asia-2015/blob/master/talk.md&#34;&gt;Brad Fritzpatrick: Profiling &amp; Optimizing in Go&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://making.pusher.com/go-tool-trace/&#34;&gt;Will Sewell: go tool trace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;&lt;a href=&#34;http://www.brendangregg.com/flamegraphs.html&#34;&gt;http://www.brendangregg.com/flamegraphs.html&lt;/a&gt; &lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://golang.org/doc/diagnostics.html&#34;&gt;https://golang.org/doc/diagnostics.html&lt;/a&gt; &lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>How to profile Python applications inside a docker container</title>
      <link>https://bruinsslot.jp/articles/profiling-python-docker/</link>
      <pubDate>Thu, 21 Nov 2019 12:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/profiling-python-docker/</guid>
      <description>&lt;p&gt;In the following post I’ll explain how you can profile a running Python program
in a Docker container using &lt;a href=&#34;https://github.com/benfred/py-spy&#34;&gt;py-spy&lt;/a&gt;. &lt;code&gt;py-spy&lt;/code&gt;
is able to generate &lt;a href=&#34;http://www.brendangregg.com/flamegraphs.html&#34;&gt;flame graphs&lt;/a&gt;,
and it can give us profiling capabilities in order to debug our Python programs.
Now, when your Python code is running in a Docker container it can be a bit
more difficult to profile your code, and this post sets out to show you how
this can be done with these tools.&lt;/p&gt;
&lt;h1 id=&#34;1-setting-up&#34;&gt;1. Setting up&lt;/h1&gt;
&lt;p&gt;Before we can profile the Python application, we need to set up the tools, and
example files, that we will be using in this project. You can inspect the
finished result &lt;a href=&#34;https://github.com/erroneousboat/profile-python-docker&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;11-project&#34;&gt;1.1 Project&lt;/h2&gt;
&lt;p&gt;Let’s get started with the project outline, and create the following files and
folders, the &lt;code&gt;docker-compose.yml&lt;/code&gt; is optional because I will show you how to
use plain &lt;code&gt;docker&lt;/code&gt; as well.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tree -L 1 --dirsfirst
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── app/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── pyspy/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;└── docker-compose.yml
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;12-py-spy&#34;&gt;1.2 py-spy&lt;/h2&gt;
&lt;p&gt;In the &lt;code&gt;pyspy/&lt;/code&gt; folder we will be creating a &lt;code&gt;Dockerfile&lt;/code&gt; that we’re going to
use to create a Docker container that contains the &lt;code&gt;py-spy&lt;/code&gt; program. The
&lt;code&gt;Dockerfile&lt;/code&gt; should contain the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# pyspy/Dockerfile
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;FROM python:3.6
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;RUN pip install py-spy
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;WORKDIR /profiles
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ENTRYPOINT [ &#34;py-spy&#34; ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;CMD []
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let’s test if is is working:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ cd pyspy/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker build -t pyspy .
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker run -it pyspy
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;py-spy 0.3.0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Sampling profiler for Python programs
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;USAGE:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    py-spy &lt;SUBCOMMAND&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;OPTIONS:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    -h, --help       Prints help information
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    -V, --version    Prints version information
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;SUBCOMMANDS:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    record    Records stack trace information to a flamegraph, speedscope
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              or raw file
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    top       Displays a top like view of functions consuming CPU
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    dump      Dumps stack traces for a target program to stdout
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    help      Prints this message or the help of the given subcommand(s)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Cool, that worked! Now, you’re also able to install &lt;code&gt;py-spy&lt;/code&gt; locally on your
host system. When you do, be sure to read the documentation at:
&lt;a href=&#34;https://github.com/benfred/py-spy&#34;&gt;https://github.com/benfred/py-spy&lt;/a&gt;, on how to do this.&lt;/p&gt;
&lt;h2 id=&#34;13-python&#34;&gt;1.3 Python&lt;/h2&gt;
&lt;p&gt;Next, we want to create a Python program that we will use to profile. Create
a new file in the &lt;code&gt;app/&lt;/code&gt; folder name &lt;code&gt;run.py&lt;/code&gt; and copy the contents into it.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# app/run.py&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; random
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;factorial&lt;/span&gt;(n):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    factorial &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, n &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        factorial &lt;span style=&#34;color:#f92672&#34;&gt;*=&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; factorial
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; __name__ &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;__main__&#34;&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        n &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; random&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;choice(range(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        f &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; factorial(n&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;n)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        print(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;Factorial of &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{n}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; is &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{f}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;format(n&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;n, f&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;f))
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, create a &lt;code&gt;Dockerfile&lt;/code&gt; in the &lt;code&gt;app/&lt;/code&gt; folder:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# app/Dockerfile
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;FROM python:3.6
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;WORKDIR /usr/src/app
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;COPY run.py .
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;CMD [ &#34;python&#34;, &#34;./run.py&#34; ]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And again, let’s see if it is working:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ cd app/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker build -t app .
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker run -t app
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Press &lt;code&gt;Control-C&lt;/code&gt; to exit the program.&lt;/p&gt;
&lt;h2 id=&#34;14-docker-compose&#34;&gt;1.4 Docker Compose&lt;/h2&gt;
&lt;p&gt;I promised that I’ll show you how to also use Docker Compose to orchestrate our
containers. Create &lt;code&gt;docker-compose.yml&lt;/code&gt; file and add the following to it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# docker-compose.yml&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;version&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;3&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;pyspy&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;build&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;context&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;pyspy/&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;pid&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;host&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;privileged&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;true&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;volumes&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;.:/profiles&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;app&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;build&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;context&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;app/&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;cap_add&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;sys_ptrace&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, we can build the containers like so:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose build
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You’ll probably wondering about what the &lt;code&gt;pid&lt;/code&gt;, &lt;code&gt;privileged&lt;/code&gt; and &lt;code&gt;cap_add&lt;/code&gt; is
used for, and I’ll get to that in the next section.&lt;/p&gt;
&lt;h1 id=&#34;2-profiling-the-program&#34;&gt;2. Profiling the program&lt;/h1&gt;
&lt;p&gt;We’ve set everything up and we’re ready to test everything. First, let’s run
the python program. We need to add &lt;code&gt;--cap-add sys_ptrace&lt;/code&gt;, because by default
Docker images do not have the &lt;code&gt;SYS_PTRACE&lt;/code&gt; capability. And this is because
Docker is restricting the &lt;code&gt;process_vm_readv&lt;/code&gt; system call that &lt;code&gt;py-spy&lt;/code&gt; uses to
directly read the memory of the Python program.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# docker
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker run --cap-add sys_ptrace -t app --name py-app
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# docker-compose
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose up app
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, before we’re able to use the &lt;code&gt;py-spy&lt;/code&gt; program, we need to get the &lt;code&gt;PID&lt;/code&gt; of
the Python program that is running in the container. We will be using the &lt;code&gt;PID&lt;/code&gt;
of the program, and &lt;code&gt;py-spy&lt;/code&gt; is going to sample from it.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# docker
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker inspect --format &#39;{{.State.Pid}}&#39; py-app
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;26982
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# docker-compose
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker inspect --format &#39;{{.State.Pid}}&#39; profile-python-docker_app_1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;26982
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, that we’ve got the &lt;code&gt;PID&lt;/code&gt; we will be using it with &lt;code&gt;py-spy&lt;/code&gt; to profile
our Python application. Now that we’re using the &lt;code&gt;PID&lt;/code&gt; of the running program
in the Docker container. We need the &lt;code&gt;py-spy&lt;/code&gt; container to be able to use
this &lt;code&gt;PID&lt;/code&gt; inside it’s container. So we to use the host’s &lt;code&gt;PID&lt;/code&gt; namespace
inside this container, and we do that by adding the flag &lt;code&gt;--pid=host&lt;/code&gt; to
the run command. Additionally, we need to add the &lt;code&gt;--privileged&lt;/code&gt; flag,
so that it allows the container the same access to the host as processes
running outside containers on the host.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# docker: change $(pwd) where you want the profiles to be saved
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker run \
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    --pid=host \
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    --privileged \
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    -v $(pwd):/profiles \
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    -it pyspy record -o myprofile.svg --pid 26982
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# docker-compose
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose run pyspy record -o myprofile.svg --pid 26982
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can now open the &lt;code&gt;myprofile.svg&lt;/code&gt; file with a browser and look at the
flamegraph that was created, and you’ll be able to interpret the results.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://bruinsslot.jp/img/myprofile.png&#34; alt=&#34;myprofile&#34;&gt;&lt;/p&gt;
&lt;p&gt;The horizontal axis represents the total number of samples collected. So the
larger the area, the more time has been spent executing the associated
function. The vertical axis represents the depth of the call stack. So the
higher the peak, the deeper the call stack. Colors don’t represent anything
specific; they’re just there to make a visual contrast. (&lt;a href=&#34;http://www.brendangregg.com/flamegraphs.html&#34;&gt;source&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;A cool feature of &lt;code&gt;py-spy&lt;/code&gt; is that we can also create our profiles in the
&lt;a href=&#34;https://www.speedscope.app/&#34;&gt;speedscope&lt;/a&gt; and inspect it with the online tool.
Add the &lt;code&gt;--format speedscope&lt;/code&gt; flag to the command and you’ll be able to import
the profile in the speedscope format.&lt;/p&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Now, you’ve got a basic setup to profile your Python applications in a Docker
container. So, be sure to read up on all the features and options of
&lt;a href=&#34;https://github.com/benfred/py-spy&#34;&gt;&lt;code&gt;py-spy&lt;/code&gt;&lt;/a&gt; so that you can fully utilize it
and, profile your Python applications in Docker containers.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>A minimal REST API Django setup as a microservices</title>
      <link>https://bruinsslot.jp/articles/minimal-django/</link>
      <pubDate>Tue, 12 Nov 2019 12:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/minimal-django/</guid>
      <description>&lt;p&gt;In this post I will set out on how to set up a django project that can be
used as a REST API microservice. To see the end result, you can investigate
the code &lt;a href=&#34;https://github.com/erroneousboat/minimal-django&#34;&gt;here&lt;/a&gt;. An important
disclaimer: the code presented here is to be used in a development environment,
review security practices for the framework(s) when you want to use it in
production or expose it to the internet.&lt;/p&gt;
&lt;h2 id=&#34;1-project-structure&#34;&gt;1. Project structure&lt;/h2&gt;
&lt;p&gt;We will using the following stack:&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Name&lt;/th&gt;
          &lt;th&gt;Version&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;Docker&lt;/td&gt;
          &lt;td&gt;18.03.1-ce&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Python&lt;/td&gt;
          &lt;td&gt;3.8&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Django&lt;/td&gt;
          &lt;td&gt;2.2.7&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;PostgreSQL&lt;/td&gt;
          &lt;td&gt;12.0&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Gunicorn&lt;/td&gt;
          &lt;td&gt;20.0.0&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;DRF&lt;/td&gt;
          &lt;td&gt;3.10.3&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Let’s first start with the project structure and create some files and
folders. Create the following files and folders:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tree -L &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; --dirsfirst
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── config/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── docker-compose.yml
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── Dockerfile
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;└── README.md
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;First update our &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ cat Dockerfile
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;FROM python:3.8
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;COPY . /srv/minimal-django
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;RUN pip3 install -r /srv/minimal-django/config/requirements.txt
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;CMD &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;gunicorn&#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;--config&#34;&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/srv/minimal-django/config/gunicorn.py&#34;&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;minimal_django.wsgi&#34;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We will use a Python base image, and of course you can choose your own
base image as you like. In the Dockerfile we’ll define that we need to copy
the code, and install the Python package requirements. Lastly, we use Gunicorn
as the WSGI application server.&lt;/p&gt;
&lt;p&gt;Now, for convenience sake we’re using &lt;code&gt;docker-compose&lt;/code&gt; to orchestrate our
docker containers. So we will add the following contents to our
&lt;code&gt;docker-compose.yml&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ cat docker-compose.yml
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;version: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;3&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;volumes:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  postgres:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    driver: local
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;services:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    postgres:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      image: postgres:12.0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      environment:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - POSTGRES_NAME&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;postgres
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - POSTGRES_USER&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;postgres
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - POSTGRES_PASSWORD&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;mysecretpassword
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - PGDATA&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;pgdata
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      volumes:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - postgres:/var/lib/postgresql/data/pgdata
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    minimal_django:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      build:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        context: .
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      ports:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;4001:4001&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      volumes:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - .:/srv/minimal_django
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      depends_on:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        - postgres
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We will be using PostgreSQL as our datastore in this example, and we will
calling our Django application &lt;code&gt;minimal_django&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next, we need to add the configuration files in the folder &lt;code&gt;config/&lt;/code&gt;, and we
will add the following files:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ls config/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gunicorn.py  requirements.txt
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But first, let’s add the python packages we will using in the Django
application, and we will be adding those in the &lt;code&gt;requirements.txt&lt;/code&gt; file.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ cat config/requirements.txt
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;django&lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt;2.2.7
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gunicorn&lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt;19.9.0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;psycopg2-binary&lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt;2.8.4
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;djangorestframework&lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt;3.10.3
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see we will be using &lt;a href=&#34;https://www.django-rest-framework.org&#34;&gt;Django REST Framework&lt;/a&gt;
for the REST API. Next up we will create the Django project.&lt;/p&gt;
&lt;h2 id=&#34;2-create-the-django-project&#34;&gt;2. Create the Django project&lt;/h2&gt;
&lt;p&gt;Now, that we have the basic structure set up. We need to create an actual
Django project, and we will be using the Docker container for this. This
way we don’t necessarily install Django on our host system.&lt;/p&gt;
&lt;p&gt;First, build the &lt;code&gt;minimal_django&lt;/code&gt; container.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose build minimal_django
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Second, we will start an interactive shell in the &lt;code&gt;minimal_django&lt;/code&gt; container.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose run minimal_django sh
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Third, we will create the Django project by executing the following command
in the attached shell:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# cd /srv/minimal-django&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# django-admin startproject minimal_django&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We’ve created the project ‘inside’ the container, and because we’ve
attached a volume to the location of the files. So now we need to fix the file
permissions on our host system, because they have been created as the root
account. So, exit from the shell in the docker container, and change the owner
of the folder that we’ve just created.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ chown -R $USER:$USER minimal_django/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;3-update-settingspy&#34;&gt;3. Update &lt;code&gt;settings.py&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Because this will be a minimal Django implementation we will updating the
&lt;code&gt;settings.py&lt;/code&gt; file, and remove the non-essential elements.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Update &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ALLOWED_HOSTS &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;*&#34;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Update &lt;code&gt;INSTALLED_APPS&lt;/code&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;INSTALLED_APPS &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;rest_framework&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;api&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Remove &lt;code&gt;MIDDLEWARE&lt;/code&gt;, and its contents&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Remove &lt;code&gt;TEMPLATES&lt;/code&gt;, and its contents&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Update the &lt;code&gt;DATABASES&lt;/code&gt; dict&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;DATABASES &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;default&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;ENGINE&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;django.db.backends.postgresql_psycopg2&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;NAME&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;postgres&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;USER&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;postgres&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;PASSWORD&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;mysecretpassword&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;HOST&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;postgres&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;PORT&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;5432&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Remove &lt;code&gt;AUTH_PASSWORD_VALIDATORS&lt;/code&gt;, and its contents&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Remove &lt;code&gt;STATIC_URL&lt;/code&gt;, and its contents&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Remove &lt;code&gt;LANGUAGE_CODE&lt;/code&gt;, &lt;code&gt;USE_I18N&lt;/code&gt;, &lt;code&gt;USE_L10N&lt;/code&gt;, and its contents&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the following Django REST Framework configuration&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;REST_FRAMEWORK &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;DEFAULT_RENDERER_CLASSES&#34;&lt;/span&gt;: [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;rest_framework.renderers.JSONRenderer&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;DEFAULT_AUTHENTICATION_CLASSES&#34;&lt;/span&gt;: [],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;DEFAULT_PERMISSION_CLASSES&#34;&lt;/span&gt;: [],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;UNAUTHENTICATED_USER&#34;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;PAGE_SIZE&#39;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;DEFAULT_PAGINATION_CLASS&#39;&lt;/span&gt;: \
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;rest_framework.pagination.LimitOffsetPagination&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;DEFAULT_FILTER_BACKENDS&#39;&lt;/span&gt;: [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;rest_framework.filters.OrderingFilter&#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Adding logging configuration, this is optional and you can edit/update it
to your preferences.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;LOGGING &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;version&#39;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;disable_existing_loggers&#39;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;formatters&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;verbose&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;format&#39;&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%(pathname)s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%(lineno)d&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; (&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%(funcName)s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;) &#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%(levelname)s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%(message)s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;simple&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;format&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%(levelname)s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;%(message)s&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;handlers&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;null&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;level&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;DEBUG&#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;class&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;logging.NullHandler&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;stderr&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;level&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;DEBUG&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;formatter&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;simple&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;class&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;logging.StreamHandler&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;loggers&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;django&#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;handlers&#39;&lt;/span&gt;: [&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;stderr&#39;&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;propagate&#39;&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;level&#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#39;WARNING&#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Again, this is not a production ready configuration!&lt;/p&gt;
&lt;h2 id=&#34;4-configuring-the-django-api-app&#34;&gt;4. Configuring the Django &lt;code&gt;api&lt;/code&gt; app&lt;/h2&gt;
&lt;p&gt;Now we are ready to create the Django app. We will call it &lt;code&gt;api&lt;/code&gt;, and within
it we will create a simple REST API. First, create a folder in which it will
be contained.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ mkdir minimal_django/api/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Inside this folder create the following files:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ls minimal_django/api/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;__init__.py  models.py  serializers.py  urls.py  views.py
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We will start with the file &lt;code&gt;models.py&lt;/code&gt;, and we will create a very simple
model.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# minimal_django/api/models.py&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; django.db &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; models
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Pet&lt;/span&gt;(models&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;Model):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    name &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; models&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;CharField(max_length&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;64&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, update the &lt;code&gt;views.py&lt;/code&gt; file where we will add the Django Rest Framework
ViewSet.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# minimal_django/api/views.py&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; rest_framework &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; viewsets
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; api.models &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Pet
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; api.serializers &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; PetSerializer
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;PetViewSet&lt;/span&gt;(viewsets&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ModelViewSet):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    queryset &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Pet&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;objects&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;all()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    serializer_class &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; PetSerializer
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Create and update the &lt;code&gt;serializers.py&lt;/code&gt; file that will be used in our application.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# minimal_django/api/serializers.py&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; rest_framework &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; serializers
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; api.models &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Pet
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;PetSerializer&lt;/span&gt;(serializers&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ModelSerializer):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Meta&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Pet
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        fields &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;name&#39;&lt;/span&gt;, )
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Update the &lt;code&gt;urls.py&lt;/code&gt; file, and we will use the Django Rest Framework default
routers.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# minimal_django/api/urls.py&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; django.conf.urls &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; include, url
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; rest_framework.routers &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; DefaultRouter
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; api &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; views
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;router &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; DefaultRouter()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;router&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;register(&lt;span style=&#34;color:#e6db74&#34;&gt;r&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;pets&#39;&lt;/span&gt;, views&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;PetViewSet)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;urlpatterns &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [url(&lt;span style=&#34;color:#e6db74&#34;&gt;r&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;^&#39;&lt;/span&gt;, include(router&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;urls))]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Also update the &lt;code&gt;urls.py&lt;/code&gt; file, of the Django project itself and remove the
paths to the admin environment.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# minimal_django/urls.py&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; django.conf.urls &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; url, include
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;urlpatterns &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    url(&lt;span style=&#34;color:#e6db74&#34;&gt;r&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;^&#39;&lt;/span&gt;, include(&lt;span style=&#34;color:#e6db74&#34;&gt;&#39;api.urls&#39;&lt;/span&gt;)),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;5-configuring-gunicorn&#34;&gt;5. Configuring Gunicorn&lt;/h2&gt;
&lt;p&gt;We will be using &lt;a href=&#34;https://gunicorn.org/&#34;&gt;Gunicorn&lt;/a&gt; as the WSGI application
server. Create and update the &lt;code&gt;gunicorn.py&lt;/code&gt; file with the following content.
Refer to the &lt;a href=&#34;http://docs.gunicorn.org/en/latest/settings.html&#34;&gt;configuration documentation&lt;/a&gt;
for more configuration options.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# config/gunicorn.py&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Documentation at: http://docs.gunicorn.org/en/latest/index.html&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;django_project_name &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;minimal_django&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Chdir to specified directory before apps loading&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;chdir &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/srv/minimal-django/minimal_django&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# The socket to bind to&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;bind &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;:4001&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# The class of worker processes for handling requests&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;worker_class &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;sync&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Is a number of OS processes for handling requests&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;workers &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Is a maximum count of active greenlets grouped in a pool that will be&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# allowed in each process&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;worker_connections &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# The maximum number of requests a worker will process before restarting&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;max_requests &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;5000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Workers silent for more than this many seconds are killed and restarted&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;timeout &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;120&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Set environment variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;raw_env &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; \
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    [&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;DJANGO_SETTINGS_MODULE=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.settings&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;format(django_project_name)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# The access log file to write to, &#34;-&#34; means to stderr&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;accesslog &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;-&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# The error log file to write to, &#34;-&#34; means to stderr&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;errorlog &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;-&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;6-start-it-up&#34;&gt;6. Start it up&lt;/h2&gt;
&lt;p&gt;Before we can run the application, we need to make initial migrations and
propagate the model definition into our database schema. We will be using
our Docker container to create those migrations, and again as a result we
need to change the folder permissions in the end.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# First start the postgresql container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose up -d postgres
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Create the migrations&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose run minimal_django &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    /srv/minimal-django/minimal_django/manage.py &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    makemigrations api
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Actually migrate the schema&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose run minimal_django &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    /srv/minimal-django/minimal_django/manage.py &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    migrate
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Reset the folder permissions of the created migrations folder&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ chown -R $USER:$USER minimal_django
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With that done we’re able to run our application, since we already started
our postgresql container we will able to execute the following command.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose up minimal_django
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, the application should be running and you’ll be able to access the API
with &lt;code&gt;curl&lt;/code&gt; or any other method.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ curl http://localhost:4001/pets/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>How to profile Go applications inside a docker container</title>
      <link>https://bruinsslot.jp/articles/profiling-golang-docker/</link>
      <pubDate>Fri, 11 Aug 2017 12:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/profiling-golang-docker/</guid>
      <description>&lt;p&gt;&lt;em&gt;This article has been updated and you can view its updated version
&lt;a href=&#34;https://bruinsslot.jp/articles/profiling-golang-docker-2/&#34;&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this post I’ll give a quick overview of several methods you can use for
profiling/debugging Go applications that are running in a docker
container. To get a more in-depth overview of the several methods,
I’ve added the source links you can reference.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#pprof&#34;&gt;pprof&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#gcvis&#34;&gt;gcvis&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#gotorch&#34;&gt;gotorch&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;general-sources&#34;&gt;general sources&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://deferpanic.com/blog/understanding-golang-memory-usage/&#34;&gt;Defer Panic - understanding golang memory usage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://blog.golang.org/profiling-go-programs&#34;&gt;golang.org - profiling go programs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://gist.github.com/Jimmy-Xu/85fb01cd7620454c6d65&#34;&gt;Jimmy-Xu - pprof in docker daemon&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;pprof&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;pprof&#34;&gt;pprof&lt;/h3&gt;
&lt;p&gt;When you want to use &lt;code&gt;pprof&lt;/code&gt; to profile your Go applications, that are running in
a container, we need to make sure we turn on the internal &lt;code&gt;pprof&lt;/code&gt; HTTP endpoints.
We’ll do this by updating the source code of your application. What follows is
an example of how you would enable those endpoints.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-golang&#34; data-lang=&#34;golang&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// main.go
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; (
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;net/http/pprof&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;_&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;expvar&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;github.com/gorilla/mux&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// OPTION 1: Add this to your application
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;go&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;func&lt;/span&gt;() {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ListenAndServe&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;0.0.0.0:6060&#34;&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;nil&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;// OPTION 2: When using gorilla mux you can do the following:
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;mux&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;NewRouter&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Index&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/cmdline&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Cmdline&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/profile&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Profile&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/symbol&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Symbol&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;HandleFunc&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/trace&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Trace&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/goroutine&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;goroutine&#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/heap&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;heap&#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/threadcreate&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;threadcreate&#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/pprof/block&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;pprof&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handler&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;block&#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Handle&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;/debug/vars&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;DefaultServeMux&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;ListenAndServe&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;0.0.0.0:6060&#34;&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;router&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, we need to make sure that we’re able to access those endpoints. When
you’re using docker-compose update your &lt;code&gt;docker-compose.yaml&lt;/code&gt; file as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;// docker-compose.yaml&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;version&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;3&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;app&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;build&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;./build-dir&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;ports&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;6060:6060&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you’ve updated the files you’ll be able to access the endpoints by using
your browser and to go to
&lt;a href=&#34;http://localhost:8888/tree&#34;&gt;http://localhost:6060/debug/pprof/&lt;/a&gt;,
or you can use &lt;code&gt;go tool pprof&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Heap profile&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# optional flags:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#   –alloc_space tells you how many megabytes have been allocated.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#   –inuse_space tells you know how many are still in use.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof http://localhost:6060/debug/pprof/heap
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# CPU profile&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof http://localhost:6060/debug/pprof/profile
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Using trace tool&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ curl http://localhost:6060/debug/pprof/trace?seconds&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt; &gt; trace.out
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool trace trace.out
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Create a heap dump and analyze it locally&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ curl http://localhost:6060/debug/pprof/heap &gt; heap.out
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go tool pprof -runtime --inuse_space &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;your-binary-here&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; heap.out
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One thing to remember is: if you’re making cpu profiles that takes a certain
amount of time you need to specify that your server is able to handle those
long request times. You typically want to add the following to your
&lt;code&gt;http.Server&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-golang&#34; data-lang=&#34;golang&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;srv&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&amp;&lt;/span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;http&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Server&lt;/span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;WriteTimeout&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;30&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;time&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;Second&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;sources&#34;&gt;sources&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://golang.org/pkg/net/http/pprof/&#34;&gt;Go - Package profile&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://blog.golang.org/profiling-go-programs&#34;&gt;Go Blog - Profiling Go Programs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://blog.ralch.com/tutorial/golang-performance-and-memory-analysis/&#34;&gt;Svet Ralchev - Performance and memory analysis of Golang programs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://stackoverflow.com/q/19591065&#34;&gt;Stack Overflow - Profiling Go web application built with Gorilla’s mux with net/http/pprof
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/bradfitz/talk-yapc-asia-2015/blob/master/talk.md&#34;&gt;Brad Fritzpatrick - Profiling &amp; Optimizing in Go&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://making.pusher.com/go-tool-trace/&#34;&gt;Pusher - go tool trace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://youtu.be/Xq5HDH8y0CE&#34;&gt;Pusher - Introduction to go tool trace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://youtu.be/mmqDlbWk_XA&#34;&gt;Rhys Hiltner - Go’s execution tracer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://deferpanic.com/blog/goroutine-tracing/&#34;&gt;Defer Panic - Goroutine Tracing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://medium.com/square-corner-blog/always-be-closing-3df5da0e00da&#34;&gt;Alec Holmes - always be closing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;gcvis&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;gcvis&#34;&gt;gcvis&lt;/h3&gt;
&lt;p&gt;To use gcvis to profile our applications we need to first install it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go get -u github.com/davecheney/gcvis
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now update your &lt;code&gt;docker-compose.yaml&lt;/code&gt; file, important is to set the &lt;code&gt;GODEBUG&lt;/code&gt;
environment variable:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;// docker-compose.yaml&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;version&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;3&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;app&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;build&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;./build-dir&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;environment&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    - &lt;span style=&#34;color:#ae81ff&#34;&gt;GODEBUG=gctrace=1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you’ve updated the &lt;code&gt;docker-compose.yaml&lt;/code&gt; file, run your container with the
command below, and gcvis will start a browser for you automatically.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ docker-compose up app | gcvis
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;sources-1&#34;&gt;sources&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/davecheney/gcvis&#34;&gt;Github - gcvis&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://dave.cheney.net/2014/07/11/visualising-the-go-garbage-collector&#34;&gt;Dave Cheney - Visualising the Go garbage collector&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;gotorch&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;go-torch&#34;&gt;go-torch&lt;/h3&gt;
&lt;p&gt;When using go-torch, first update your code to enable the pprof endpoints.
Reference the &lt;a href=&#34;#pprof&#34;&gt;&lt;code&gt;pprof&lt;/code&gt;&lt;/a&gt; section on how to do that. Next, you need to
get go-torch:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go get -u github.com/uber/go-torch
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ git clone git@github.com:brendangregg/FlameGraph.git
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ export PATH&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;$PATH:/path/to/FlameGraph
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And you can run it as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go-torch -u http://localhost:6060/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;sources-2&#34;&gt;sources&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/uber/go-torch&#34;&gt;Github - go-torch&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>How to restore a PostgreSQL database on Kubernetes</title>
      <link>https://bruinsslot.jp/articles/restore-postgres-database-kubernetes/</link>
      <pubDate>Sat, 06 Aug 2016 12:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/restore-postgres-database-kubernetes/</guid>
      <description>&lt;p&gt;As I wanted to transfer the database from one kubernetes cluster to a new one,
I had to find a way to do this. And with a bit of trial and error I succeeded.
So for future reference, and the hope that this might help someone, I figured
I would write it down.&lt;/p&gt;
&lt;p&gt;First we will transfer the database to our file system:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# pod-name         name of the postgres pod&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# postgres-user    database user that is able to access the database&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# database-name    name of the database&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;kubectl exec &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;pod-name&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; -- &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    bash -c &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;pg_dump -U [postgres-user] [database-name]&#34;&lt;/span&gt; &gt; database.sql
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will download the specified database on your file system as &lt;code&gt;database.sql&lt;/code&gt;.
Now the following command will restore the database:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# pod-name         name of the postgres pod&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# postgres-user    database user that is able to access the database&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# database-name    name of the database&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;cat database.sql | kubectl exec -i &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;pod-name&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; -- &lt;span style=&#34;color:#ae81ff&#34;&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;&lt;/span&gt;    psql -U &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;postgres-user&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; -d &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;database-name&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Sources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/kubernetes/kubernetes/issues/13776&#34;&gt;https://github.com/kubernetes/kubernetes/issues/13776&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.postgresql.org/docs/8.0/static/backup.html#BACKUP-DUMP-RESTORE&#34;&gt;https://www.postgresql.org/docs/8.0/static/backup.html#BACKUP-DUMP-RESTORE&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Create command line applications with a config file</title>
      <link>https://bruinsslot.jp/articles/create-command-line-applications-with-a-config-file/</link>
      <pubDate>Fri, 10 Jun 2016 12:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/create-command-line-applications-with-a-config-file/</guid>
      <description>&lt;p&gt;In this tutorial we’ll be using &lt;a href=&#34;https://github.com/erroneouboat/engage&#34;&gt;engage&lt;/a&gt;
to create a cli application by only using a json config file. Enage was created
because at &lt;a href=&#34;https://fuelup.co&#34;&gt;FuelUp&lt;/a&gt; we have several bash scripts placed in
different folders in the project. I wanted a way to bundle them so that
I didn’t have to remember the location of these scripts. Additionally when
developing it isn’t uncommon to have to type several commands after another,
and it would be nice to have them easy accessible. And I really wanted to have
the command &lt;code&gt;fuel up&lt;/code&gt; to start the platform from the commandline. :)&lt;/p&gt;
&lt;p&gt;So, the solution was to create a cli application and I wanted to do this in
&lt;a href=&#34;http://golang.org&#34;&gt;Go&lt;/a&gt;. However, adding new commands to the application would
require co-workers to know Go, update the code and recreate the binaries. I
wanted something simpler, just a json file that was easy to edit and would
reference the scripts and commands, that would make up the cli application.&lt;/p&gt;
&lt;p&gt;This is where ’engage’ comes in, and in the following sections I’ll explain how
to install, configure and use it. So that you’ll be able to create your own
cli application.&lt;/p&gt;
&lt;p&gt;In short, it’ll make this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;our first engage app&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;authors&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;William T. Riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;email&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;w.t.riker@starfleet.org&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;version&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;1.0&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;commands&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;say&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;action&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;echo&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;use `say` to relay a message&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Into this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ riker
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;NAME:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   riker - our first enage app
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;USAGE:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   riker &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;global options&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; command &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;command options&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;arguments...&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;VERSION:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   1.0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;AUTHOR&lt;span style=&#34;color:#f92672&#34;&gt;(&lt;/span&gt;S&lt;span style=&#34;color:#f92672&#34;&gt;)&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   William T. Riker &lt;w.t.riker@starfleet.org&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;COMMANDS:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    say use &lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;say&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt; to relay a message
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;GLOBAL OPTIONS:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   --help, -h           show help
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   --version, -v        print the version
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;installation&#34;&gt;Installation&lt;/h3&gt;
&lt;p&gt;First make sure you have &lt;a href=&#34;https://golang.org/dl/&#34;&gt;the Go Programming Language&lt;/a&gt;
installed on your system. Then clone the ’engage’ project on your system.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ git clone git@github.com:erroneousboat/engage.git
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now think of a name you want to give your new cli application. In this example
we’ll use &lt;code&gt;riker&lt;/code&gt;. Move to the project folder and execute the following
command:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ make APP_NAME&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;riker
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will place your new application in the &lt;code&gt;bin/&lt;/code&gt; folder. Now lets create
our config file and some commands.&lt;/p&gt;
&lt;h3 id=&#34;configuration&#34;&gt;Configuration&lt;/h3&gt;
&lt;p&gt;Create a &lt;code&gt;engage.json&lt;/code&gt; file next to your application and add the following
lines to it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;our first engage app&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;authors&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;William T. Riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;email&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;w.t.riker@starfleet.org&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;version&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;1.0&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;commands&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;say&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;action&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;echo&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;use `say` to relay a message&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, let’s test it out. Direct yourself to the &lt;code&gt;bin/&lt;/code&gt; folder, which should now
contain the &lt;code&gt;riker&lt;/code&gt; binary and the &lt;code&gt;engage.json&lt;/code&gt; file.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ./riker
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;NAME:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   riker - our first enage app
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;USAGE:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   riker &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;global options&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; command &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;command options&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;arguments...&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;VERSION:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   1.0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;AUTHOR&lt;span style=&#34;color:#f92672&#34;&gt;(&lt;/span&gt;S&lt;span style=&#34;color:#f92672&#34;&gt;)&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   William T. Riker &lt;w.t.riker@starfleet.org&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;COMMANDS:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    say use &lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;say&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt; to relay a message
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;GLOBAL OPTIONS:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   --help, -h           show help
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;   --version, -v        print the version
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now let’s try to use our &lt;code&gt;say&lt;/code&gt; command.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ./riker say &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;What&#39;s a knock-out like you doing in a computer-generated gin joint like this?&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;What&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&#39;&lt;/span&gt;s a knock-out like you doing in a computer-generated gin joint like this?
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Awesome! Now we’ll create a command that will execute two scripts. We’ll create
two simple scripts to show that we’re able to chain commands with the use of a
semicolon &lt;code&gt;;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Create the following files: &lt;code&gt;script1.sh&lt;/code&gt; and &lt;code&gt;script2.sh&lt;/code&gt;. And add the
following lines:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# script1.sh&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#!/bin/bash&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;echo to boldy go where
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# script2.sh&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#!/bin/bash&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;echo no man has gone before
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Make them executable:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo chmod +x script1.sh script2.sh
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now update the &lt;code&gt;engage.json&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;our first engage app&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;authors&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;William T. Riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;email&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;w.t.riker@starfleet.org&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;version&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;1.0&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;commands&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;say&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;action&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;echo&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;use `say` to relay a message&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;tagline&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;action&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;./script1.sh; ./script2.sh&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;space the final frontier&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This should give us the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ./riker tagline
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;to boldy go where
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;no man has gone before
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can also use commandline arguments with our cli app. Create a file
&lt;code&gt;script3.sh&lt;/code&gt; and the lines:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# script3.sh&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ARG&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;&lt;/span&gt;$1&lt;span style=&#34;color:#e6db74&#34;&gt;&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;echo Hello &lt;span style=&#34;color:#e6db74&#34;&gt;${&lt;/span&gt;ARG&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now update the &lt;code&gt;engage.json&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;our first engage app&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;authors&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;William T. Riker&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;email&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;w.t.riker@starfleet.org&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;version&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;1.0&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;commands&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;say&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;action&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;echo&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;use `say` to relay a message&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;tagline&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;action&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;./script1.sh; ./script2.sh&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;space the final frontier&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;name&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;greetings&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;action&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;./script3.sh&#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;usage&#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;use to greet someone&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And, now you can do the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ riker greetings World
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Hello World
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ riker greetings Picard
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Hello Picard
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;usage&#34;&gt;Usage&lt;/h3&gt;
&lt;p&gt;Now, that we have created our cli app it would be nice to have it run directly
from the commandline, instead of referencing the location of the binary on the
filesystem.&lt;/p&gt;
&lt;p&gt;When you want to create a cli app for a project for instance, then you might
want to place the cli app in that project together with its &lt;code&gt;engage.json&lt;/code&gt;
and create a symlink in one of the following locations.&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Folder&lt;/th&gt;
          &lt;th&gt;Access&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;code&gt;$HOME/bin&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;yourself only&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;code&gt;/usr/local/bin&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;you and other local users&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;code&gt;/usr/local/sbin&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;root only&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;And, to create the symlink:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ln -s ~/location/of/app $HOME/bin
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now you’ll be able to access your cli app directly from the commandline!&lt;/p&gt;
&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;In this post I’ve shown you have you can create your own cli application with a
config file. If you like to know more about ’engage’, have feedback/suggestions
want to follow its development. Then visit the project page on
&lt;a href=&#34;https://github.com/erroneousboat/engage&#34;&gt;github&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>How to enable true-color for neovim, tmux, and gnome-terminal</title>
      <link>https://bruinsslot.jp/articles/how-to-enable-true-color-for-neovim-tmux-and-gnome-terminal/</link>
      <pubDate>Sun, 29 May 2016 12:00:00 +0000</pubDate>
      
      <guid>https://bruinsslot.jp/articles/how-to-enable-true-color-for-neovim-tmux-and-gnome-terminal/</guid>
      <description>&lt;p&gt;With the release of tmux version 2.2 we’re able to get true-color support
inside of tmux. And because both neovim and gnome-terminal are also supporting
this, we can have great looking interfaces and colorschemes inside of tmux.&lt;/p&gt;
&lt;p&gt;However, there are some steps we need to take in order to make this work.
So I created this blog post for future reference, and hopefully you might find
it useful too.&lt;/p&gt;
&lt;h3 id=&#34;gnome-terminal&#34;&gt;gnome-terminal&lt;/h3&gt;
&lt;p&gt;So lets start with gnome-terminal. First, we need to make sure that
gnome-terminal is version linked with &lt;code&gt;libvte &gt;= 0.36&lt;/code&gt;. In order to check
this, run the following command, and if you have the correct version of
gnome-terminal. The version should be greater than or equal to &lt;code&gt;0.36&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ldd /usr/lib/gnome-terminal/gnome-terminal-server | grep libvte
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If this isn’t the case, we need to get gnome-terminal from the staging
ppa and install the latest version.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo add-apt-repository ppa:gnome3-team/gnome3-staging
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo apt-get update
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo apt-get install gnome-terminal
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# the following will remove the staging repository because we only want to&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# install gnome-terminal&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo add-apt-repository -r ppa:gnome3-team/gnome3-staging
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You might need to restart your computer or you could restart the
gnome-terminal-server. And, just to be sure, check if &lt;code&gt;libvte&lt;/code&gt; is the correct
version.&lt;/p&gt;
&lt;p&gt;Now, let’s check if we can really get the true-color spectrum. Download the
following script, and execute it.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# download script&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ wget https://raw.githubusercontent.com/robertknight/konsole/master/tests/color-spaces.pl
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# make the script executable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo chmod +x color-spaces.pl
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# run the script&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ./color-spaces.pl
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It should look something like the following:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://bruinsslot.jp/img/post_true-color_gnome.png&#34; alt=&#34;gnome-terminal&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;optional-support-for-italics&#34;&gt;Optional: support for italics&lt;/h3&gt;
&lt;p&gt;I wanted to be able to have support for italics in tmux, and this
is how you do it. Create a file name &lt;code&gt;xterm-256color-italic.terminfo&lt;/code&gt; with
the following contents:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# A xterm-256color based TERMINFO that adds the escape sequences for italic.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;xterm-256color-italic|xterm with &lt;span style=&#34;color:#ae81ff&#34;&gt;256&lt;/span&gt; colors and italic,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sitm&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\E&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;3m, ritm&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\E&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;23m,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                use&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;xterm-256color,
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then install it by doing the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tic xterm-256color-italic.terminfo
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now you need to make sure that the terminfo is used by both your shell and
tmux. Make sure the following line is present in your &lt;code&gt;.bashrc&lt;/code&gt; or &lt;code&gt;.zshrc&lt;/code&gt;.
Depending on which shell you’re using.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;export TERM&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;xterm-256color-italic
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In your &lt;code&gt;.tmux.conf&lt;/code&gt; file add the following line:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;set -g default-terminal &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;xterm-256color-italic&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now you can check if it is set correctly by typing in the following command in
both the gnome-terminal and tmux.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ echo $TERM
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;xterm-256color-italic
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we should be able to get italics, and to check this, type the following in
both gnome-terminal and tmux. This should output the word &lt;em&gt;italics&lt;/em&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;echo &lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;tput sitm&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;italics&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;tput ritm&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;tmux&#34;&gt;tmux&lt;/h3&gt;
&lt;p&gt;On to tmux. First we need to check if we have the correct version.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tmux -V
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# it should be &gt;= 2.2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;tmux 2.2
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If it isn’t the correct version, then remove tmux from you system and install
it. Get the latest &lt;code&gt;tar.gz&lt;/code&gt; file from the following &lt;a href=&#34;https://github.com/tmux/tmux/releases&#34;&gt;page&lt;/a&gt;.
Extract the contents and install it. Instructions are also available on the
&lt;a href=&#34;https://github.com/tmux/tmux&#34;&gt;tmux github page&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# on ubuntu:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo apt-get install -y libevent-dev libncurses-dev build-essential
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tar -xzvf tmux-2.2.tar.gz
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ cd tmux-2.2
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ./configure &lt;span style=&#34;color:#f92672&#34;&gt;&amp;&amp;&lt;/span&gt; make
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ sudo make install
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now that we have the correct version we need to enable Tc terminal capability
for the outer terminal (to which tmux is attached) by setting the
terminal-overrides option in tmux.&lt;/p&gt;
&lt;p&gt;First it will be reported as missing.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tmux info | grep Tc
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Tc: missing
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And by adding the following line to your &lt;code&gt;.tmux.conf&lt;/code&gt; file (you might need
to restart your terminal):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;set-option -ga terminal-overrides &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;,xterm-256color:Tc&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# When you&#39;re using the italic setup use the following line&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;set-option -ga terminal-overrides &lt;span style=&#34;color:#e6db74&#34;&gt;&#34;,xterm-256color-italic:Tc&#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The flag will be set to true:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tmux
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ tmux info | grep Tc
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;197: Tc: &lt;span style=&#34;color:#f92672&#34;&gt;(&lt;/span&gt;flag&lt;span style=&#34;color:#f92672&#34;&gt;)&lt;/span&gt; true
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now check the true-color capabilities of tmux by executing the script we used
with the gnome-terminal.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ./color-spaces.pl
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;neovim&#34;&gt;neovim&lt;/h3&gt;
&lt;p&gt;To enable true color capabilities for neovim add the following line to your
&lt;code&gt;init.vim&lt;/code&gt; file (located at &lt;code&gt;~/.config/nvim/&lt;/code&gt;):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;set termguicolors
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now you can choose a colorscheme that doesn’t force neovim to use 256 colors,
or one that doesn’t contain the line &lt;code&gt;if !has(&#39;gui_running&#39;)&lt;/code&gt;.
When it doesn’t it will use the &lt;code&gt;guifg&lt;/code&gt; and &lt;code&gt;guibg&lt;/code&gt; colors from the colorscheme.&lt;/p&gt;
&lt;p&gt;Sources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/erroneousboat/dotfiles&#34;&gt;My dotfiles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://askubuntu.com/a/532939&#34;&gt;How to enable 24bit true color support in gnome-terminal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://homeonrails.com/2016/05/truecolor-in-gnome-terminal-tmux-and-neovim/&#34;&gt;TrueColor in Gnome Terminal, Tmux and Neovim&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/tmux/tmux/blob/2.1/FAQ#L355-L383&#34;&gt;Italics in tmux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.nerdyweekly.com/posts/enable-italic-text-vim-tmux-gnome-terminal/&#34;&gt;Enable italic text in vim tmux gnome-terminal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://sunaku.github.io/tmux-24bit-color.html&#34;&gt;Adding 24-bit TrueColor RGB escape sequences to tmux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/neovim/neovim/wiki/FAQ#how-can-i-use-true-colors-in-the-terminal&#34;&gt;Wiki neovim - How can I use true colors in the terminal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://gist.github.com/XVilka/8346728&#34;&gt;True Colors in the terminal&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
  </channel>
</rss>
