<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Phoenix &#8211; Other Things</title>
	<atom:link href="https://blog.adamzolo.com/category/phoenix/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.adamzolo.com</link>
	<description>Blog about Things by Adam Zolotarev</description>
	<lastBuildDate>Mon, 29 Jan 2018 00:53:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Elixir Phoenix Cache</title>
		<link>https://blog.adamzolo.com/elixir-phoenix-cache/</link>
					<comments>https://blog.adamzolo.com/elixir-phoenix-cache/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Mon, 29 Jan 2018 00:53:01 +0000</pubDate>
				<category><![CDATA[Elixir]]></category>
		<category><![CDATA[Phoenix]]></category>
		<category><![CDATA[Web]]></category>
		<guid isPermaLink="false">http://blog.adamzolo.com/?p=717</guid>

					<description><![CDATA[An implementation with ets. Let&#8217;s start with implementation Update application.ex Finally, use it Relavent links: https://stackoverflow.com/questions/35218738/caching-expensive-computation-in-elixir https://dockyard.com/blog/2017/05/19/optimizing-elixir-and-phoenix-with-ets]]></description>
										<content:encoded><![CDATA[<p>An implementation with ets. </p>
<p>Let&#8217;s start with implementation</p>
<pre class="brush: plain; title: ; notranslate">
defmodule SimpleCache do
  @table :simple_cache

  def init(_) do
    :ets.new(@table, &#x5B;
      :set,
      :named_table,
      :public,
      read_concurrency: true,
      write_concurrency: true
    ])

    {:ok, %{}}
  end

  def start_link do
    GenServer.start_link(__MODULE__, &#x5B;], name: __MODULE__)
  end

  def fetch(key, expires_in_seconds, fun) do
    case lookup(key) do
      {:hit, value} -&gt;
        value

      :miss -&gt;
        value = fun.()
        put(key, expires_in_seconds, value)
        value
    end
  end

  defp lookup(key) do
    case :ets.lookup(@table, key) do
      &#x5B;{^key, expires_at, value}] -&gt;
        case now &lt; expires_at do
          true -&gt; {:hit, value}
          false -&gt; :miss
        end

      _ -&gt;
        :miss
    end
  end

  defp put(key, expires_in_seconds, value) do
    expires_at = now + expires_in_seconds
    :ets.insert(@table, {key, expires_at, value})
  end

  defp now do
    :erlang.system_time(:seconds)
  end
end


</pre>
<p>Update application.ex</p>
<pre class="brush: plain; title: ; notranslate">
 def start(_type, _args) do
    import Supervisor.Spec

    children = &#x5B;
      supervisor(SimpleCache, &#x5B;])
    ]
    opts = &#x5B;strategy: :one_for_one, name: Supervisor]
    Supervisor.start_link(children, opts)
  end

</pre>
<p>Finally, use it</p>
<pre class="brush: plain; title: ; notranslate">
    cache_for_seconds = 60
    key = 'key'

    SimpleCache.fetch(key, cache_for_seconds, fn -&gt;
      {:ok, some_expensive_operation}
    end)
</pre>
<p>Relavent links:<br />
<a href="https://stackoverflow.com/questions/35218738/caching-expensive-computation-in-elixir" rel="noopener" target="_blank">https://stackoverflow.com/questions/35218738/caching-expensive-computation-in-elixir</a><br />
<a href="https://dockyard.com/blog/2017/05/19/optimizing-elixir-and-phoenix-with-ets" rel="noopener" target="_blank">https://dockyard.com/blog/2017/05/19/optimizing-elixir-and-phoenix-with-ets</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/elixir-phoenix-cache/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Elixir Phoenix Deployment with Distillery</title>
		<link>https://blog.adamzolo.com/elixir-phoenix-deployment-distillery/</link>
					<comments>https://blog.adamzolo.com/elixir-phoenix-deployment-distillery/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Sat, 21 Oct 2017 22:35:58 +0000</pubDate>
				<category><![CDATA[Elixir]]></category>
		<category><![CDATA[Phoenix]]></category>
		<guid isPermaLink="false">http://blog.adamzolo.com/?p=690</guid>

					<description><![CDATA[OS: Ubuntu 17.04/zesty We&#8217;ll be deploying using a git hook. This is not intended to be used in a production environment, but works just fine for a personal side project. Let&#8217;s start with the server setup Erlang/Elixir #Add Erlang Solutions repo wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb &#38;&#38; sudo dpkg -i erlang-solutions_1.0_all.deb sudo apt-get update #Install Erlang/OTP sudo apt-get&#8230;<p><a class="more-link" href="https://blog.adamzolo.com/elixir-phoenix-deployment-distillery/" title="Continue reading &#8216;Elixir Phoenix Deployment with Distillery&#8217;">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>OS: Ubuntu 17.04/zesty</p>
<p>We&#8217;ll be deploying using a git hook. This is not intended to be used in a production environment, but works just fine for a personal side project.</p>
<p>Let&#8217;s start with the server setup<br />
<strong>Erlang/Elixir</strong></p>
<pre class="brush: plain; title: ; notranslate">
#Add Erlang Solutions repo
wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb &amp;&amp; sudo dpkg -i erlang-solutions_1.0_all.deb

sudo apt-get update

#Install Erlang/OTP
sudo apt-get install esl-erlang

</pre>
<p>We&#8217;ll be using kiex for Elixir</p>
<pre class="brush: plain; title: ; notranslate">
#Install Elixir
\curl -sSL https://raw.githubusercontent.com/taylor/kiex/master/install | bash -s

#Add to .bashrc
test -s &quot;$HOME/.kiex/scripts/kiex&quot; &amp;&amp; source &quot;$HOME/.kiex/scripts/kiex&quot;

kiex install 1.4.0
kiex use 1.4.0
kiex default 1.4.0
</pre>
<p>Let&#8217;s init a repo</p>
<pre class="brush: plain; title: ; notranslate">
apt-get install git-core
mkdir repos &amp;&amp; cd repos
mkdir my_repo 
cd my_repo
git init --bare
</pre>
<p>Add a remote to your local repo:</p>
<pre class="brush: plain; title: ; notranslate">
git remote add some_name root@IP_ADDRESS:repos/your_repo.git
</pre>
<p>Go back to the server and create a post-deploy hook. This will generate a new release after each git push.</p>
<p>Create post-receive file inside of your git hooks directory with this content:</p>
<pre class="brush: plain; title: ; notranslate">
#!/bin/bash -l

&#x5B;&#x5B; -s &quot;$HOME/.kiex/scripts/kiex&quot; ]] &amp;&amp; source &quot;$HOME/.kiex/scripts/kiex&quot;

GIT_REPO=$HOME/repos/your_repo.git
TMP_GIT_CLONE=$HOME/tmp/git/your_repo

cd $TMP_GIT_CLONE

PORT=4000 _build/prod/rel/{app_name}/bin/{app_name} stop
cd $HOME
rm -r $TMP_GIT_CLONE
git clone $GIT_REPO $TMP_GIT_CLONE
cd $TMP_GIT_CLONE

mix deps.get
cd assets
npm install
brunch build --production
cd ..
MIX_ENV=prod mix do compile, phx.digest, release --env=prod

MIX_ENV=prod mix ecto.migrate
PORT=4000 _build/prod/rel/{app_name}/bin/{app_name} start

</pre>
<p>This will stop your existing elixir app, remove old code, compile and release your new code, run the migration, and start the app on port 4000.</p>
<p>If you run it behind NGINX, set it up as a reverse proxy:</p>
<pre class="brush: plain; title: ; notranslate">
server {
  listen 80;
  listen &#x5B;::]:80;

  server_name domain.com;

  location / {
     proxy_redirect off;
     proxy_pass http://127.0.0.1:4000/;
  }
}
</pre>
<p>Let&#8217;s go back to your Phoenix project and add distillery<br />
Add to mix.exs deps:</p>
<pre class="brush: plain; title: ; notranslate">
  {:distillery, &quot;~&gt; 1.5&quot;}
</pre>
<p>Initialize reliase config:</p>
<pre class="brush: plain; title: ; notranslate">
mix release.init
</pre>
<p>Update config/prod.exs</p>
<pre class="brush: plain; title: ; notranslate">
  config :app_name, AiPhoenixWeb.Endpoint,
    http: &#x5B;port: {:system, &quot;PORT&quot;}],
    url: &#x5B;host: &quot;localhost&quot;, port: {:system, &quot;PORT&quot;}], # This is critical for ensuring web-sockets properly authorize.
    cache_static_manifest: &quot;priv/static/cache_manifest.json&quot;,
    server: true,
    root: &quot;.&quot;,
    version: Mix.Project.config&#x5B;:version]

</pre>
<p>If using PostgreSQL, let&#8217;s install Postgresql and create a deploy user for our database:</p>
<pre class="brush: plain; title: ; notranslate">
sudo apt-get install postgresql postgresql-contrib libpq-dev
sudo su - postgres
createuser --pwprompt deploy
createdb -O deploy my_app_name_production
Exit
</pre>
<p>Auto-start your phoenix app on reboot:<br />
Create a new file:</p>
<pre class="brush: plain; title: ; notranslate">
#!/bin/bash

cd $HOME/phoenix_app_location
PORT=4001 _build/prod/rel/phoenix_name/bin/phoenix_name start

</pre>
<p>Make it an executable<br />
chmod +x phoenix_autostart.sh</p>
<p>Schedule it as a cron task:<br />
Crontab -e</p>
<pre class="brush: plain; title: ; notranslate">
@reboot /root/path_to_script/phoenix_autostart.sh
</pre>
<p>The last step to to &#8220;git push&#8221; and verify that everything works.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/elixir-phoenix-deployment-distillery/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Phoenix and Ecto. First Steps</title>
		<link>https://blog.adamzolo.com/phoenix-and-ecto-first-steps/</link>
					<comments>https://blog.adamzolo.com/phoenix-and-ecto-first-steps/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Thu, 06 Apr 2017 22:02:33 +0000</pubDate>
				<category><![CDATA[Elixir]]></category>
		<category><![CDATA[Phoenix]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://blog.adamzolo.com/?p=483</guid>

					<description><![CDATA[Valid as of Phoenix 1.2 Show routes: mix phoenix.routes Generating resources: mix phoenix.gen.html Post posts &#8211;no-model mix phoenix.gen.json Post posts Ecto Types :string :integer :map :binary_id :float :boolean Writing Queries Two ways Query import Ecto.Query from p in context.Posts where p.Title.Contains(&#34;Stuff&#34;) select p; Expression MyApp.Post &#124;&#62; where(titlle: &#34;Stuff&#34;) &#124;&#62; limit(1) Making changes &#8211; https://hexdocs.pm/ecto/Ecto.Changeset.html changeset&#8230;<p><a class="more-link" href="https://blog.adamzolo.com/phoenix-and-ecto-first-steps/" title="Continue reading &#8216;Phoenix and Ecto. First Steps&#8217;">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>Valid as of Phoenix 1.2</p>
<p>Show routes:<br />
mix phoenix.routes</p>
<p>Generating resources:<br />
mix phoenix.gen.html Post posts &#8211;no-model<br />
mix phoenix.gen.json Post posts</p>
<p><strong>Ecto</strong><br />
Types</p>
<ul>
<li>:string</li>
<li>:integer</li>
<li>:map</li>
<li>:binary_id</li>
<li>:float</li>
<li>:boolean</li>
</ul>
<p><strong>Writing Queries</strong><br />
Two ways</p>
<ol>
<li><a href="https://hexdocs.pm/ecto/Ecto.Query.html" target="_blank">Query</a>
<pre class="brush: plain; title: ; notranslate">
import Ecto.Query

from p in context.Posts
where p.Title.Contains(&quot;Stuff&quot;)
select p;
</pre>
</li>
<li>Expression
<pre class="brush: plain; title: ; notranslate">
MyApp.Post
|&gt; where(titlle: &quot;Stuff&quot;)
|&gt; limit(1)
</pre>
</li>
</ol>
<p>Making changes &#8211; https://hexdocs.pm/ecto/Ecto.Changeset.html<br />
changeset = Post.changeset(post, %{title: &#8220;updated&#8221;})<br />
Repo.update(changeset)<br />
Repo.delete(post)</p>
<p>Migrations<br />
Generate migration:<br />
mix ecto.gen.migration [migration_name] -r [repo]</p>
<p>Generate schema:<br />
mix phoenix.gen.model [Schema] [table] [fields] -r [repo]</p>
<p>Run/Rollback migration<br />
mix ecto.migrate -r [repo]<br />
mix ecto.rollback -r [repo]</p>
<p>Generate migration:<br />
Does not generate schema module:<br />
mix ecto.gen.migration [migration_name] -r [repo]</p>
<p>Generates both schema model and a migration:<br />
mix phoenix.gen.model [Schema] [table] [fields] -r [repo]</p>
<p>To avoid specifying repo all the time:<br />
config :my_app, ecto_repos: [MyApp.Repo]</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/phoenix-and-ecto-first-steps/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Starting with Phoenix. Installation.</title>
		<link>https://blog.adamzolo.com/starting-with-phoenix-installation/</link>
					<comments>https://blog.adamzolo.com/starting-with-phoenix-installation/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Sat, 01 Apr 2017 19:45:16 +0000</pubDate>
				<category><![CDATA[Elixir]]></category>
		<category><![CDATA[Phoenix]]></category>
		<guid isPermaLink="false">http://blog.adamzolo.com/?p=469</guid>

					<description><![CDATA[Prerequisites (valid as of Phoenix 1.2) Mac Elixir &#8211; brew install elixir node &#8211; brew install node Postgres &#8211; Postgres.app Windows For Windows, can use Chocolatey to install node and Elixir. Postgres seems to be outdated as of time of this post. Or download from source and install: Elixir node Postgres Add to Windows path&#8230;<p><a class="more-link" href="https://blog.adamzolo.com/starting-with-phoenix-installation/" title="Continue reading &#8216;Starting with Phoenix. Installation.&#8217;">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>Prerequisites (valid as of Phoenix 1.2)</p>
<p><strong>Mac</strong></p>
<ul>
<li>Elixir &#8211; brew install elixir</li>
<li>node &#8211; brew install node</li>
<li>Postgres &#8211; <a href="http://postgresapp.com/" target="_blank">Postgres.app</a></li>
</ul>
<p><strong>Windows</strong><br />
For Windows, can use <a href="https://chocolatey.org/" target="_blank">Chocolatey </a>to install node and Elixir. Postgres seems to be outdated as of time of this post. Or download from source and install:</p>
<ul>
<li><a href="http://elixir-lang.org/install.html#windows" target="_blank">Elixir</a></li>
<li><a href="https://nodejs.org/en/download/" target="_blank">node</a></li>
<li><a href="https://www.enterprisedb.com/downloads/postgres-postgresql-downloads#windows" target="_blank">Postgres</a></li>
</ul>
<p>Add to Windows path PostgreSQL (dependent on PostgreSQL version), so we can use psql on command line &#8211;<br />
C:\Program Files\PostgreSQL\9.6\bin<br />
C:\Program Files\PostgreSQL\9.6\lib</p>
<p><strong>Install phoenix:</strong></p>
<pre class="brush: plain; title: ; notranslate">
$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
</pre>
<p>Alternatively, download a specific version from https://github.com/phoenixframework/archives.</p>
<p>Generate and run a new project:</p>
<pre class="brush: plain; title: ; notranslate">
$ mix phoenix.new my_first_app
$ mix ecto.create
$ npm install
$ iex -S mix phoenix.server
iex&gt; :observer.start

</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/starting-with-phoenix-installation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
