<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adi Iyengar on Medium]]></title>
        <description><![CDATA[Stories by Adi Iyengar on Medium]]></description>
        <link>https://medium.com/@aditya7iyengar?source=rss-dccf8ff5238a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Xtfj07s9vnpQksNRO3idow.png</url>
            <title>Stories by Adi Iyengar on Medium</title>
            <link>https://medium.com/@aditya7iyengar?source=rss-dccf8ff5238a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 04:26:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aditya7iyengar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Elixir: Architecting API Clients]]></title>
            <link>https://medium.com/@aditya7iyengar/elixir-architecting-api-clients-5fd3cf72898?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fd3cf72898</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[api-client]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[httpoison]]></category>
            <category><![CDATA[https]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Thu, 16 Aug 2018 20:04:48 GMT</pubDate>
            <atom:updated>2018-08-16T20:04:48.067Z</atom:updated>
            <content:encoded><![CDATA[<p>Over the past two years, I have been working a lot with APIs in elixir and have written quite a few API clients. I thought it would be nice to share it with the community on the Annkissam’s blog. I go over various design patterns (like <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a>, <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">Separation of Concerns</a> etc) and how we can use them to make our API clients better, modular and easily extendible. I also talk about ways to test them while sticking to the principles outline in Jose’s post about <a href="http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/">mocks and explicit contracts</a>. Here is a <a href="https://www.annkissam.com/elixir/alembic/posts/2018/08/15/architecting-api-clients.html">complete blog post</a> walking you through writing an API Client for the <a href="https://www.freshbooks.com/developers">Freshbooks API</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fd3cf72898" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL Subscriptions with Phoenix, Absinthe and WebSockets]]></title>
            <link>https://medium.com/@aditya7iyengar/graphql-subscriptions-with-phoenix-absinthe-and-websockets-7d5e41354130?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d5e41354130</guid>
            <category><![CDATA[phoenix]]></category>
            <category><![CDATA[absinthe]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[websocket]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Sat, 14 Jul 2018 14:00:55 GMT</pubDate>
            <atom:updated>2018-07-14T14:00:55.234Z</atom:updated>
            <content:encoded><![CDATA[<p>At Annkissam, we have been building Elixir apps running on production since 2015. In a push towards moving our internal suite of apps to elixir, we have been revisiting ways to solve many problems. One of those problems is communication between a constellation of apps in a Service Based Architecture. We decided to use <a href="https://github.com/absinthe-graphql/absinthe">Absinthe</a> and WebSockets to accomplish that, and also recently made a bunch of that work open-source in the form of two hex packages: <a href="https://github.com/annkissam/common_graphql_client">common_graphql_client</a> and <a href="https://github.com/annkissam/absinthe_websocket">absinthe_websocket</a>. These libraries make using Absinthe with websockets, and inter-app communication a cinch. My colleague, Eric Sullivan explains how to use these two packages in this <a href="https://annkissam.com/elixir/alembic/posts/2018/07/13/graphql-subscriptions-connecting-phoenix-applications-with-absinthe-and-websockets.html">walkthrough</a>. Please check it out and let us know your thoughts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d5e41354130" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir Tip #6: Calling Sudo Commands]]></title>
            <link>https://medium.com/@aditya7iyengar/elixir-tip-6-calling-sudo-commands-43aa3cf78324?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/43aa3cf78324</guid>
            <category><![CDATA[askpass]]></category>
            <category><![CDATA[zenity]]></category>
            <category><![CDATA[system-cmd]]></category>
            <category><![CDATA[sudo]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Wed, 11 Jul 2018 23:59:40 GMT</pubDate>
            <atom:updated>2018-07-11T23:59:40.888Z</atom:updated>
            <content:encoded><![CDATA[<p>In Elixir, to interact with the host system or the virtual machine, we use the module <a href="https://hexdocs.pm/elixir/System.html">System</a>. In order to run a command on the system, we can use the function <a href="https://hexdocs.pm/elixir/System.html#cmd/3">System.cmd/3</a>:</p><pre># This function returns the calls mix deps.get command System.cmd(&quot;mix&quot;, [&quot;deps.get&quot;])</pre><p>System.cmd/3 is a great way to run a command from elixir layer, but it has some hurdles while trying to use a sudo command. If you try to call a sudo command with System.cmd/3 it gives the following error:</p><pre>System.cmd(&quot;sudo&quot;, [&quot;su&quot;]) <br># Gives this output: <br># sudo: no tty present and no askpass program specified</pre><h3>Creating an askpass program</h3><p>In order to work around this we need to provide an askpass script, which can prompt the users for their sudo password. There are any askpass applications available, but one of the most common ones is GNOME’s <a href="https://github.com/GNOME/zenity">Zenity</a>. Using zenity, we can ask for password using the command: $ zenity --password --title=Authentication.</p><p><em>I use </em><em>zenity for my scripts because it has a working build for all the Operating Systems that I use (Arch Linux, Ubuntu, Dedian, Mac OSX, Pop!_OS, Fedora, CentOS and more..)</em></p><p>To install zenity run the following commands:</p><p>For an arch_linux user like me, run: $ sudo pacman -S zenity --noconfirm</p><p>For an ubuntu or pop_os user, run: $ sudo apt-get -y install zenity</p><p>For a mac user, run: $ brew install zenity (Make sure you have Homebrew installed)</p><p>For a fedora or cent_os user, run: yum install zenity</p><p>Now that we have installed zenity, we’re ready to use it.</p><p>So, let’s create a file askpass.sh with the following contents:</p><pre>#!/bin/bash <br>zenity --password --title=Authentication</pre><p>Now that we have a program which prompts for user password, we can use this while calling the sudo command with System.cmd/3.</p><h3>Using askpass.sh with System.cmd/3</h3><p>System.cmd/3 takes a Keyword as third parameter, which accepts the env key. This allows us to pass command-specific environment variables to the call. We can use this feature to pass the environment variable, SUDO_ASKPASS. This variable should point to the askpass.sh file, which is the program that asks for password.</p><p>One more thing: We will need to call the sudo command with -A option, which specifies that it needs to use the askpass program defined by the SUDO_ASKPASS environment variable.</p><p>I will also be adding another option, into: IO.stream(:stdio, :line) so we can see the output of the sudo command line-by-line.</p><p>The final call looks like this:</p><pre>System.cmd(&quot;sudo&quot;, [&quot;-A&quot; , &quot;su&quot;], <br>  env: [{&quot;SUDO_ASKPASS&quot;, &quot;./askpass.sh&quot;}], <br>  into: IO.stream(:stdio, :line))</pre><p>Once invoked, this command will open a zenity window which prompts for the password and once entered, it will continue with the command’s execution.</p><p><em>For a more advanced usage of this check out my project, </em><a href="https://github.com/aditya7iyengar/adify/blob/master/adifier/lib/adifier/invoker.ex"><em>adify</em></a><em>. This project sets up a machine with </em><em>asdf, </em><em>erlang, </em><em>elixir and all the tools that I use (and uses the above trick to do that).</em></p><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180711/calling-sudo-commands-from-elixir"><em>www.adiiyengar.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43aa3cf78324" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir Tip #5: IEx shell history]]></title>
            <link>https://medium.com/@aditya7iyengar/elixir-tip-5-iex-shell-history-bf466bfad1b3?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf466bfad1b3</guid>
            <category><![CDATA[iex]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[elixir-tip]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Wed, 11 Jul 2018 13:23:45 GMT</pubDate>
            <atom:updated>2018-07-11T13:23:45.998Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the top features in OTP 20 is shell history persistence. So in the Eshell, you can access history from a previous shell by pressing ↑ and ↓ arrow keys.</p><h3>Enable Shell History</h3><p>This feature can be enabled by adding/updating the environment variable, ERL_AFLAGS. ERL_AFLAGS is one of the environment variables supported by ERTS. “The content of this variable is added to the beginning of the command line for erl.”</p><p>So, we can export that environment variable:</p><pre># in ~/.bashrc (or ~/.zshrc) <br>export ERL_AFLAGS=&quot;-kernel shell_history enabled&quot;</pre><p>Now, after running source command on the rc file, both erl and iex sessions will persist their shell history.</p><p>For more information visit <a href="https://hexdocs.pm/iex/IEx.html#module-shell-history">this page</a></p><h3>erl &amp; iex catch</h3><p>One thing to keep in mind is that the shell_history is shared between iex and erl. So, if you’re using both erlang and elixir, it could be a problem:</p><pre>$ erl<br>Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]<br><br>Eshell V10.0  (abort with ^G)<br>1&gt; 1+1.<br>2<br>BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded<br>       (v)ersion (k)ill (D)b-tables (d)istribution<br>^C%<br>$ iex<br>Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]<br><br>Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)<br>iex(1)&gt; 1+1. % --- AFTER PRESSING ↑</pre><p>It’s something to keep in mind. Currently, I don’t see a way of separating ways to overcome this. Maybe a flag in ERL_AFLAGS for &quot;-kernel shell_history_file &lt;filename&gt;&quot; where you can provide a shell_history_file can be a way of keeping erl and iex history separate.</p><h3>Configure shell history file size</h3><p>OTP 20 also comes with options for increasing (or decreasing) shell history file size, in order to increase the limit of history that can be stored. This value can be set by using the option shell_history_file_bytes and the value for which is the number of bytes that erl shell should remember.</p><p>Default number of bytes is 524288 bytes, which is 512kb. I like having this file up to 2mb. I think size beyond this is hard to manage, which also slows down the history access.</p><p>This can be done by exporting ERL_AFLAGS:</p><pre># in ~/.bashrc (or ~/.zshrc) <br>export ERL_AFLAGS=&quot;-kernel shell_history enabled -kernel shell_history_file_bytes 2097152&quot;</pre><p>This enables my erl shell to store history worth 2mb (which is the same as 2097152 bytes)</p><h3>Getting iex and erl history</h3><p>Erlang’s erl provides a way to view the shared history. This can be done by using kernel’s h() command.</p><pre>$ erl<br>Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]<br><br>Eshell V10.0  (abort with ^G)<br>1&gt; 1+1.<br>2<br>2&gt; 2+2.<br>4<br>3&gt; h().<br>1: 1 + 1<br>-&gt; 2<br>2: 2 + 2<br>-&gt; 4<br>ok<br>4&gt;</pre><p>This is a handy command when you’re trying to use a few commands a bunch of times for erl testing.</p><p>Have fun coding with OTP 20 and iex!</p><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180503/iex-shell-history"><em>www.adiiyengar.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf466bfad1b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir Tip #4: Global .iex.exs file]]></title>
            <link>https://medium.com/@aditya7iyengar/elixir-tip-4-global-iex-exs-file-6871bd9bd087?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6871bd9bd087</guid>
            <category><![CDATA[configuration]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[elixir-tip]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Wed, 11 Jul 2018 13:20:02 GMT</pubDate>
            <atom:updated>2018-07-11T13:20:02.199Z</atom:updated>
            <content:encoded><![CDATA[<p>Before an iex shell starts, it looks like a file .iex.exs. It starts looking at the current directory and if it doesn’t find one, it looks at a global one at $HOME/.iex.exs (usually ~/.iex.exs). All the code inside the file is loaded and added to the IEx shell’s context, so all the modules imported, aliases, required, and bindings from the file are accessible inside the shell.</p><p>In my .iex.exs, I do the following:</p><ul><li>Enable ansi for better characters support.</li><li>Add message_queue to the prompt. I like playing with multiple iex nodes and it’s handy to monitor inter-node communication.</li><li>Improve history_limit and inspect_limit.</li><li>Make eval results bright.</li><li>Add Plug support.</li><li>Add Phoenix support and detect phoenix apps.</li><li>Add Ecto support and detect ecto app.</li><li>Alias the main ecto Repo module.</li></ul><p><em>NOTE: As I do more elixir, I keep improving my </em><em>.iex.exs</em></p><p>Here is my global iex.exs file.</p><pre># Will be using `ANSI`<br>Application.put_env(:elixir, :ansi_enabled, true)<br><br># Letting people know what iex.exs they are using<br>IO.puts IO.ANSI.magenta_background() &lt;&gt; IO.ANSI.bright() &lt;&gt; IO.ANSI.underline()<br>        # &lt;&gt; IO.ANSI.blink_slow()<br>        &lt;&gt; &quot;Using global .iex.exs (located in ~/.iex.exs)&quot; &lt;&gt; IO.ANSI.reset()<br><br># Get queue length for the IEx process<br># This is fun to see while playing with nodes<br>queue_length = fn -&gt;<br>  self()<br>  |&gt; Process.info()<br>  |&gt; Keyword.get(:message_queue_len)<br>end<br><br>prefix = IO.ANSI.light_black_background() &lt;&gt; IO.ANSI.green() &lt;&gt; &quot;%prefix&quot;<br>         &lt;&gt; IO.ANSI.reset()<br>counter = IO.ANSI.light_black_background() &lt;&gt; IO.ANSI.green() &lt;&gt; &quot;-%node-(%counter)&quot;<br>          &lt;&gt; IO.ANSI.reset()<br>info = IO.ANSI.light_blue() &lt;&gt; &quot;✉ #{queue_length.()}&quot; &lt;&gt; IO.ANSI.reset()<br>last = IO.ANSI.yellow() &lt;&gt; &quot;➤➤➤&quot; &lt;&gt; IO.ANSI.reset()<br>alive = IO.ANSI.bright() &lt;&gt; IO.ANSI.yellow() &lt;&gt; IO.ANSI.blink_rapid() &lt;&gt; &quot;⚡&quot;<br>        &lt;&gt; IO.ANSI.reset()<br><br>default_prompt = prefix &lt;&gt; counter &lt;&gt; &quot; | &quot; &lt;&gt; info &lt;&gt; &quot; | &quot; &lt;&gt; last<br>alive_prompt = prefix &lt;&gt; counter &lt;&gt; &quot; | &quot; &lt;&gt; info &lt;&gt; &quot; | &quot; &lt;&gt; alive &lt;&gt; last<br><br>inspect_limit = 5_000<br>history_size = 100<br><br>eval_result = [:green, :bright]<br>eval_error = [:red, :bright]<br>eval_info = [:blue, :bright]<br><br># Configuring IEx<br>IEx.configure [<br>  inspect: [limit: inspect_limit],<br>  history_size: history_size,<br>  colors: [<br>    eval_result: eval_result,<br>    eval_error: eval_error,<br>    eval_info: eval_info,<br>  ],<br>  default_prompt: default_prompt,<br>  alive_prompt: alive_prompt<br>]<br><br># Phoenix Support<br>import_if_available Plug.Conn<br>import_if_available Phoenix.HTML<br><br>phoenix_app = :application.info()<br>  |&gt; Keyword.get(:running)<br>  |&gt; Enum.reject(fn {_x, y} -&gt;<br>    y == :undefined<br>  end)<br>  |&gt; Enum.find(fn {x, _y} -&gt;<br>    x |&gt; Atom.to_string() |&gt; String.match?(~r{_web})<br>  end)<br><br>  # Check if phoenix app is found<br>case phoenix_app do<br>  nil -&gt; IO.puts &quot;No Phoenix App found&quot;<br>  {app, _pid} -&gt;<br>    IO.puts &quot;Phoenix app found: #{app}&quot;<br>    ecto_app = app<br>      |&gt; Atom.to_string()<br>      |&gt; (&amp;Regex.split(~r{_web}, &amp;1)).()<br>      |&gt; Enum.at(0)<br>      |&gt; String.to_atom()<br><br>    exists = :application.info()<br>      |&gt; Keyword.get(:running)<br>      |&gt; Enum.reject(fn {_x, y} -&gt;<br>        y == :undefined<br>      end)<br>      |&gt; Enum.map(fn {x, _y} -&gt; x end)<br>      |&gt; Enum.member?(ecto_app)<br><br>    # Check if Ecto app exists or running<br>    case exists do<br>      false -&gt; IO.puts &quot;Ecto app #{ecto_app} doesn&#39;t exist or isn&#39;t running&quot;<br>      true -&gt;<br>        IO.puts &quot;Ecto app found: #{ecto_app}&quot;<br><br>        # Ecto Support<br>        import_if_available Ecto.Query<br>        import_if_available Ecto.Changeset<br><br>        # Alias Repo<br>        repo = ecto_app |&gt; Application.get_env(:ecto_repos) |&gt; Enum.at(0)<br>        quote do<br>          alias unquote(repo), as: Repo<br>        end<br>    end<br>end</pre><p><em>Let me know if you have ways in which this can be further improved..</em></p><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180709/my-iex-exs"><em>www.adiiyengar.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6871bd9bd087" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir Releases: Solving Build-time vs Run-time env problem with Akd]]></title>
            <link>https://medium.com/@aditya7iyengar/solving-build-time-vs-run-time-env-problem-with-akd-ac573bcc9f3d?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac573bcc9f3d</guid>
            <category><![CDATA[akd]]></category>
            <category><![CDATA[distillery]]></category>
            <category><![CDATA[phoenix]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Thu, 05 Jul 2018 16:14:34 GMT</pubDate>
            <atom:updated>2018-07-05T16:19:41.540Z</atom:updated>
            <content:encoded><![CDATA[<p>I have been deploying/running production applications for about two years now and the first problem that comes to mind while deploying OTP releases is Build-time vs Run-time environment configuration. While running a Mix project there isn’t much of a difference between build-time and run-time as you are running the app in the same environment in which you’re compiling (building) it. However, that might not be the case when using releases. At Annkissam, we have come up with a few solutions to that problem which I explain better in <a href="https://www.annkissam.com/elixir/resources/using-akd-solve-build-time-vs-run-time-environment-problem">this</a> post.</p><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180701/using-akd-to-solve-buildtime-vs-runtime-problem"><em>https://www.adiiyengar.com/blog/20180701/using-akd-to-solve-buildtime-vs-runtime-problem</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac573bcc9f3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run TCP Services in Docker]]></title>
            <link>https://medium.com/@aditya7iyengar/run-tcp-services-in-docker-ec6926668f1f?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec6926668f1f</guid>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Tue, 26 Jun 2018 16:03:15 GMT</pubDate>
            <atom:updated>2018-06-26T16:03:15.358Z</atom:updated>
            <content:encoded><![CDATA[<p>Most of the dynamic web applications depend on databases like mysql, postgres, mongodb etc. However, setting up an environment (or a server) to work with those services can be a pain. And if you add periodic upgrades, version management and OS-specific package manager issues, it could really be a nightmare!</p><p>One solution to this problem is Containerization. This might not be the best solution for high-volume production apps, but for most of the apps (and at least for dev environment), it can be a life saver.</p><p>In this post, we will be using <a href="https://docs.docker.com/">docker</a> as our Containerization Engine to set up a Postgres Service.</p><p><em>For instructions on installing docker, check out </em><a href="https://docs.docker.com/docker-for-mac/install/"><em>this page</em></a></p><h3>Pull Postgres Image</h3><p>In order to run postgres inside a Docker container, you will need to use an image. There are two ways to get that image:</p><ul><li>Pull a trusted, maintained and official postgres image from DockerHub.</li><li>Pull a base OS image (say CentOS) and run a series of commands on it to work with postgres.</li></ul><p>In order pull an image from DockerHub, Here, I will be pulling the official postgres image from DockerHub:</p><p><em>Linux Users must use </em><em>sudo with </em><em>docker</em></p><p>$ docker pull postgres:9.3.23</p><p>This will take a couple minutes to pull postgres image with tag, 9.3.23 from DockerHub.</p><p><em>It’s important to use tags here, because docker pulls the latest image if a tag isn’t specified. You don’t want to use an updated postgres without knowledge.</em></p><h3>Running the Postgres Container</h3><p>Now that we have the postgres image, we can create and run a container using that image. For a container which is expected to be “long running”, we typically use docker run with -d (detached) option. This allows us to create a stopped container which can be ran in the background using docker start.</p><p>Moreover, we want to be able to access the container’s port from host machine which is why we also provide -p option, which allows us to do port forwarding from a docker container to the host. Since postgres runs on the port 5432, we want to expose that port from the container and forward it to host’s 5432 port.</p><p>We’re also providing a name to the container which will allow us to perform operations on that container without having to know it’s hashed id.</p><p>Optionally, we can also provide an environment variable to the container POSTGRES_PASSWORD which sets the password to the user postgres.</p><p>So, to launch the postgres container, use the following command:</p><p>$ docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=******* -d postgres:9.3.23</p><p>We can now start this container by typing:</p><p>$ docker start postgres</p><p>To test if postgres container is running as expected we can launch psql:</p><pre>$ docker exec -it postgres psql -Upostgres -W <br>Password for user postgres: ******** <br>psql (9.3.23) Type &quot;help&quot; for help. </pre><pre>postgres=#</pre><p>Now, we have a working containerized postgres service.</p><h3>Data Persistence</h3><p>When running a postgres container, we most likely want to persist the data. In order to do so across multiple container instances, we can use <a href="https://docs.docker.com/storage/volumes/">Volumes</a>.</p><p>First, let’s stop and remove the current container:</p><p>$ docker stop postgres &amp;&amp; docker rm postgres</p><p>Now, we can create a container which uses a location on the host machine to use as a volume which will be mounted in the postgres container to a location where it stores data (by default: /var/lib/postgresql/data). This can be done by using the -v option:</p><p>$ docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=******* -d -v /tmp/postgresdata:/var/lib/postgresql/data postgres:9.3.23</p><p>This command will create a directory named postgresdata in /tmp folder.</p><p>Now, we have a container which persists data to a host volume, that can be used to create another container.</p><h3>Start postgres on System Reboot</h3><p>Docker provides a neat way of restarting a container with different policies. Check out <a href="https://docs.docker.com/config/containers/start-containers-automatically/">this page</a> for more details.</p><p>I like keeping my postgres container running unless I have explicitly stopped it. So, I use restart unless-stopped policy.</p><p>So, our final command should look something like this:</p><p>$ docker run --name postgres --restart unless-stopped -p 5432:5432 -e POSTGRES_PASSWORD=******* -d -v /tmp/postgresdata:/var/lib/postgresql/data postgres:9.3.23</p><h3>Conclusion</h3><p>This kind of a set up can be used for mysql, phpadmin and many other services. I use docker for all my tools that use tcp. It makes managing these tools a lot easier as all I have to maintain is docker, and everything else is handled by the maintainers of the image that I’m using. It also works on multiple operating systems (as long as they support docker).</p><p><em>Don’t forget to alias the final </em><em>run command :)</em></p><p>Have fun Dockerizing!</p><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180624/run-tcp-services-in-docker"><em>https://www.adiiyengar.com/blog/20180624/run-tcp-services-in-docker</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec6926668f1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir Tip #3: Open Files with Vim from iex]]></title>
            <link>https://medium.com/@aditya7iyengar/elixir-tip-3-open-files-with-vim-from-iex-87db76d543fc?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/87db76d543fc</guid>
            <category><![CDATA[programming-tips]]></category>
            <category><![CDATA[vim]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[neovim]]></category>
            <category><![CDATA[erlang]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Sun, 24 Jun 2018 12:46:48 GMT</pubDate>
            <atom:updated>2018-06-24T12:46:48.424Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the less known (but useful) features of Elixir’s IEx module is the open/1 function. Open works with either a given Module, Module.function/arity or {file, line_number}. Once configured, it allows the developers to open a file with a given file path and line number. This is incredibly useful while debugging inside a IEx.pry session as it allows you to jump to the place where a particular function is defined.</p><p>For example:</p><pre>iex&gt; open String.to_atom/1</pre><p>Calling open as above will open the file corresponding to the String module where to_atom is defined.</p><p>To accomplish this IEx.Helpers module uses the environment variable, ELIXIR_EDITOR. If the environment variable isn’t exported, it uses EDITOR as a fallback.</p><p>However, by default it attempts to open the editor while keeping the iex session alive. So, this command doesn’t work with vim or nvim right off the bat.</p><h3>Configuring open/1 to work with Vim (and Neovim)</h3><p>I will be configuring open/1 to work with nvim (my primary text editor). But, a similar configuration can be used for vim and other terminal-based editors.</p><p>Let’s start by adding exporting the ELIXIR_EDITOR environment variable.</p><pre><em># in ~/.bashrc (or ~/.zshrc)</em><br><br>export ELIXIR_EDITOR<strong>=</strong>&quot;nvim&quot;</pre><p>But this won’t work. In order for open/1 to work with nvim, we need to satisfy two cases:</p><ul><li>open/1 should attempt to open nvim in a separate window. This will keep the IEx session alive while giving us the ability to open a file.</li><li>open/1 attempts to open a file using file:line notation, which doesn’t work with nvim. So, we will have to configure the format of that notation.</li></ul><p>For the first case, we can just open our nvim in a new terminal as follows:</p><p><em>Linux Users (with gnome-terminal)</em></p><pre><em># in ~/.bashrc (or ~/.zshrc)</em><br><br>export ELIXIR_EDITOR<strong>=</strong>&quot;gnome-terminal -- nvim&quot;</pre><p><em>Mac Users</em></p><pre><em># in ~/.bashrc (or ~/.zshrc)</em><br><br>export ELIXIR_EDITOR<strong>=</strong>&quot;open -a Terminal &#39;nvim&#39;&quot;</pre><p>This should allow us to call open, but we still have to configure the editor command to take a line_number. Elixir provides interpolation for __FILE__ and __LINE__.</p><p>We can use them to open a file with a specific line_number in a Neovim window as follows:</p><p><em>Linux Users (with gnome-terminal)</em></p><pre><em># in ~/.bashrc (or ~/.zshrc)</em><br><br>export ELIXIR_EDITOR<strong>=</strong>&quot;gnome-terminal -- nvim +__LINE__ __FILE__&quot;</pre><p><em>Mac Users</em></p><pre><em># in ~/.bashrc (or ~/.zshrc)</em><br><br>export ELIXIR_EDITOR<strong>=</strong>&quot;open -a Terminal &#39;nvim +__LINE__ __FILE__&#39;&quot;</pre><p>Now running and iex and calling open/1 should open a file with a specific line number.</p><h3>With Tmux</h3><p>Since I use tmux, I changed my final version of the ELIXIR_EDITOR env to look like this:</p><p><em>Linux Users (with gnome-terminal)</em></p><pre><em># in ~/.bashrc (or ~/.zshrc)</em><br><br>export ELIXIR_EDITOR<strong>=</strong>&quot;gnome-terminal tmux new -- nvim +__LINE__ __FILE__&quot;</pre><p><em>Mac Users</em></p><pre><em># in ~/.bashrc (or ~/.zshrc)</em><br><br>export ELIXIR_EDITOR<strong>=</strong>&quot;open -a Terminal &#39;tmux new nvim +__LINE__ __FILE__&#39;&quot;</pre><p>This allows me to open a file in nvim with tmux configurations.</p><p>Here is the finished product:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*GUmmP2faYbCZKj5h.gif" /><figcaption><em>File-Jump Heaven</em></figcaption></figure><p>This also works with open/0 which opens the current prying location inside a prying session started by either calling IEx.pry/0 or IEx.break!/4.</p><p>Check out <a href="https://hexdocs.pm/iex/IEx.Helpers.html#open/1">IEx.Helpers.open/1</a> for more information.</p><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180623/iex-open-files-with-vim"><em>https://www.adiiyengar.com/blog/20180623/iex-open-files-with-vim</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87db76d543fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir Tip #1: Mix Test Watch Stale]]></title>
            <link>https://medium.com/@aditya7iyengar/mix-test-watch-stale-170e1550b6f4?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/170e1550b6f4</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Tue, 19 Jun 2018 14:31:36 GMT</pubDate>
            <atom:updated>2018-06-24T10:26:57.170Z</atom:updated>
            <content:encoded><![CDATA[<p>Mix comes with a good set of tools, but it’s true power can be seen when we try to configure it. Mix makes it awfully easy to provide some basic configurations and overrides. One such configuration is aliases.</p><p>In this blog, we’re going to use aliases to make our TDD lives easier while using a mix project.</p><p>$ mix test is the main task which runs all the tests for a mix project. It takes a command line option, --stale which runs only the tests which references the modules that changed since last $ mix test --stale call. You can read more about it <a href="https://hexdocs.pm/mix/Mix.Tasks.Test.html#module-stale">here</a>. It also accepts the option, --listen-on-stdin which listens on stdin while running the tests. When running tests with this option, tests are run again when it receives a newline. We can use these options in combination to give us the ability to run stale tests with just one keystroke: $ mix test --stale --listen-on-stdin.</p><p>Now, we want to integrate this with our file system, such that whenever we change “relevant” files, stale tests get run automatically without even a single keystroke.</p><p>To accomplish this we can write a custom alias in the mix.exs file:</p><p><em>This example is for an umbrella app, but can be modified for a non-umbrella too.</em></p><pre># in mix.exs</pre><pre>defmodule MyUmbrella.Mixfile do<br>  use Mix.Project</pre><pre>def project do<br>    [<br>      apps_path: &quot;apps&quot;,<br>      start_permanent: Mix.env() == :prod,<br>      deps: deps(),<br>      aliases: aliases()<br>    ]<br>  end</pre><pre>defp deps do<br>    []<br>  end</pre><pre>defp aliases do<br>    [<br>      &quot;test.watch.stale&quot;: &amp;test_watch_stale/1,<br>    ]<br>  end</pre><pre># This function runs a system command with a watcher depending upon the<br>  # Operating System.<br>  def test_watch_stale(_) do<br>    System.cmd(<br>      &quot;sh&quot;,<br>      [&quot;-c&quot;, &quot;#{get_system_watcher()} apps/ | mix test --stale --listen-on-stdin&quot;],<br>      into: IO.stream(:stdio, :line)<br>    )<br>  end</pre><pre># Works only for Mac and Linux<br>  defp get_system_watcher do<br>    case System.cmd(&quot;uname&quot;, []) do<br>      {&quot;Linux\n&quot;, 0} -&gt; &quot;inotifywait -e modify -e create -e delete -mr&quot; # For Linux systems inotify should work<br>      {&quot;Darwin\n&quot;, 0} -&gt; &quot;fswatch&quot; # For Macs, fswatch comes directly installed<br>      {kernel, 0} -&gt; raise &quot;Watcher not supported on kernel: #{kernel}&quot;<br>    end<br>  end<br>end</pre><p>By defining a custom alias test.watch.stale and delegating it to the function, test_watch_stale/1, we are adding the ability to call $ mix test.watch.stale from the command line which calls the function test_watch_stale/1 with the list of arguments we pass from the command line (in this case none, so an empty list).</p><p>test_watch_stale function calls mix test --stale --listen-on-stdin piped from a watcher function which spits an stdin whenever an event is triggered, which in this case is when apps/ folder is modified.</p><p>get_system_watcher function gets the system watcher commands for different operating systems. Macs come preinstalled with fswatch, so this should work on a mac right away. Linux systems will have to install inotify-tools for this to work.</p><p><em>I decided not to support Windows for now, but there are file watcher binaries like </em><a href="https://facebook.github.io/watchman/docs/install.html"><em>watchman</em></a><em> that work on all OS.</em></p><p>Now, we can follow the perfect TDD where our changes to the file system trigger relevant tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*ZwUEiE5ehdqIWnkp.gif" /><figcaption>TDD Heaven</figcaption></figure><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180607/mix-test-watch-stale"><em>https://www.adiiyengar.com/blog/20180607/mix-test-watch-stale</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=170e1550b6f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir Tip #2: Mix Xref to Enforce better Design]]></title>
            <link>https://medium.com/@aditya7iyengar/using-xref-to-enforce-better-design-132911b3c3b2?source=rss-dccf8ff5238a------2</link>
            <guid isPermaLink="false">https://medium.com/p/132911b3c3b2</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <dc:creator><![CDATA[Adi Iyengar]]></dc:creator>
            <pubDate>Tue, 19 Jun 2018 14:28:54 GMT</pubDate>
            <atom:updated>2018-06-24T10:27:26.971Z</atom:updated>
            <content:encoded><![CDATA[<p>Elixir is one of my favorite languages! Dealing in functions, pipes, Process-Oriented programming, OTP and a great community make working in Elixir a great experience. However, there isn’t a better workflow/means to limit access control across modules (for example: protected in ruby). Because of this deficiency, I often find myself working in a project where apps try to make calls to modules that they shouldn’t.</p><p>One classic example that comes to mind is a phoenix app in an umbrella. Often times people make calls to the Repo in the ecto app from Phoenix controllers. In phoenix 1.3, <a href="https://hexdocs.pm/phoenix/contexts.html#content">Contexts</a> were introduced which add an implicit layer of abstraction around a set of database operations. The idea was to use these contexts as an API to make changes to the database. This was a great step forward and a clean solution to the above problem, besides the fact that you could still make calls to the Repo. So, Using contexts as means of communication with the database wasn’t enforced and I didn’t like that.</p><p>Recently, I came across mix xref task which can be used to do cross reference checks between the modules of a mix project. So, I did the obvious, use it to enforce better design for my applications.</p><p>Let’s take an umbrella of apps, my_app_umbrella with apps: my_app and my_app_web.</p><p>Here my_app is the ecto app which has MyApp.Repo defined and my_app_web is the phoenix app.</p><p>We can create a new mix task which checks for cross reference and raises an error if apps other than my_app/ try to reference MyApp.Repo.</p><pre># in mix.exs of the umbrella project.</pre><pre>defmodule MyApp.Umbrella.Mixfile do<br>  use Mix.Project<br><br>  @allowed_xrefs [{MyApp.Repo, [~r{apps/my_app/}]}]<br><br>  def project do<br>    [<br>      apps_path: &quot;apps&quot;,<br>      start_permanent: Mix.env == :prod,<br>      deps: deps(),<br>      aliases: aliases()<br>    ]<br>  end<br><br>  defp deps do<br>    []<br>  end<br><br>  defp aliases do<br>    [<br>      &quot;test.xref&quot;: &amp;test_xref/1,<br>    ]<br>  end<br><br>  defp test_xref(_) do<br>    Enum.each(@allowed_xrefs, fn {callee, callers} -&gt;<br>      case System.cmd(&quot;sh&quot;, [&quot;-c&quot;, &quot;mix xref callers #{callee}&quot;]) do<br>        {result, 0} -&gt;<br>          check_allowed(callers, callee, result)<br>        _ -&gt; raise &quot;Unexpected Error&quot;<br>      end<br>    end)<br>  end<br><br>  defp check_allowed(callers, callee, string) do<br>    string<br>    |&gt; String.split(&quot;==&gt;&quot;)<br>    |&gt; Enum.map(&amp;String.trim/1)<br>    |&gt; Enum.filter(&amp; &amp;1 != &quot;&quot;)<br>    |&gt; Enum.each(&amp;check_allowed_for_app(&amp;1, callee, callers))<br><br>    require Logger<br>    Logger.info(&quot;All ok&quot;)<br>  end<br><br>  def check_allowed_for_app(app_string, callee, callers) do<br>    [app | files] = String.split(app_string, &quot;\n&quot;)<br><br>    case (files<br>      |&gt; Enum.map(&amp; &quot;apps/#{app}/&quot; &lt;&gt; &amp;1)<br>      |&gt; Enum.filter(&amp; !match_callers?(callers, &amp;1))) do<br>      [] -&gt; :ok<br>      files -&gt; raise &quot;Tried to access #{callee} from: <br>               #{Enum.join(files, &quot;, &quot;)}.. Not allowed!&quot;<br>    end<br>  end<br><br>  def match_callers?(callers, file) do<br>    Enum.any?(callers, &amp;String.match?(file, &amp;1))<br>  end<br>end</pre><p>Here the module attribute, @allowed_xrefs, is a list of tuples. First element of the tuple is a callee (the module whose access we want to control) and the second element of the tuple is a list of callers (list of regexps of files which are allowed to access the callee). In this example, callee is MyApp.Repo and callers include ~r{apps/my_app/}. This means only files in the app my_app can make calls to the callee, MyApp.Repo.</p><p>This allows us to run $ mix test.xref to check if there are any files calling modules that we have limited access to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*dCCsb4Nn7xKferrC.gif" /><figcaption>DDD Heaven</figcaption></figure><p>This task can further be extended to functions of a callee, to limit calls to a particular function in a module.</p><p>Checkout <a href="https://hexdocs.pm/mix/Mix.Tasks.Xref.html#content">mix xref</a> for more information on the task and to extend the usage of $ mix test.xref.</p><p><em>Originally published at </em><a href="https://www.adiiyengar.com/blog/20180618/using-xref-to-enforce-better-design"><em>https://www.adiiyengar.com/blog/20180618/using-xref-to-enforce-better-design</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=132911b3c3b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>