<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Benjamin</title>
    <description>The latest articles on DEV Community by Benjamin (@bmpickford).</description>
    <link>https://dev.to/bmpickford</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F200614%2F9c89e09e-e05a-43d0-a2ec-00d250adf914.jpeg</url>
      <title>DEV Community: Benjamin</title>
      <link>https://dev.to/bmpickford</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bmpickford"/>
    <language>en</language>
    <item>
      <title>Monorepos and Workspaces - an introduction</title>
      <dc:creator>Benjamin</dc:creator>
      <pubDate>Tue, 04 Jan 2022 01:56:27 +0000</pubDate>
      <link>https://dev.to/bmpickford/monorepos-and-workspaces-an-introduction-59l6</link>
      <guid>https://dev.to/bmpickford/monorepos-and-workspaces-an-introduction-59l6</guid>
      <description>&lt;p&gt;&lt;strong&gt;tldr;&lt;/strong&gt;&lt;br&gt;
When making a monorepo, it generally consist of workspaces (can use &lt;a href="https://docs.npmjs.com/cli/v7/using-npm/workspaces"&gt;npm&lt;/a&gt;, &lt;a href="https://classic.yarnpkg.com/lang/en/docs/workspaces/"&gt;yarn&lt;/a&gt;or &lt;a href="https://pnpm.io/workspaces"&gt;pnpm&lt;/a&gt;), but additional tooling exists like &lt;a href="https://github.com/lerna/lerna"&gt;lerna&lt;/a&gt; and &lt;a href="https://github.com/vercel/turborepo"&gt;turborepo&lt;/a&gt; (and lots of others too) to help with easing the problems of managing lots of interlinked packages&lt;/p&gt;

&lt;p&gt;see some real world examples&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a monorepo?
&lt;/h3&gt;

&lt;p&gt;There are lots of other posts that dive into monorepos and their benefits and recommend reading them before diving into the tooling of one. Here is my quick tldr;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A monorepo is a single repository, that contains multiple loosely coupled packages within it. It differs from a monolith in that it usually has a focus on distributing individual packages, and saves time by allowing multiple changes that might usually need to be made across multiple repository's in a single repository and PR.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Further reading
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.atlassian.com/git/tutorials/monorepos"&gt;https://www.atlassian.com/git/tutorials/monorepos&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/monorepo/"&gt;https://css-tricks.com/monorepo/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@mattklein123/monorepos-please-dont-e9a279be011b"&gt;https://medium.com/@mattklein123/monorepos-please-dont-e9a279be011b&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Challenges
&lt;/h2&gt;

&lt;p&gt;It's important to understand the challenges that monorepos present before diving into tooling and weighing up the best suite of tools for your use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  1 - Versioning
&lt;/h3&gt;

&lt;p&gt;The largest challenge people generally face with monorepos managing versions and deployments of packages. One scenario might be that you make a change in one component, bump the version and then want that version to propagate to all the other packages that might depend on it. This is of particular concern for breaking changes as you want to make sure you're not going to break anything for the end users.&lt;/p&gt;

&lt;h3&gt;
  
  
  2 - Deployment and CI
&lt;/h3&gt;

&lt;p&gt;Another challenge can be how to manage deployments, CI. Though a lot of CI tools have functionality to assist with this (e.g. &lt;a href="https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#onpushpull_requestpaths"&gt;Github Actions&lt;/a&gt;, &lt;a href="https://circleci.com/developer/orbs/orb/circleci/path-filtering"&gt;CircleCI&lt;/a&gt;, you may need to think about whether you want to run your CI suite against a single package, or the whole repository, and if any automation might differ depending on the change.&lt;/p&gt;

&lt;h3&gt;
  
  
  3 - PR's
&lt;/h3&gt;

&lt;p&gt;As monorepos can get quite large, figuring out who is best suited for reviews and/or approvals can get a bit tricky. If you use github, adding a &lt;a href="https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners"&gt;CODEOWNERS&lt;/a&gt; can help with automating this&lt;/p&gt;

&lt;h2&gt;
  
  
  Tooling
&lt;/h2&gt;

&lt;p&gt;The below tooling is a list of the most popular tools used for monorepos and managing workspaces. There is not necessarily a magic combination, but rather what works for you and your existing tooling. Not all of these tools are exclusive either, for example, yarn workspaces and lerna can be used together to create a more rich experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Workspaces
&lt;/h3&gt;

&lt;p&gt;Most monorepos in the javascript ecosystem are built upon workspaces, a feature that is now readily available in most of the popular package managers. There isn't necessarily one that is better than the rest and generally follow a somewhat similar syntax pattern, but there may be one that your more comfortable with.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://classic.yarnpkg.com/lang/en/docs/workspaces/"&gt;yarn workspaces&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/cli/v7/using-npm/workspaces"&gt;npm workspaces&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pnpm.io/workspaces"&gt;pnpm workspaces&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Additional tooling
&lt;/h3&gt;

&lt;p&gt;These are optional dependencies that are used to help with deployments, caching and linking packages. There is not necessarily a magic combination, but rather what works for you and your existing tooling.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/lerna/lerna"&gt;lerna&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/vercel/turborepo"&gt;turborepo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nx.dev/"&gt;nx&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Real world use cases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1 - Component library
&lt;/h3&gt;

&lt;p&gt;One common use of monorepos is a design library. It allows for multiple publishable components and allows for users to import only what they need if desired, and encourages best practices for designing and creating your components by forcing them to be independent entities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1 - Chakra UI:&lt;/strong&gt; &lt;a href="https://github.com/chakra-ui/chakra-ui"&gt;https://github.com/chakra-ui/chakra-ui&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Example 2 - Material UI:&lt;/strong&gt; &lt;a href="https://github.com/mui-org/material-ui"&gt;https://github.com/mui-org/material-ui&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2 - Optional configurations and extras
&lt;/h3&gt;

&lt;p&gt;Another common use case is that a product might have different available options that are made available to the end user, but don't necessarily fit within the core product. This could be using different build tools / versions, different variations of linting or packages that might only cater for specific use cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1 - Storybook:&lt;/strong&gt; &lt;a href="https://github.com/storybookjs/storybook"&gt;https://github.com/storybookjs/storybook&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Example 2 - OpenWC:&lt;/strong&gt; &lt;a href="https://github.com/open-wc/open-wc"&gt;https://github.com/open-wc/open-wc&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Example 3 - Lit:&lt;/strong&gt; &lt;a href="https://github.com/lit/lit.dev"&gt;https://github.com/lit/lit.dev&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More resources
&lt;/h2&gt;

&lt;p&gt;[list] &lt;strong&gt;&lt;a href="https://github.com/korfuri/awesome-monorepo"&gt;Awesome monorepo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Curated list of monorepo resources&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;[blog] &lt;strong&gt;&lt;a href="https://blog.bitsrc.io/11-tools-to-build-a-monorepo-in-2021-7ce904821cc2"&gt;11 Great Tools for a Monorepo in 2021&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Probably the best tools to develop and build your monorepo.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;[blog] &lt;strong&gt;&lt;a href="https://semaphoreci.com/blog/what-is-monorepo"&gt;What is a monorepo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Monorepos can foster rapid development workflows. In this post, we’ll examine if they are the right fit for you and your company.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;[blog] **&lt;a href="https://adrianhesketh.com/2021/05/11/trying-out-npm-and-yarn-workspaces/"&gt;Trying out npm and yarn workspaces&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A practical walkthrough of npm vs yarn workspaces&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>npm</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Integrating Prism into a create-react-app project</title>
      <dc:creator>Benjamin</dc:creator>
      <pubDate>Sun, 19 Sep 2021 17:19:33 +0000</pubDate>
      <link>https://dev.to/bmpickford/integrating-prism-into-a-create-react-app-project-106l</link>
      <guid>https://dev.to/bmpickford/integrating-prism-into-a-create-react-app-project-106l</guid>
      <description>&lt;h2&gt;
  
  
  &lt;a href="https://prismjs.com/"&gt;Prism&lt;/a&gt; + &lt;a href="https://github.com/facebook/create-react-app"&gt;create-react-app&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Prism doesn't have a setup akin to much of the modern web, which can create some friction when integrating with modern tooling for ever changing frameworks. There are a few methods of installing prismjs, but this guide follows their recommended approach. Assuming a project already exists that was created with create-react-app, these steps should get you up and running with Prism quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Download and Installation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to &lt;a href="https://prismjs.com/"&gt;https://prismjs.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Click Download and select the theme you want, languages and plugins you would like to support&lt;/li&gt;
&lt;li&gt;Download JS and CSS&lt;/li&gt;
&lt;li&gt;Copy the JS and CSS to the public folder&lt;/li&gt;
&lt;li&gt;In your &lt;code&gt;public/index.html&lt;/code&gt;, add this tag:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- index.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"%PUBLIC_URL%/prism.js"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;link&lt;/span&gt; &lt;span class="nx"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stylesheet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/prism.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Usage with dyanmic content
&lt;/h3&gt;

&lt;p&gt;You will need to do one of the below if you are changing content that needs syntax highlighting. For example, wisiwyg editor. Add one of the below code blocks where you're update your data.&lt;/p&gt;

&lt;h4&gt;
  
  
  Option 1 - Reload all highlight on value change.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt; &lt;span class="cm"&gt;/* your dynamic code block */&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Prism&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;highlightAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`language-json`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Option 2 - Reload single code block (recommended)
&lt;/h4&gt;

&lt;p&gt;This approach is recommended if you have more than 1 code block, as it will save you unnessesary reloads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt; &lt;span class="cm"&gt;/* your dynamic code block */&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Prism&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;highlightElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;codeEl&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`language-json`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;code&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Aternative
&lt;/h2&gt;

&lt;p&gt;If the above approach doesn't suit your application or team, there is an npm package you can use, however you will need to also use the supplied babel plugin. When using a no config solution like create-react-app, this becomes difficult and you may will may need to use &lt;a href="https://github.com/gsoft-inc/craco"&gt;craco&lt;/a&gt; or eject your configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SomeComponent.jsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Prism&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prismjs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// code here&lt;/span&gt;

&lt;span class="nx"&gt;Prism&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;highlightAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// code here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the babel plugin see: &lt;a href="https://github.com/mAAdhaTTah/babel-plugin-prismjs"&gt;https://github.com/mAAdhaTTah/babel-plugin-prismjs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Full Example
&lt;/h2&gt;

&lt;p&gt;To see a full working example, PrismJS has been implemented in the following web app:&lt;br&gt;
&lt;a href="https://github.com/bmpickford/dynamoconverter"&gt;https://github.com/bmpickford/dynamoconverter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>ShouldIKeepWatching - Digital Ocean Hackathon</title>
      <dc:creator>Benjamin</dc:creator>
      <pubDate>Fri, 08 Jan 2021 23:38:58 +0000</pubDate>
      <link>https://dev.to/bmpickford/shouldikeepwatching-digital-ocean-hackathon-9i6</link>
      <guid>https://dev.to/bmpickford/shouldikeepwatching-digital-ocean-hackathon-9i6</guid>
      <description>&lt;h2&gt;
  
  
  What I built
&lt;/h2&gt;

&lt;p&gt;A web app to help you decide whether or not to invest any more time in a TV show you're watching. No one wants to waste their time on bad TV!&lt;/p&gt;

&lt;h3&gt;
  
  
  Category Submission: Random Roulette
&lt;/h3&gt;

&lt;h3&gt;
  
  
  App Link
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://shouldikeepwatching-kpcfk.ondigitalocean.app/" rel="noopener noreferrer"&gt;https://shouldikeepwatching-kpcfk.ondigitalocean.app/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Screenshots
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsk2mn5386ch9momamzzg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsk2mn5386ch9momamzzg.PNG" alt="hackathon_screen_1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxn3tq1qguih45rzvq9oe.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxn3tq1qguih45rzvq9oe.PNG" alt="hackathon_screen_2"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnrqtw1vgvbil3io85wq5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnrqtw1vgvbil3io85wq5.PNG" alt="hackathon_screen_3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Link to Source Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/bmpickford/shouldikeepwatching" rel="noopener noreferrer"&gt;https://github.com/bmpickford/shouldikeepwatching&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Permissive License
&lt;/h3&gt;

&lt;p&gt;MIT&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;With great new TV shows coming out every other week, it's hard to know what's actually worth watching, especially if you're going to be investing your time in lots of seasons. I'm always hesitant to start a TV show that I know is going to go downhill (e.g. Heroes, Dexter) so I want to know upfront how bad it gets. This heatmap I created for the TV shows, gives a quick visual representation of a TV show so I know what I'm in for.&lt;/p&gt;

&lt;h3&gt;
  
  
  How I built it
&lt;/h3&gt;

&lt;p&gt;I used NextJS (for the first time) to try and explore the realm of SSR using React. It was a suprisingly nice developer experience, and a much smaller learning curve than expected. Will definitely look to NextJS for future projects that could benefit from SSR.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Firebase Auth with React Hooks in Typescript</title>
      <dc:creator>Benjamin</dc:creator>
      <pubDate>Thu, 08 Oct 2020 05:05:50 +0000</pubDate>
      <link>https://dev.to/bmpickford/firebase-auth-with-react-hooks-in-typescript-44b7</link>
      <guid>https://dev.to/bmpickford/firebase-auth-with-react-hooks-in-typescript-44b7</guid>
      <description>&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;Firebase is a great provider for hooking up easy and simple authentication to a new or existing project, and can easily be integrated with other features of Google Cloud Platform. Adding it to your application can be simple, but a lot of tutorials don't cater for larger scale applications, and how it could be implemented cleanly and with quality assurance in mind.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assumed Knowledge
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Firebase&lt;/li&gt;
&lt;li&gt;Typescript&lt;/li&gt;
&lt;li&gt;React&lt;/li&gt;
&lt;li&gt;React Hooks (specifically useContext)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting Up
&lt;/h3&gt;

&lt;h4&gt;
  
  
  GCP and Firebase
&lt;/h4&gt;

&lt;p&gt;To start, you'll need to setup a GCP account and login to the Firebase console at &lt;a href="https://console.firebase.google.com/"&gt;https://console.firebase.google.com/&lt;/a&gt;. You'll then need to setup a project for us to use in this run through. For this walkthrough, I've named my project "HooksAuth"&lt;/p&gt;

&lt;p&gt;I'm then going to enable email/password and Google Authentication by navigating to the "Authentication" page from the left menu and enabling them there&lt;/p&gt;

&lt;h4&gt;
  
  
  React
&lt;/h4&gt;

&lt;p&gt;To setup your react project from scratch, you can use create-react-app by running the following command in your cli: &lt;code&gt;npx create-react-app hooksAuth --template typescript&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The next (optional) step is to remove all the boilerpate content from your React App. e.g. react images, manifest, App.tsx content etc. This helps me to start from a cleaner slate and not have create-react-app content in my site&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up the hooks
&lt;/h4&gt;

&lt;p&gt;We are going to abstract our firebase app away through useContext so it's state can be shared through the app, and any abstractions and access can easily be updated and refactored.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;FirebaseContext.ts&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add your config to this file (this can be retrieved by registering a web app in the firebase console), and will look something like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import firebase from 'firebase';

const firebaseConfig = {
    apiKey: "KEY",
    authDomain: "somedomain.firebaseapp.com",
    databaseURL: "https://somedomain.firebaseio.com",
    projectId: "hooksauth-ID_HERE",
    storageBucket: "hooksauth-ID_HERE.appspot.com",
    messagingSenderId: "SENDER_ID",
    appId: "APP_ID"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now we want to secure these values using .env (before we accidentally commit anything). To do this, create a file called .env.local in the root of the project, and add this file to .gitignore. In that file, you'll want to define your ID's in something similar to this format:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REACT_APP_API_KEY=KEY
REACT_APP_AUTH_DOMAIN=somedomain.firebaseapp.com
REACT_APP_DB_URL=https://somedomain.firebaseio.com
REACT_APP_PROJECT_ID=hooksauth-ID_HERE
REACT_APP_STORAGE_BUCKET=hooksauth-ID_HERE.appspot.com
REACT_APP_MESSAGING_SENDER_ID=SENDER_ID
REACT_APP_APP_ID=APP_ID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and update your config to be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const firebaseConfig = {
    apiKey: process.env.REACT_APP_API_KEY,
    authDomain: process.env.REACT_APP_AUTH_DOMAIN,
    databaseURL: process.env.REACT_APP_DB_URL,
    projectId: process.env.REACT_APP_PROJECT_ID,
    storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
    messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
    appId: process.env.REACT_APP_APP_ID,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Move the firebase initialisation. To stop the firebase initialisation happening multiple times, we want to wrap it in an if statement to make it look like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Initialize Firebase
if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig);
    firebase.analytics();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now we want to actually do some React stuff. We want to create a context for us to use in the rest of our app. So using react hooks, we will create a context that contains the auth providers we will use, and a way to access our firebase object:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export interface IFirebaseContext {
    firebase: firebase.app.App,
    authProviders: string[]
}

export const FirebaseContext = React.createContext({} as IFirebaseContext)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then our provider to wrap the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const FirebaseProvider = ({ children }: any) =&amp;gt; {
    return (
        &amp;lt;div&amp;gt;
            &amp;lt;FirebaseContext.Provider value={{ 'firebase': firebase.app(), authProviders } as IFirebaseContext}&amp;gt;
                {children}
            &amp;lt;/FirebaseContext.Provider&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now to wrap the rest of our app. So in App.tsx, we want to wrap the root div in &lt;code&gt;&amp;lt;FirebaseProvider&amp;gt;&lt;/code&gt;. And now in the rest of our App, if we want to access our auth object, we would simply call &lt;code&gt;const firebaseContext = useContext(FirebaseContext);&lt;/code&gt; and access it there. This can be combined with the firebase-ui package to get your authentication up and running in no time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For an example of usage with the react-firebaseui package and saving user data see my github repo: &lt;a href="https://github.com/bmpickford/firebase-auth-example"&gt;https://github.com/bmpickford/firebase-auth-example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open to any improvements so feel free to submit a PR or an issue&lt;/p&gt;

</description>
      <category>react</category>
      <category>firebase</category>
      <category>typescript</category>
      <category>auth</category>
    </item>
    <item>
      <title>Practical Beginners Guide to Problems with JS async/await</title>
      <dc:creator>Benjamin</dc:creator>
      <pubDate>Mon, 30 Mar 2020 22:44:15 +0000</pubDate>
      <link>https://dev.to/bmpickford/practical-beginners-guide-to-problems-with-js-async-await-200p</link>
      <guid>https://dev.to/bmpickford/practical-beginners-guide-to-problems-with-js-async-await-200p</guid>
      <description>&lt;h2&gt;
  
  
  What is it
&lt;/h2&gt;

&lt;p&gt;I don't want to go too in depth into what async/await is, and how it came about as there are many other good blog posts about it, but it is basically a syntax sugar around Promises, and gives your code the ability to be asynchronous&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;The async/await syntax has given us the ability to have Promises in a more readable and cleaner way then the native Promise syntax. The basic usage of it, looks something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;GetSomeResource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://someurl.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DoStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;GetSomeResource&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This has the obvious problem of not handling errors, but there are cases where maybe we don't want to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems
&lt;/h2&gt;

&lt;p&gt;There are a few gotchas with async/await, especially if your application lacks the linting around it. I have seen both Senior and Junior devs fall into the simple trap of forgetting an await somewhere, which can make debugging and testing quite unreliable and confusing. So what happens when someone forgets the simple await syntax? e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AddSomeResource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://someurl.com/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;Method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;GetSomeResource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://someurl.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DoStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;AddSomeResource&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;GetSomeResource&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What would happen here is that the &lt;code&gt;AddSomeResource&lt;/code&gt; function would run in the background while other tasks happened. So if in this example, anything relied on that function posting data, there's a chance that it wouldn't have been finished executing, and could cause intermittent errors in your application.&lt;br&gt;
There is another case however where you don't rely on that resource elsewhere in the same scope, and everything seems to be working fine when it gets tested. &lt;/p&gt;

&lt;p&gt;However once the external service is down and starts timing out, because the error hasn't been caught or passed up the chain to be dealt with elsewhere it starts logging errors while your other code is running. But the chain of functions associated with that call still seem to be working fine, until something gets called that requires that resource somewhere else in the application. This can send you down a bit of a rabbi hole and waste possibly several hours trying to debug what's actually going on&lt;/p&gt;

&lt;p&gt;Forgetting await can be easier than it seems, especially when not using linting and some kind of typing framework such as Typescript. e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DoSomeStuffAndReturnBool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;GetSomeNumAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getNum&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DoStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isTrue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DoSomeStuffAndReturnBool&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, especially to someone starting out in JS, it could be easy to not think about the async keyword and just see it returns a bool. But in the case of async, it always returns the result as a Promise&lt;/p&gt;

&lt;h2&gt;
  
  
  Thinking an await, awaits all the way down
&lt;/h2&gt;

&lt;p&gt;I recently had a discussion with another developer who thought that the await keyword, would await all the child functions too, even the ones without await keywords. e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AddSomeResource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://someurl.com/user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;Method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DoStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;AddSomeResource&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DoParentStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This does not automatically await the AddSomeResource call in DoStuff()&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;DoStuff&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Add some linting around async and Promises so you don't forget to await, otherwise you could spend too much time debugging your application or getting inconsistent results&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Basic Go HTTP server with Postgres</title>
      <dc:creator>Benjamin</dc:creator>
      <pubDate>Fri, 16 Aug 2019 11:44:23 +0000</pubDate>
      <link>https://dev.to/bmpickford/basic-go-http-server-with-postgres-og3</link>
      <guid>https://dev.to/bmpickford/basic-go-http-server-with-postgres-og3</guid>
      <description>&lt;p&gt;I've found myself creating simple Golang microservices using the same basic design to start out. In this post I'll go through the steps from scratch to help create a simple starting point for a Go microservice or API with the ability to create a customer&lt;/p&gt;

&lt;p&gt;Throughout this brief tutorial, I have purposely tried to keep it as minimal as possible to get to know the native packages (net/http, encoding/json)&lt;/p&gt;




&lt;h3&gt;
  
  
  Basic HTTP server
&lt;/h3&gt;

&lt;p&gt;So presuming you have to go environment all setup, let's start by creating a &lt;code&gt;main.go&lt;/code&gt; file in our new project with a basic http server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"8080"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here, if you run &lt;code&gt;go run .&lt;/code&gt; you shouldn't get any errors and should be a good indicator your go setup is correct&lt;/p&gt;

&lt;p&gt;So now we have a basic server, it's time to setup a handler to check our configurations are all good. Here we will create a simple response for GET /. To do this, add this handler function to &lt;code&gt;main.go&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handleHomePage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello, the site is running :)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then this to your &lt;code&gt;main()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;homePage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your &lt;code&gt;main.go&lt;/code&gt; file should now look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handleHomePage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handleHomePage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello, the site is running :)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to test this works, hit the server with curl using &lt;code&gt;curl -X GET 0.0.0.0:8080/&lt;/code&gt; and your response should be &lt;code&gt;Hello, the site is running :)&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Postgres setup
&lt;/h3&gt;

&lt;p&gt;So now we have a basic HTTP server, it's time to add our postgres DB. Assuming you have postgres already installed, lets add a Database and a table. To do this, open postgres in the command line and execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE go_project;
CREATE TABLE customer (id SERIAL, username VARCHAR NOT NULL);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Go/Postgres connection
&lt;/h3&gt;

&lt;p&gt;To setup our connection, we will be using the pq package for golang (use &lt;code&gt;go get github.com/lib/pq&lt;/code&gt; to download the package). We will not create a new file called postgres.go, that will contain a singleton that will be used in our subsequent files to access the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"database/sql"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;

    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="s"&gt;"github.com/lib/pq"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;GetDB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;connStr&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"user=postgres dbname=go_project"&lt;/span&gt;
        &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;connStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we are doing here, is creating a connection using the name of our database and a user for our postgres instance. This might change depending on your setup, and may need some authentication&lt;/p&gt;




&lt;h3&gt;
  
  
  Finishing our handlers
&lt;/h3&gt;

&lt;p&gt;Now it's time to expand our application to include some real logic. To start, we have to create our Customer struct. Firstly though, we have to create a new file for our handlers. I have created one called &lt;code&gt;handler.go&lt;/code&gt; but you can name it anything you like. In this file, add the package name and our new struct as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Customer&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;       &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to add our handler. For this, we need to create a decoder that will decode the body of our request (which will be JSON format) into our struct. This can be done using the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handleCustomerPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="n"&gt;Customer&lt;/span&gt;

    &lt;span class="n"&gt;decoder&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDecoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;decoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;201&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have the ability to decode a request body into a Customer model, it's time to add our handler to &lt;code&gt;main.go&lt;/code&gt; as we did with our root response. This time though, we have to filter the POST request because the native http package doesn't have the functionality for that. If you add gorilla mux, this can be handled for you, but for this case I want to keep it as minimal as possible. So we need to add another handler in main, as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handleCustomerRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Method&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MethodPost&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;handleCustomerRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;201&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and a call to it in the &lt;code&gt;main()&lt;/code&gt; function as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/customer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handleCustomerRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Linking it all together
&lt;/h3&gt;

&lt;p&gt;Now we need to create our query for saving a customer. To do this, we need to create a new file (i called mine &lt;code&gt;database.go&lt;/code&gt;), and add the following function that takes in a username&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;SaveCustomer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;GetDB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"INSERT INTO customer (username) VALUES($1)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now in &lt;code&gt;handler.go&lt;/code&gt;, call this function using the customers username as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SaveCustomer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to test our functionality so far. So start up your go service using &lt;code&gt;go run .&lt;/code&gt; again. Now if you hit your endpoint, you should get a 201, and a customer with an ID and username in your database&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I hope you have learnt something about creating a basic http server in golang. If you want to see the full codebase, see &lt;a href="https://github.com/bmpickford/goHttpServer"&gt;https://github.com/bmpickford/goHttpServer&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>sql</category>
      <category>api</category>
    </item>
    <item>
      <title>Rewriting your (git) history</title>
      <dc:creator>Benjamin</dc:creator>
      <pubDate>Fri, 02 Aug 2019 04:01:25 +0000</pubDate>
      <link>https://dev.to/bmpickford/rewriting-your-git-history-4p7a</link>
      <guid>https://dev.to/bmpickford/rewriting-your-git-history-4p7a</guid>
      <description>&lt;p&gt;So you probably know a lot of basics of git, but if you have been using it for a while you have probably encountered a situation where you want to change a commit that's already been pushed. Some argue this is bad practice, and others say it's bad to have an unclean commit history. Although I probably lean more towards the latter, this post is simply about the tools you can use if you want to change some of your already pushed commits&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Change most recent commit message
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit --amend
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you have been using git for a while you probably already know about this one. It's generally used to change your most recent commits message if you found a type or wanted to change your commit message layout. It can also be used to changed the author by appending &lt;code&gt;--reset-author&lt;/code&gt; to the amend&lt;/p&gt;

&lt;h1&gt;
  
  
  The interactive rebase
&lt;/h1&gt;

&lt;p&gt;For these examples, I am going to be using 3 commits I have created (see below) and all of these examples first involve the interactive rebase command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git rebase -i HEAD~3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will lead you to a terminal output that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pick 74c349f [1] Dummy commit 1
pick f490852 [2] Dummy commit 2
pick 717eaa6 [3] Dummy commit 3

# Rebase b8d17a8..717eaa6 onto b8d17a8 (3 commands)
#
# Commands:
# p, pick &amp;lt;commit&amp;gt; = use commit
# r, reword &amp;lt;commit&amp;gt; = use commit, but edit the commit message
# e, edit &amp;lt;commit&amp;gt; = use commit, but stop for amending
# s, squash &amp;lt;commit&amp;gt; = use commit, but meld into previous commit
# f, fixup &amp;lt;commit&amp;gt; = like "squash", but discard this commit's log message
# x, exec &amp;lt;command&amp;gt; = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop &amp;lt;commit&amp;gt; = remove commit
# l, label &amp;lt;label&amp;gt; = label current HEAD with a name
# t, reset &amp;lt;label&amp;gt; = reset HEAD to a label
# m, merge [-C &amp;lt;commit&amp;gt; | -c &amp;lt;commit&amp;gt;] &amp;lt;label&amp;gt; [# &amp;lt;oneline&amp;gt;]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c &amp;lt;commit&amp;gt; to reword the commit message.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I will use this as the basis for the rest of the examples&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Squashing commits
&lt;/h2&gt;

&lt;p&gt;Squashing commits is useful if you have some related commits that could be merged into one larger commit, or in some cases, used to make code reviews easier&lt;/p&gt;

&lt;p&gt;To squash the commits simply replace the word &lt;code&gt;pick&lt;/code&gt; with &lt;code&gt;s&lt;/code&gt; for commits 2 and 3 to merge them both into commit 1.&lt;br&gt;
e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pick 74c349f [1] Dummy commit 1
s f490852 [2] Dummy commit 2
s 717eaa6 [3] Dummy commit 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will lead you to a new commit where you can edit/merge the commit message after you save and exit the commit message. After saving your commit message you should be able to &lt;code&gt;git log&lt;/code&gt; and see your changes all in one commit&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Changing an old commit message (reword)
&lt;/h2&gt;

&lt;p&gt;If I wanted to change the message of commit 3, i would do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pick 74c349f [1] Dummy commit 1
r f490852 [2] Dummy commit 2
pick 717eaa6 [3] Dummy commit 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then continue on like a normal &lt;code&gt;git commit --amend&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Dropping a commit
&lt;/h2&gt;

&lt;p&gt;If you are dropping a commit, be extra careful about the contents and consider squashing instead. However if you really want to delete a commit, just use the drop command. e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pick 74c349f [1] Dummy commit 1
d f490852 [2] Dummy commit 2
pick 717eaa6 [3] Dummy commit 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will delete &lt;code&gt;[2] Dummy commit 2&lt;/code&gt; from your commit history and the changes along with it&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Editing a commit
&lt;/h2&gt;

&lt;p&gt;If you want to add or remove a file/change from a previous commit, you can use the edit functionality. For example, if you added a file but it should actually belong in the previous commit, you would do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pick 74c349f [1] Dummy commit 1
e f490852 [2] Dummy commit 2
pick 717eaa6 [3] Dummy commit 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then you can add/remove files as you wish. Once you are done, then use the &lt;code&gt;git commit --amend&lt;/code&gt; command, followed by a &lt;code&gt;git rebase --continue&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This can also be used to split a commit in two. As an example, if you follow the above edit step, and want to add a new commit before it you would do the above and then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reset HEAD^
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From here to you add the files you would like, and create new commits. Then when you are done, execute the &lt;code&gt;git rebase --continue&lt;/code&gt; command&lt;/p&gt;




&lt;p&gt;Thank you for reading, and I hope this clears up some of the interactive rebase functionality&lt;/p&gt;

</description>
      <category>git</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
