<?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 mark harper on Medium]]></title>
        <description><![CDATA[Stories by mark harper on Medium]]></description>
        <link>https://medium.com/@marksaystuff?source=rss-717af1cad8ae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*p0V_-HKr1lz_WFNt.jpeg</url>
            <title>Stories by mark harper on Medium</title>
            <link>https://medium.com/@marksaystuff?source=rss-717af1cad8ae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:28:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marksaystuff/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[CORS Settings in Phoenix]]></title>
            <link>https://medium.com/@marksaystuff/cors-settings-in-phoenix-bb27d7160?source=rss-717af1cad8ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb27d7160</guid>
            <category><![CDATA[quick-tips]]></category>
            <dc:creator><![CDATA[mark harper]]></dc:creator>
            <pubDate>Thu, 28 May 2020 08:00:01 GMT</pubDate>
            <atom:updated>2020-05-28T08:00:01.402Z</atom:updated>
            <content:encoded><![CDATA[<p>Every web developer has come across a CORS error before. It’s one of the last things you want to deal with when putting together your application. You just want to get the code moving and worry about this later.</p><p>Fortunately, <a href="https://admin.evolvingdev.io/p/e19f9e7a-e9c6-47b3-987e-171cc70177ab/phoenixframework.org/">Phoenix</a> makes it easy to build and serve frontend code from the same origin, usually making CORS a non-issue. This is not always an acceptable setup. Sometimes you want to run the client at a separate origin, the client is actually anyone consuming your public api, or you need to allow all cross origin requests temporarily. In this post, I’ll layout how you can manage CORS settings in your Phoenix app so it’s not a problem.</p><p>I’ve setup an example project where each commit roughly follows the steps that follow. If you want to go ahead and take a look, here you <a href="https://github.com/MarkHarper/phoenix_cors_example">go</a>.</p><p><a href="https://admin.evolvingdev.io/p/e19f9e7a-e9c6-47b3-987e-171cc70177ab/repop">F</a>or our example, we have an application with a Phoenix backend and a <a href="https://reactjs.org/">React</a> frontend. It’s a fairly small app that displays a list of stored images. The React app is setup to be served separately from the Phoenix application. This means the default create-react-app and Phoenix configurations will result in CORS errors in development. More specifically, the browser will not allow a client application at origin A to read content from a server at origin B without the presence of a CORS header. In order to add CORS headers, we can use a library called <a href="https://hexdocs.pm/corsica/Corsica.html">Corsica</a>. Let’s add it as a dependency for your Phoenix app. The deps declaration in your mix.exs file should look something like this:</p><pre>defp deps do<br>    [<br>      ...<br>      {:corsica, &quot;~&gt; 1.1.3&quot;}<br>    ]<br>end</pre><p>With Corsica declared as a dependency, all we have to do is make a single configuration change. Add the following to your endpoints.ex file.</p><pre>plug Corsica,<br>    origins: &quot;*&quot;</pre><p>If you reload the react app, then you should see the images stored by your Phoenix app. There is a problem with this approach though. Adding the wildcard CORS header means that any address can make requests to our server. This potentially leaves it more vulnerable than we would like. An alternative approach is to use a whitelist of valid domains. We could use the following configuration to limit the domains to just our React app running locally.</p><pre>plug Corsica,<br>    origins: [&quot;http://localhost:3000&quot;]</pre><p>At the very least this restricts access to the only client that is relevant for our current purposes. But what about production? We don’t want to change our configuration manually just to develop locally or for our server in production. This is where configs come into play. We can offload the value of the origins list to the prod or dev config.</p><p>The endpoint.ex Corsica configuration will have to be updated to retrieve the configuration. Application.get_env will do the trick. The first argument should be the application under which the config property has been stored. If you look at your mix.exs file, you will find the available applications in the application function. The second argument should be the name of the config property being accessed. In our case that property is :origins. The Corsica config should look something like this.</p><pre>plug Corsica,<br>    origins: Application.get_env(:your_app_id, :origins)</pre><p>We will have to store our config values in the files appropriate for their environment, dev and prod. This can be done with the config function as follows.</p><p>config/prod.exs:</p><pre>config :your_app_id, origins: [&quot;YOUR_PROD_APP_URL&quot;]</pre><p>config/dev.exs:</p><pre>config :your_app_id, origins: [&quot;http://localhost:3000&quot;]</pre><p>Now, you should have a fully functioning origin whitelist for CORS requests. Corsica is capable of much more than origin whitelists. I won’t go into detail here, but I’ve also used it for content-type and authorization headers. For more information, check out the <a href="https://hexdocs.pm/corsica/Corsica.html">Corsica</a> documentation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb27d7160" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Up and Running with Phoenix Live Reload and Docker]]></title>
            <link>https://medium.com/@marksaystuff/up-and-running-with-phoenix-live-reload-and-docker-704a8031d49f?source=rss-717af1cad8ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/704a8031d49f</guid>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[mark harper]]></dc:creator>
            <pubDate>Wed, 27 May 2020 22:32:14 GMT</pubDate>
            <atom:updated>2020-05-27T22:32:14.031Z</atom:updated>
            <content:encoded><![CDATA[<p>It is becoming more and more common to deploy containerized applications for a variety of reasons:</p><ul><li>consistency between development and production</li><li>isolation of the service environments</li><li>the ability to easily scale modular services</li></ul><p>There are many more benefits of using containers, but you get the gist of it. The most commonly used container system is Docker. As it’s popularity has risen, a growing number of people in the <a href="http://phoenixframework.org/">Phoenix</a> community are deploying applications either inside or built within docker containers.</p><p>However, note the first benefit of containers that I have listed above. I develop on a Mac, but most often I deploy to Linux droplets on <a href="http://digitalocean.com/">Digital Ocean</a>. Even if I build my app inside Docker during the deployment process, then I am just putting off dealing with potential unseen problems while working on my Mac. This left me wanting to run Phoenix locally inside a Docker container.</p><p>You can certainly read the documentation for both Phoenix and Docker to create a setup from scratch, but there are many great articles on this topic. Here are a couple:</p><ul><li><a href="https://pspdfkit.com/blog/2018/how-to-run-your-phoenix-application-with-docker/">How to run your phoenix application with docker</a></li><li><a href="https://dev.bleacherreport.com/starting-a-phoenix-project-with-docker-ab2a171fa2f4">Starting a phoenix project with docker</a></li></ul><p>After taking my first crack at it, I had Phoenix up and running in a Docker container. The important elements of the setup are:</p><ul><li>dockerfile: installs the base docker image &amp; dependencies, sets up a directory for our codebase, and runs the shell script</li><li>docker compose file: defines the docker services that will be used (the phoenix image and the postgres image), provides environment variables, and the application’s port</li><li>shell script file: installs dependencies (mix and npm), handles creating the database, and starts the phoenix server</li></ul><p>Even with the application running fine, there was one issue. A major positive aspect of Phoenix development no longer worked: live reloading. With some minor changes I was able to get my development process back in shape.</p><p><strong>dockerfile:</strong></p><ul><li>Add a line to install inotify tools. This is required for responding to file change events.</li><li>Remove the use of the Docker copy command. We will be using a different method for providing our codebase to the container. More on that shortly.</li></ul><pre>FROM elixir:1.8.2-alpine<br><br>RUN apk update<br>RUN apk upgrade --no-cache<br>RUN apk add nodejs=10.14.2-r0 nodejs-npm=10.14.2-r0<br>RUN apk add inotify-tools=3.20.1-r1<br>RUN apk add postgresql-client=11.3-r0<br>RUN mix local.rebar --force<br>RUN mix local.hex --force<br><br>RUN mkdir /app<br><br>WORKDIR /app<br><br>CMD [&quot;sh&quot;, &quot;./entrypoint.sh&quot;]</pre><p><strong>docker-compose.yml:</strong></p><ul><li>Add a <a href="https://docs.docker.com/storage/volumes/">Docker volume</a> to the Phoenix container</li></ul><pre>version: &#39;3.2&#39;<br><br>services:<br>  phoenix:<br>    build:<br>      context: .<br>    volumes:<br>      - .:/app<br>    environment:<br>      PGUSER: postgres<br>      PGPASSWORD: postgres<br>      PGDATABASE: app<br>      PGPORT: 5432<br>      PGHOST: db<br>    ports:<br>      - &quot;4000:4000&quot;<br>    depends_on:<br>      - db<br>  db:<br>    image: postgres:9.6<br>    environment:<br>      POSTGRES_USER: postgres<br>      POSTGRES_PASSWORD: postgres<br>      PGDATA: /var/lib/postgresql/data/pgdata<br>    restart: always<br>    volumes:<br>      - pgdata:/var/lib/postgresql/data<br>volumes:<br>  pgdata:</pre><p><strong>webpack.config.js:</strong></p><ul><li>Add a configuration to tell webpack to poll for file changes.</li></ul><pre>const path = require(&#39;path&#39;);<br>const glob = require(&#39;glob&#39;);<br>const MiniCssExtractPlugin = require(&#39;mini-css-extract-plugin&#39;);<br>const UglifyJsPlugin = require(&#39;uglifyjs-webpack-plugin&#39;);<br>const OptimizeCSSAssetsPlugin = require(&#39;optimize-css-assets-webpack-plugin&#39;);<br>const CopyWebpackPlugin = require(&#39;copy-webpack-plugin&#39;);<br><br>module.exports = (env, options) =&gt; ({<br>  optimization: {<br>    minimizer: [<br>      new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),<br>      new OptimizeCSSAssetsPlugin({})<br>    ]<br>  },<br>  entry: {<br>      &#39;./js/app.js&#39;: [&#39;./js/app.js&#39;].concat(glob.sync(&#39;./vendor/**/*.js&#39;))<br>  },<br>  output: {<br>    filename: &#39;app.js&#39;,<br>    path: path.resolve(__dirname, &#39;../priv/static/js&#39;)<br>  },<br>  watchOptions: {<br>    poll: true<br>  },<br>  module: {<br>    rules: [<br>      {<br>        test: /\.js$/,<br>        exclude: /node_modules/,<br>        use: {<br>          loader: &#39;babel-loader&#39;<br>        }<br>      },<br>      {<br>        test: /\.css$/,<br>        use: [MiniCssExtractPlugin.loader, &#39;css-loader&#39;]<br>      }<br>    ]<br>  },<br>  plugins: [<br>    new MiniCssExtractPlugin({ filename: &#39;../css/app.css&#39; }),<br>    new CopyWebpackPlugin([{ from: &#39;static/&#39;, to: &#39;../&#39; }])<br>  ]<br>});</pre><p>After making those changes and restarting your container, you will notice that any asset or elixir file change will trigger a server/page reload. For a full example project check out this <a href="https://github.com/MarkHarper/phoenix-docker-live-reload">repo</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=704a8031d49f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>