<?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: Bruno Luis Panuto Silva</title>
    <description>The latest articles on DEV Community by Bruno Luis Panuto Silva (@nubunto).</description>
    <link>https://dev.to/nubunto</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%2F266461%2Ff6d6c1c0-eca2-4357-af1e-03102d1d3050.jpeg</url>
      <title>DEV Community: Bruno Luis Panuto Silva</title>
      <link>https://dev.to/nubunto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nubunto"/>
    <language>en</language>
    <item>
      <title>WMHub: easy Web Monetization for all</title>
      <dc:creator>Bruno Luis Panuto Silva</dc:creator>
      <pubDate>Thu, 04 Jun 2020 14:41:10 +0000</pubDate>
      <link>https://dev.to/nubunto/wmhub-easy-web-monetization-for-all-8</link>
      <guid>https://dev.to/nubunto/wmhub-easy-web-monetization-for-all-8</guid>
      <description>&lt;h2&gt;
  
  
  What I built
&lt;/h2&gt;

&lt;p&gt;A web app that allows you to automatically add Web Monetization to your project without having to code JS or add &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; tags. It also collects metrics on pages with Web Monetization and consolidates that into the platform.&lt;/p&gt;

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

&lt;p&gt;Foundational Technology&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://wmhub.live" rel="noopener noreferrer"&gt;https://wmhub.live&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Link to Code
&lt;/h2&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/WebMonetizationHub" rel="noopener noreferrer"&gt;
        WebMonetizationHub
      &lt;/a&gt; / &lt;a href="https://github.com/WebMonetizationHub/wmhub-legacy" rel="noopener noreferrer"&gt;
        wmhub-legacy
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Wmhub&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;Here you'll find our application!&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Setup&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;Install the Elixir programming language, clone this repo and run:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;mix deps.get
cd assets/ &amp;amp;&amp;amp; npm install &amp;amp;&amp;amp; cd ..
iex -S mix phx.server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/WebMonetizationHub/wmhub-legacy" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


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

&lt;p&gt;The stack for this project is Elixir, with PostgreSQL for the database and Phoenix as the web framework.&lt;/p&gt;

&lt;p&gt;The "library" injected in pages to handle Web Monetization events is made in JavaScript, the ES5 version. We want to include everyone!&lt;/p&gt;

&lt;p&gt;All interactions apart from the landing page, signin and signup forms are Phoenix LiveViews, which classifies this app as an SPA (after you login).&lt;/p&gt;

&lt;p&gt;Communication with pages to broadcast real-time payment pointer updates to users that are already connected to your app are handled by Phoenix Channels.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Resources/Info
&lt;/h2&gt;

&lt;p&gt;The plan is to expand WMHub beyond a simple "payment pointer" update tool. Here are some of the ideas in the roadmap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A templating tool that allows you to hook Web Monetization events and display content. Maybe display a link to paying users! There is a challenge here in flexibility vs mitigating XSS attack vectors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A "trigger" tool that runs on Web Monetization events and allows you to hook any type of code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A marketplace of triggers and templates, so that users can install things without any knowledge whatsoever.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Collect (anonimized/with consent) data from users visiting a WMHub powered project, allowing the content creator more insight on how their content is consumed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>gftwhackathon</category>
    </item>
    <item>
      <title>How to serve JavaScript from a Phoenix View</title>
      <dc:creator>Bruno Luis Panuto Silva</dc:creator>
      <pubDate>Sun, 10 May 2020 05:46:08 +0000</pubDate>
      <link>https://dev.to/nubunto/phoenix-views-are-not-only-for-html-dfb</link>
      <guid>https://dev.to/nubunto/phoenix-views-are-not-only-for-html-dfb</guid>
      <description>&lt;p&gt;When we first hear and use Phoenix Views, it's primary use case is to render HTML or JSON views.&lt;/p&gt;

&lt;p&gt;But that's not all Phoenix Views can do!&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript!
&lt;/h2&gt;

&lt;p&gt;HTLM and JSON have their uses in web apps and APIs, for sure. But what if we are an API or web application providing a service not to a user or system, but to a browser?&lt;/p&gt;

&lt;p&gt;A web browser can deal with different assets. But if you want it to &lt;em&gt;do&lt;/em&gt; something, you have to speak JavaScript. There are some notable exceptions (WebAssembly?) but this is the de-facto way of getting code to a user's browser.&lt;/p&gt;

&lt;p&gt;So, are we able to create a Phoenix View that renders JavaScript? Let's give it a shot.&lt;/p&gt;

&lt;p&gt;In order to pull this off, we would need:&lt;/p&gt;

&lt;p&gt;1) A Phoenix.View that is going to render our JavaScript file&lt;br&gt;
2) A template, probably with an extension like &lt;code&gt;.js.eex&lt;/code&gt;&lt;br&gt;
3) A controller that sets up the correct &lt;code&gt;Content-Type&lt;/code&gt; and renders this &lt;code&gt;.js.eex&lt;/code&gt; template.&lt;/p&gt;

&lt;p&gt;Let's try!&lt;/p&gt;
&lt;h2&gt;
  
  
  The View
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Myapp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JSView&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;MyappWeb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:view&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We're stating that the template is available in the directory configured by the &lt;code&gt;:root&lt;/code&gt; property. By default, that would be &lt;code&gt;myapp_web/templates/#{myresource}/#{mycontroller.html.eex}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But we are not HTML.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Template
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="c1"&gt;# myapp_web/templates/js/something.js.eex&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'oh my, from Elixir? This is &amp;lt;%= @adjective %&amp;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 it's a normal template. But there's a key difference: we're actually a &lt;code&gt;.js.eex&lt;/code&gt; extension. This is just to signal that we're building a .js template, it is not dealt with differently by EEx.&lt;/p&gt;

&lt;p&gt;And we have access to any assigns, as you can see when we read the &lt;code&gt;@adjective&lt;/code&gt;. No surprises here!&lt;/p&gt;

&lt;p&gt;So, how do we render it?&lt;/p&gt;
&lt;h2&gt;
  
  
  The Controller
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;MyappWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;JSController&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;MyappWeb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:controller&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"something.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;adjective:&lt;/span&gt; &lt;span class="s2"&gt;"awesome"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;something.js&lt;/code&gt; gets mapped to the corresponding &lt;code&gt;something.js.eex&lt;/code&gt; template we defined above. Not unlike building a "normal" HTML Phoenix View. Super clean!&lt;/p&gt;

&lt;p&gt;There's only one last thing we need to do in order to correctly serve this file as valid JavaScript: the &lt;code&gt;Content-Type&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="c1"&gt;# lib/myapp_web/router.ex&lt;/span&gt;
&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;MyappWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Router&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# lots of stuff above...&lt;/span&gt;

  &lt;span class="n"&gt;pipeline&lt;/span&gt; &lt;span class="ss"&gt;:js&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;plug&lt;/span&gt; &lt;span class="ss"&gt;:put_js_content_type&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;put_js_content_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;put_content_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"text/javascript"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="s2"&gt;"/custom_js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;MyappWeb&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;pipe_through&lt;/span&gt; &lt;span class="ss"&gt;:js&lt;/span&gt;
    &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s2"&gt;"/myjsfile.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;JSController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:index&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So there you have it. Phoenix Views are great and really flexible!&lt;/p&gt;

&lt;h1&gt;
  
  
  DANGER
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Be Careful&lt;/em&gt; with how you deal with user input when pasting it inside JS code. You could be opening yourself up to a XSS attack! You can read more about XSS &lt;a href="https://owasp.org/www-community/attacks/xss/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Phoenix has facilities to deal with (some) JavaScript data escaping. Refer to &lt;a href="https://hexdocs.pm/phoenix_html/Phoenix.HTML.html#javascript_escape/1"&gt;javascript_escape&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In short: make sure that data you put on your file is not able to run arbitrary code. This is not meant to be a security guide, so viewer discretion is advised.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Granted, this is not your common use case. Usually, JS files are static and served from a CDN. But sometimes we need to step out of the ordinary, and it's amazing that it's so simple to do in Phoenix!&lt;/p&gt;

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