<?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: John Behan</title>
    <description>The latest articles on DEV Community by John Behan (@jjmax75).</description>
    <link>https://dev.to/jjmax75</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%2F325958%2F9663809d-6373-49af-8782-8f9f31e36cb6.jpeg</url>
      <title>DEV Community: John Behan</title>
      <link>https://dev.to/jjmax75</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jjmax75"/>
    <language>en</language>
    <item>
      <title>Using Environment Variables in your app</title>
      <dc:creator>John Behan</dc:creator>
      <pubDate>Thu, 11 Mar 2021 12:43:31 +0000</pubDate>
      <link>https://dev.to/jjmax75/using-environment-variables-in-your-app-17kk</link>
      <guid>https://dev.to/jjmax75/using-environment-variables-in-your-app-17kk</guid>
      <description>&lt;h2&gt;
  
  
  Keeping secrets
&lt;/h2&gt;

&lt;p&gt;Some values in your application are unique to where your app is being run. For example you may be testing your app against a local server when you are developing it on your computer. But when you publish your app you will want it to use a publicly available server on the Internet.&lt;/p&gt;

&lt;p&gt;Another issue you face is maintaining values in your app that you don't want to share publicly. Things like an API Key or a database password are examples of these. You can have them in your code when working on your computer. But when you push your code to a repository, or share it with a colleague, you don't want to send these secret values.&lt;/p&gt;

&lt;p&gt;Managing these values can be painful. It is very easy to forget to change them before publishing or pushing your code. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you forget to change a value that points to a local server your app will break. Worse - it may break for everyone but you as you are still running the local service your app needs 🤦🏼‍♀️&lt;/li&gt;
&lt;li&gt;Forget to remove secrets and you risk publishing sensitive information. At best this will mean a cleanup job for your Git repository and a quick change of those secrets. At worst.... well that's a subject for another day.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is a simple solution for these probelms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment Variables
&lt;/h2&gt;

&lt;p&gt;Let's break this down a little. The environment can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The computer you are using when writing your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For a backend service it could be the server that you deploy your application to.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A frontend application may need to go through a build step. The environment in this case is the system where you perform the build step.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are already familiar with variables. They are a way to refer to a value stored in memory. Environment Variables are the same.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;The example application is a Node service which is connected to a database. It is deployed to Heroku. Heroku is one of the easiest ways to deploy a node application. Setting Environment Variables there is straightforward. But you could use any platform - Environment Variables will be available there too.&lt;/p&gt;

&lt;p&gt;This app has both problems I outlined at the start:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You may want to use a local database during development. But your app uses a different database for production. So you need to change the database connection string when deploying.&lt;/li&gt;
&lt;li&gt;The connection string is a secret that you do not want to share.  You do not want this information leaking to the Internet. Doing so would compromise the integrity of your application and the security of your data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can solve these problems by using Environment Variables. The best way to do this on your local machine is to use the dotenv package.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install dotenv: &lt;code&gt;npm i dotenv&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At the top of the main file of your application (index.js for example) add: &lt;code&gt;require('dotenv').config()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in the root directory of your application&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the variables the app needs into this new file. For example: &lt;code&gt;DATABASE_CONNECTION_STRING=mongodb://localhost:27017&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;.env&lt;/code&gt; to your app's .gitignore file.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now replace any instance of the database connection string with &lt;code&gt;process.env.DATABASE_CONNECTION_STRING&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Adding &lt;code&gt;.env&lt;/code&gt; to your app's .gitignore file solves the second problem - exposing sensitive information. Now when you push your code to the repository the database connection string is not included.&lt;/p&gt;

&lt;p&gt;You still need to address how to use Environment Variables when your app is deployed. The production database will have a different URL. For example its connection string could be mongodb://xyz456-shard-00-00.ab123.mongodb.net:27017 &lt;/p&gt;

&lt;p&gt;In Heroku you can add this information with Heroku's Config Vars. Remember to use the same variable name - &lt;code&gt;DATABASE_CONNECTION_STRING&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's it! When your app is deployed to Heroku it will connect to the production database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;I hope you can see how effective Environment Variables will be for you in your code. They are quite easy to start using. But as with anything - the more you practise using them, the easier they become to use.&lt;/p&gt;

&lt;p&gt;Look through your code. Check for any places you are hardcoding values that are dependent on the environment. Some places to look are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;database connection - username, password, url&lt;/li&gt;
&lt;li&gt;api keys and ids&lt;/li&gt;
&lt;li&gt;path such as your app's url&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also use Environment Variables in your frontend code. The underlying principle is the same.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>heroku</category>
    </item>
    <item>
      <title>Proof of concept for How They Voted</title>
      <dc:creator>John Behan</dc:creator>
      <pubDate>Fri, 21 Aug 2020 16:40:52 +0000</pubDate>
      <link>https://dev.to/howtheyvoted/proof-of-concept-for-how-they-voted-dlk</link>
      <guid>https://dev.to/howtheyvoted/proof-of-concept-for-how-they-voted-dlk</guid>
      <description>&lt;h2&gt;
  
  
  A project I am working on - How They Voted
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; - Check out &lt;a href="https://howtheyvoted.ie"&gt;How They Voted&lt;/a&gt; and the &lt;a href="https://github.com/How-They-Voted-Ireland"&gt;How They Voted organisation on Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is an app to easily see how TDs (elected representatives in Ireland) have voted on issues in the Dáil (Irish parliament).&lt;/p&gt;

&lt;p&gt;It is very early stages now but the goal is to create something to allow users to easily answer the question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How did &lt;em&gt;{my-local-td}&lt;/em&gt; vote on that issue/bill/motion?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is what the mobile version of the app looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MlxsQj80--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://boxitoff.s3-eu-west-1.amazonaws.com/images/how-they-voted-v0-mobile.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MlxsQj80--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://boxitoff.s3-eu-west-1.amazonaws.com/images/how-they-voted-v0-mobile.jpg" class="image-center" title="Screenshot of mobile version of the How They Voted app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this is the desktop version:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qqkUam_a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://boxitoff.s3-eu-west-1.amazonaws.com/images/how-they-voted-v0-desktop.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qqkUam_a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://boxitoff.s3-eu-west-1.amazonaws.com/images/how-they-voted-v0-desktop.jpg" class="image-center" title="Screenshot of desktop version of the How They Voted app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Built with Svelte and Sapper
&lt;/h2&gt;

&lt;p&gt;I've been using Svelte and Sapper a lot lately - this site was recently migrated from Gatsby to these in a few days.&lt;/p&gt;

&lt;p&gt;The How They Voted app is built to run as a statically generated site using Sapper's &lt;code&gt;export&lt;/code&gt; script.&lt;/p&gt;

&lt;p&gt;It's hosted on Github Pages and is deployed on a merge to Master using Github Actions.&lt;/p&gt;

&lt;p&gt;I'm really happy with it. It is very early days now but the main page of the app has a Lighthouse score of 95 - Performance, 100 - Accessibility, 100 - Best Practices, 100 - SEO.&lt;/p&gt;

&lt;p&gt;The app is a PWA by default. I haven't done anything with this though yet, it's just built using the default Sapper settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using public APIs
&lt;/h2&gt;

&lt;p&gt;All the data used in the app is pulled from the oireachtas.ie servers unless stated as coming from elsewhere.&lt;/p&gt;

&lt;p&gt;The APIs provided by the team there are very good and any questions I have had have been answered in good time and always with extra info.&lt;/p&gt;

&lt;p&gt;I wasn't able to retrieve some data through the API so I created some scraping tools with Node and Puppeteer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idea and goals
&lt;/h2&gt;

&lt;p&gt;The main goal for this project is to give a non-partisan view of the public dealings of our government. I believe that the more engaged and informed we are of what is happening in government, the more we can influence what happens there and can hold our elected officials to account.&lt;/p&gt;

&lt;p&gt;I have a few ideas about where to go next with the app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better design&lt;/li&gt;
&lt;li&gt;TD stats - how they tend to vote, what issues they engage with, etc...&lt;/li&gt;
&lt;li&gt;Services to automate collection of data (currently happens manually)&lt;/li&gt;
&lt;li&gt;Peer reviewed contributions for naming, tagging, summarising votes and debates&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Contributing
&lt;/h2&gt;

&lt;p&gt;There is a lot of work needed to make this app achieve all its goals. These contributions will be for code, content, ideas, governance and probably a hundred other things I haven't thought of yet.&lt;/p&gt;

&lt;p&gt;It's my first time working on a project like this and I've regularly questioned my motives and the whole concept but it has also been fun to work on. I'm looking forward to seeing where this all goes :D&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>javascript</category>
      <category>architecture</category>
      <category>contributorswanted</category>
    </item>
    <item>
      <title>Using attributes in Rust to suppress warnings</title>
      <dc:creator>John Behan</dc:creator>
      <pubDate>Sun, 22 Mar 2020 10:25:27 +0000</pubDate>
      <link>https://dev.to/jjmax75/using-attributes-in-rust-to-suppress-warnings-2gbh</link>
      <guid>https://dev.to/jjmax75/using-attributes-in-rust-to-suppress-warnings-2gbh</guid>
      <description>&lt;p&gt;Warnings and errors are good. We want to be told when we're doing something wrong or something with the potential to cause problems in the future. But sometimes it's ok to turn off those warnings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turning off a warning in Rust at the code level
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
  let mut my_variable = 'foo';
  my_variable = 'bar';
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we try to compile this block of code we will see this warning -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;warning:&lt;/strong&gt; variable &lt;code&gt;my_variable&lt;/code&gt; is assigned to, but never used&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By adding &lt;code&gt;#[allow(unused_variables)]&lt;/code&gt; above the variable declaration we can suppress this warning&lt;/p&gt;

&lt;p&gt;But there is another warning -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;warning:&lt;/strong&gt; value assigned to &lt;code&gt;my_variable&lt;/code&gt; is never read&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can suppress this warning by adding  &lt;code&gt;#[allow(unused_assignments)]&lt;/code&gt; above the line that reassigns &lt;code&gt;my_variable&lt;/code&gt;. Or...&lt;/p&gt;

&lt;p&gt;If we add &lt;code&gt;#[allow(unused_variables, unused_assignments)]&lt;/code&gt; above the function. Our code now looks like this and gives no warnings when compiled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#[allow(unused_variables, unused_assignments)]
fn main() {
  let mut my_variable = "foo";
  my_variable = "bar";
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Suppressing warnings when developing can be useful as it reduces the amount of noise we need to filter out when we're trying something out or debugging. But it is important to turn these warnings back on and to address any underlying issues once our code is ready.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
    <item>
      <title>Managing Rust binary files and Git when using rustc</title>
      <dc:creator>John Behan</dc:creator>
      <pubDate>Mon, 16 Mar 2020 16:54:08 +0000</pubDate>
      <link>https://dev.to/jjmax75/managing-rust-binary-files-and-git-when-using-rustc-488g</link>
      <guid>https://dev.to/jjmax75/managing-rust-binary-files-and-git-when-using-rustc-488g</guid>
      <description>&lt;h2&gt;
  
  
  What is rustc?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;rustc&lt;/code&gt; is the compiler for Rust. When we use &lt;code&gt;cargo&lt;/code&gt; to compile our source files it is actually calling &lt;code&gt;rustc&lt;/code&gt; to do all the hard work.&lt;/p&gt;

&lt;p&gt;Don't believe me? :D Use this command to see the full output from a call to &lt;code&gt;cargo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cargo build --verbose&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rustc&lt;/code&gt; outputs the compiled binary file to the same directory as it is called in.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rustc hello.rs&lt;/code&gt; will create the binary file &lt;code&gt;hello&lt;/code&gt; in the same directory as our source file &lt;code&gt;hello.rs&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does Git come in to this?
&lt;/h2&gt;

&lt;p&gt;All good. But if we are using Git we don't want to commit these binary files to our repository. "Let's just use a .gitignore file" I hear you cry. Perfect but we can only git ignore files and directories by name not by type or anything else.&lt;/p&gt;

&lt;p&gt;A little trick we can use is to create a folder called &lt;code&gt;bin&lt;/code&gt; and git ignore that. Then use the following when compiling with &lt;code&gt;rustc&lt;/code&gt; - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;rustc --out-dir bin hello&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now our source files and binary files are kept apart and our binaries never make it into the Git repository.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>git</category>
    </item>
    <item>
      <title>Getting started with Rust on Mac</title>
      <dc:creator>John Behan</dc:creator>
      <pubDate>Sat, 14 Mar 2020 19:40:03 +0000</pubDate>
      <link>https://dev.to/jjmax75/getting-started-with-rust-on-mac-16co</link>
      <guid>https://dev.to/jjmax75/getting-started-with-rust-on-mac-16co</guid>
      <description>&lt;h2&gt;
  
  
  The Easy Way&lt;sup&gt;TM&lt;/sup&gt; to Install Rust (on a Mac)
&lt;/h2&gt;

&lt;p&gt;The easiest way to install Rust on a Mac is to head on over to &lt;a href="https://www.rust-lang.org/tools/install"&gt;https://www.rust-lang.org/tools/install&lt;/a&gt; and use the shell command there - &lt;/p&gt;

&lt;p&gt;&lt;code&gt;curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But...&lt;/p&gt;

&lt;p&gt;Sometimes we get the following error -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unknown SSL protocol error in connection to static.rust-lang.org&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes.  I got this on my work machine but not my personal one.&lt;/p&gt;

&lt;p&gt;You can read a bit about this issue here - &lt;a href="https://www.wormly.com/answers/sid/142/topicid/19"&gt;https://www.wormly.com/answers/sid/142/topicid/19&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a fairly straightforward solution&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fairly Easy Way&lt;sup&gt;TM&lt;/sup&gt; to Install Rust (on a Mac)
&lt;/h2&gt;

&lt;p&gt;(See Update below before trying this)&lt;/p&gt;

&lt;p&gt;We want to use &lt;code&gt;rustup&lt;/code&gt; so let's get that first.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install rustup&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next let's setup cargo and rustc,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rustup-init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally we can check our installation with - &lt;code&gt;rustc --version&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  UPDATE
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/jeikabu"&gt;jeikabu&lt;/a&gt; on Dev.to has given this tip to try.&lt;/p&gt;

&lt;p&gt;The old way of installing rustup - &lt;code&gt;curl sh.rustup.rs -sSf | sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I haven't tested this but it is worth trying before falling back on brew.&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
  </channel>
</rss>
