<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Intro on s20n blog</title><link>https://blog.s20n.dev/tags/intro/</link><description>Recent content in Intro on s20n blog</description><generator>Hugo 0.150.0</generator><language>en-us</language><lastBuildDate>Fri, 30 Jun 2023 21:23:07 +0530</lastBuildDate><atom:link href="https://blog.s20n.dev/tags/intro/index.xml" rel="self" type="application/rss+xml"/><item><title>C++ for the Web with Drogon</title><link>https://blog.s20n.dev/posts/cpp-for-the-web-drogon/</link><pubDate>Fri, 30 Jun 2023 21:23:07 +0530</pubDate><guid>https://blog.s20n.dev/posts/cpp-for-the-web-drogon/</guid><description>&lt;p&gt;&lt;a href="https://drogon.org"&gt;Drogon&lt;/a&gt; is currently &lt;a href="https://www.techempower.com/benchmarks/#section=data-r21"&gt;the fastest web server in the world&lt;/a&gt;. If you already know a bit of C++, it wouldn&amp;rsquo;t hurt to try it out. I personally think drogon is great, however I do find its documentation somewhat lacking which is all the more reason to contribute to them. If you find that drogon is right for you, please consider &lt;a href="https://github.com/drogonframework/drogon"&gt;contributing to / sponsoring their project&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this blog, we&amp;rsquo;ll see how we can set up a basic REST API using drogon and CMake.&lt;/p&gt;
&lt;p&gt;The code snippets I&amp;rsquo;ve provided apply to macOS and Linux. However, you can still follow along if you&amp;rsquo;re on windows and have worked with CMake and VS before.&lt;/p&gt;
&lt;h2 id="project-setup"&gt;Project Setup&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re like me and you don&amp;rsquo;t mind using CMake with submodules, we can set up drogon like so:&lt;/p&gt;
&lt;p&gt;Get the pre-requisite libraries installed in accordance with &lt;a href="https://github.com/drogonframework/drogon/wiki/ENG-02-Installation"&gt;the official documentation&lt;/a&gt;. For macOS, I had to install &lt;code&gt;ossp-uuid&lt;/code&gt; and &lt;code&gt;jsoncpp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Create a CMakeLists.txt in your project root.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;touch CMakeLists.txt
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Initialize Git in your project root.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git init
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Add drogon as a submodule&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git submodule add https://github.com/drogonframework/drogon.git external/drogon
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Pull drogon&amp;rsquo;s submodules (trantor)&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git submodule update --init --recursive
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;At this point you&amp;rsquo;ve got your dependencies setup. Let&amp;rsquo;s go ahead and add some code for our basic REST API.&lt;/p&gt;
&lt;p&gt;Create a new directory for our source files&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;mkdir src
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And add our entrypoint&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;touch src/main.cpp
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here&amp;rsquo;s a basic &lt;code&gt;CMakeLists.txt&lt;/code&gt; file that will build our project&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-cmake" data-lang="cmake"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cmake_minimum_required(&lt;span style="color:#e6db74"&gt;VERSION&lt;/span&gt; &lt;span style="color:#e6db74"&gt;3.25.2&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;project(
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;drogontutorial&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;VERSION&lt;/span&gt; &lt;span style="color:#e6db74"&gt;0.1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;DESCRIPTION&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Drogon CMake Tutorial&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;LANGUAGES&lt;/span&gt; &lt;span style="color:#e6db74"&gt;CXX&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;# compiler flags
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&lt;/span&gt;set(&lt;span style="color:#e6db74"&gt;CMAKE_CXX_STANDARD&lt;/span&gt; &lt;span style="color:#e6db74"&gt;20&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;set(&lt;span style="color:#e6db74"&gt;CMAKE_CXX_STANDARD_REQUIRED&lt;/span&gt; &lt;span style="color:#e6db74"&gt;ON&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;set(&lt;span style="color:#e6db74"&gt;CMAKE_CXX_FLAGS&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;${CMAKE_CXX_FLAGS} -Wall -Wextra&amp;#34;&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;add_executable(&lt;span style="color:#f92672"&gt;${&lt;/span&gt;PROJECT_NAME&lt;span style="color:#f92672"&gt;}&lt;/span&gt; &lt;span style="color:#e6db74"&gt;src/main.cpp&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;add_subdirectory(&lt;span style="color:#e6db74"&gt;external/drogon&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;target_link_libraries(&lt;span style="color:#f92672"&gt;${&lt;/span&gt;PROJECT_NAME&lt;span style="color:#f92672"&gt;}&lt;/span&gt; &lt;span style="color:#e6db74"&gt;PRIVATE&lt;/span&gt; &lt;span style="color:#e6db74"&gt;drogon&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And this will be our &lt;code&gt;src/main.cpp&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;drogon/drogon.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;using&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;namespace&lt;/span&gt; drogon;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; app().setLogPath(&lt;span style="color:#e6db74"&gt;&amp;#34;./&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .setLogLevel(trantor&lt;span style="color:#f92672"&gt;::&lt;/span&gt;Logger&lt;span style="color:#f92672"&gt;::&lt;/span&gt;kWarn)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .addListener(&lt;span style="color:#e6db74"&gt;&amp;#34;0.0.0.0&amp;#34;&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;8080&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .setThreadNum(&lt;span style="color:#ae81ff"&gt;8&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .registerHandler(&lt;span style="color:#e6db74"&gt;&amp;#34;/hello?name={name}&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; [](&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; HttpRequestPtr&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; req,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;function&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; HttpResponsePtr&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;)&lt;span style="color:#f92672"&gt;&amp;gt;&amp;amp;&amp;amp;&lt;/span&gt; callback,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;string&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; name)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Json&lt;span style="color:#f92672"&gt;::&lt;/span&gt;Value json;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; json[&lt;span style="color:#e6db74"&gt;&amp;#34;result&amp;#34;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;ok&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; json[&lt;span style="color:#e6db74"&gt;&amp;#34;message&amp;#34;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;string(&lt;span style="color:#e6db74"&gt;&amp;#34;hello,&amp;#34;&lt;/span&gt;) &lt;span style="color:#f92672"&gt;+&lt;/span&gt; name;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;auto&lt;/span&gt; resp &lt;span style="color:#f92672"&gt;=&lt;/span&gt; HttpResponse&lt;span style="color:#f92672"&gt;::&lt;/span&gt;newHttpJsonResponse(json);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; callback(resp);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; { Get,&lt;span style="color:#e6db74"&gt;&amp;#34;LoginFilter&amp;#34;&lt;/span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .run();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="building-and-running-the-app"&gt;Building and Running the App&lt;/h2&gt;
&lt;p&gt;So far your directory structure should look something like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;├── CMakeLists.txt
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;├── external
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;│ └── drogon
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;└── src
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; └── main.cpp
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ae81ff"&gt;4&lt;/span&gt; directories, &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt; files
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Go ahead and create a directory to house our build files&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;mkdir build &lt;span style="color:#f92672"&gt;&amp;amp;&amp;amp;&lt;/span&gt; cd build
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now lets generate the Makefile using&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cmake ..
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and now to compile our application,&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;make -j&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;nproc&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Hopefully after all this, you should find an executable called &lt;code&gt;drogontutorial&lt;/code&gt; in the build directory.&lt;/p&gt;
&lt;p&gt;let&amp;rsquo;s run this file to start our server.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;./drogontutorial
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, your server should be listening on port 8080. Test it out by sending your name to &lt;code&gt;/hello&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;curl http://localhost:8080/hello?name&lt;span style="color:#f92672"&gt;=&lt;/span&gt;yourname
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And just like that you&amp;rsquo;ve got a starter project to write your own REST APIs in C++.&lt;/p&gt;
&lt;p&gt;If you find this nice, do check out their &lt;a href="https://github.com/drogonframework/drogon/tree/master/examples"&gt;examples on github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Until next time!&lt;/p&gt;</description></item><item><title>A Shallow Dive Into Webassembly</title><link>https://blog.s20n.dev/posts/a-shallow-dive-into-webassembly/</link><pubDate>Fri, 18 Mar 2022 19:22:00 +0530</pubDate><guid>https://blog.s20n.dev/posts/a-shallow-dive-into-webassembly/</guid><description>&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/WA.png" alt="Dive into WebAssembly"&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;ve been following the buzz in the past few years, you must have certainly heard about &lt;a href="https://webassembly.org"&gt;WebAssembly&lt;/a&gt;, the new, low-level, general purpose programming language that can be used to compile code for the web.
Webassembly was created by Mozilla and Google to be &lt;a href="https://medium.com/@torch2424/webassembly-is-fast-a-real-world-benchmark-of-webassembly-vs-es6-d85a23f8e193"&gt;extremely fast&lt;/a&gt;, safe and to bring near native performance to code running in the browser.&lt;/p&gt;
&lt;p&gt;Webassembly has been designed with performance in mind. It has been built on top of &lt;a href="https://github.com/llvm/llvm-project"&gt;LLVM&lt;/a&gt; which means it will have access to all of LLVM’s optimizations and features.&lt;/p&gt;
&lt;p&gt;Webassembly was created by the WebAssembly Community Group in 2015. The goal of Webassembly is to provide a compilation target that has a similar level of performance as native machine code but with the high-level convenience of JavaScript. It is supported by &lt;a href="https://webassembly.org/roadmap/"&gt;all 4 of the major browser engines&lt;/a&gt; today.&lt;/p&gt;
&lt;h2 id="but-why-webassembly"&gt;But Why WebAssembly?&lt;/h2&gt;
&lt;p&gt;With the near-native performance promised by WebAssembly, It is quite possible to &lt;a href="https://www.infoq.com/news/2019/07/doom3-web-assembly-port/"&gt;run really graphically intensive games&lt;/a&gt; right in the browser.
You surely must have used &lt;a href="https://www.figma.com"&gt;Figma&lt;/a&gt; or atleast heard of it. It&amp;rsquo;s a really powerful Graphic Design app which runs flawlessly, purely on the browser. The reason for its amazing performance is that It is powered by WebAssembly.
Infact I am pretty confident that in the near future Audio/Video and Photo editing on the Web could go mainstream with such a leap in performance.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/WOW.png" alt="WOW"&gt;&lt;/p&gt;
&lt;h2 id="assemblyscript"&gt;AssemblyScript&lt;/h2&gt;
&lt;p&gt;In order to write WebAssembly Code which runs in your browser, you don&amp;rsquo;t actually need to learn WebAssembly syntax. WebAssembly is better thought of as a compilation target for other higher level languages which are easier to write code in.
In fact, if you know Typescript, you can jump right in to &lt;a href="https://www.assemblyscript.org/"&gt;AssemblyScript&lt;/a&gt; which has &lt;a href="https://www.npmjs.com/package/assemblyscript"&gt;approximately 13000 npm downloads each week&lt;/a&gt; at the time of this publication.&lt;/p&gt;
&lt;p&gt;But if you&amp;rsquo;re feeling really adventurous, you really gotta try compiling raw C++ into WebAssembly. Yes you heard me right, It is indeed possible to cause a &lt;a href="https://stackoverflow.com/questions/55583474/how-do-i-debug-an-emscripten-segmentation-fault-due-to-exceeding-the-available-d"&gt;segfault in the browser&lt;/a&gt; with your code!&lt;/p&gt;
&lt;h2 id="running-c-in-the-browser"&gt;Running C++ in the browser&lt;/h2&gt;
&lt;p&gt;With WebAssembly, it is possible to call a C++ function in the browser. You could use a toolchain like &lt;a href="https://emscripten.org"&gt;Emscripten&lt;/a&gt; to do this but it is completely possible to compile C++ directly into WebAssembly&amp;rsquo;s binary format &lt;code&gt;wasm&lt;/code&gt; by setting &lt;code&gt;wasm32&lt;/code&gt; as the target for clang++.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s write a function that does bitwise left-shifts and yields the nth power of 2&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;extern&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;C&amp;#34;&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;powerOf2&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; (&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;lt;&amp;lt;&lt;/span&gt; n);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;C++ does &lt;a href="https://en.wikipedia.org/wiki/Name_mangling"&gt;Name Mangling&lt;/a&gt; which changes the name of the exported function and we won&amp;rsquo;t be able to call it from the browser if that happens. To suppress this behaviour, we need to wrap the function in &lt;code&gt;extern &amp;quot;C&amp;quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The C++ file can be compiled with the command :&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;clang++ --target&lt;span style="color:#f92672"&gt;=&lt;/span&gt;wasm32 --no-standard-libraries -Wl,--export-all -Wl,--no-entry -o power.wasm power.cpp
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--target=wasm32&lt;/code&gt; sets the target architecture to WebAssembly&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--no-standard-libraries&lt;/code&gt; because this target does not support libc++ (discussed later)&lt;/li&gt;
&lt;li&gt;&lt;code&gt; -Wl,--export-all&lt;/code&gt; exports all symbols (the function &lt;code&gt;powerOf2&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Wl,--no-entry&lt;/code&gt; tells the compiler that there is no &lt;code&gt;main&lt;/code&gt; function&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep in mind that you&amp;rsquo;re going to need clang++ and thus need to have LLVM installed.
You could download the prebuilt packages depending on your OS or &lt;a href="https://llvm.org/docs/GettingStarted.html"&gt;build llvm from source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;once your wasm binary is generated, you can simply call that function in your webpage like so&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;html&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;meta&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;charset&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;utf-8&amp;#34;&lt;/span&gt; /&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;title&lt;/span&gt;&amp;gt;WASM instantiateStreaming() test&amp;lt;/&lt;span style="color:#f92672"&gt;title&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;WebAssembly&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;instantiateStreaming&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;fetch&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;power.wasm&amp;#34;&lt;/span&gt;)).&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;((&lt;span style="color:#a6e22e"&gt;obj&lt;/span&gt;) =&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;obj&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;instance&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;exports&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;powerOf2&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;62&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; );
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;/&lt;span style="color:#f92672"&gt;html&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/instantiate-streaming.html"&gt;Code Based on source: WebAssembly JS-API documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here, We are calling the &lt;code&gt;powerOf2&lt;/code&gt; function with an argument of &lt;code&gt;62&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;You can check the output in the console showing &lt;code&gt;1073741824n&lt;/code&gt; and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"&gt;&lt;code&gt;n&lt;/code&gt;
indicates&lt;/a&gt; that the number is infact a BigInt, which is right as we return a &lt;code&gt;long long&lt;/code&gt; from the C++ function.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/console-wa-output.png" alt="Console Output"&gt;&lt;/p&gt;
&lt;p&gt;As mentioned before, libc++ is not supported by the target &lt;code&gt;wasm32&lt;/code&gt;, but in case you want to use it, you could target &lt;code&gt;wasi&lt;/code&gt; &lt;a href="https://stackoverflow.com/a/67174440/11335534"&gt;refer to this answer on stackoverflow&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And That&amp;rsquo;s it! You&amp;rsquo;ve successfully run C++ in your browser.
It&amp;rsquo;s time now to port all of your legacy code over to the browser. Hope you could follow along with this. Happy memory leaks and race conditions!&lt;/p&gt;</description></item><item><title>Headless CMS vs Traditional CMS</title><link>https://blog.s20n.dev/posts/headless-cms-vs-traditional-cms/</link><pubDate>Tue, 22 Feb 2022 17:28:00 +0530</pubDate><guid>https://blog.s20n.dev/posts/headless-cms-vs-traditional-cms/</guid><description>&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/headless.png" alt="Totally Headless Nick"&gt;&lt;/p&gt;
&lt;p&gt;Today is Tue, 22/02/2022. Happy Twosday!&lt;/p&gt;
&lt;p&gt;A CMS or Content Management System is a piece of software for digital content management, it can also be used as an enterprise wide digital asset management system, which enables you to manage your digital assets (images, videos, documents and other such files). The CMS comes with a web browser based user interface that allows users to upload content, edit their media files and to control their website.&lt;/p&gt;
&lt;p&gt;Headless CMS is a new trend in the CMS world. It is a backend-only solution that provides an API to interact with the content. This type of CMS is often used by developers who want to build their own frontend and don&amp;rsquo;t want to deal with the complexity of the backend frameworks.&lt;/p&gt;
&lt;p&gt;In contrast, traditional CMSs are more common for small businesses who need a simple way to manage their website content. They are also easier for non-technical users who just want to update their website without any coding knowledge.&lt;/p&gt;
&lt;h2 id="why-do-you-need-a-cms"&gt;Why do you need a CMS?&lt;/h2&gt;
&lt;p&gt;Using a CMS abstracts away a lot of technical intricacies from the content creator.
CMSs are specifically designed so that webmasters or site managers can create, modify and organise digital resources in various ways and for various levels of functionality or to provide search engine optimisation (SEO), online marketing automation processes, online sales opportunities, or self-service tools for their customers.&lt;/p&gt;
&lt;p&gt;CMSs have changed the world of web development, it has changed the way in which websites are designed and managed. &lt;a href="https://whatcms.org/Tech_Reports"&gt;Over 15 million domains&lt;/a&gt; on the internet run on some CMS technology like Wordpress, Drupal, Joomla, Shopify to name a few.&lt;/p&gt;
&lt;h2 id="why-headless"&gt;Why Headless?&lt;/h2&gt;
&lt;p&gt;Now, you might be thinking, if it&amp;rsquo;s way easier to use a traditional CMS like wordpress, why on earth would you want to go through the ordeal of decapitating the CMS and having to write your own frontend?&lt;/p&gt;
&lt;p&gt;Well, The main benefit of Headless CMS is that it can be used to create an API for your website, which means you can have more control over your data and how it&amp;rsquo;s displayed on your site.&lt;/p&gt;
&lt;p&gt;This type of CMS also has many other benefits:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt;
because there is no frontend interface, the server doesn&amp;rsquo;t have to process as much data and can therefore handle more requests at once. Headless CMSs are also faster than traditional CMSs because they don&amp;rsquo;t need to load all of the content on every page request, which saves time and resources on both server and client side.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Flexibility:&lt;/strong&gt;
You can use any frontend you want, which means developers can choose the best one for their project. This also means that you don&amp;rsquo;t have to worry about updating the frontend when there are changes in the backend.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Future-proof-ness:&lt;/strong&gt;
There is no guarantee that the epic looking theme you chose on wordpress or that wordpress itself will still be in the vogue in the future. Does that mean your content is not valuable anymore? Heck no, your content should not die with the platform and using an API like the ones in Headless CMS will ensure that the content can always be shown wherever you please.&lt;/p&gt;
&lt;h2 id="jamstack-and-the-rise-of-headless"&gt;JAMstack and the rise of Headless&lt;/h2&gt;
&lt;p&gt;JAM stands for Javascript, API and Markup. It refers to the approach of decoupling the frontend, backend and the database which you would find on a traditional website and distributing it over these three services.&lt;/p&gt;
&lt;p&gt;Basically, a Javascript Application retrieves the content from a latent Headless CMS through an API in the form of pre-rendered static markup.
This lets them condense the entire app and serve it over a CDN which leads to blazing fast performance and easily scalable applications.&lt;/p&gt;
&lt;p&gt;All this has lead to the rapid adoption of JAMstack applications through platforms like Vercel, Netlify, Github Pages etc. and along with that, brought forth a rise in popularity for Headless CMS platforms.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/jamstack.png" alt="JAM Stack"&gt;&lt;/p&gt;
&lt;h2 id="so-whats-the-catch-chief"&gt;So What&amp;rsquo;s the Catch, Chief?&lt;/h2&gt;
&lt;p&gt;Well, the main disadvantage of headless CMS is that it requires more technical knowledge than traditional CMS. This means that it might not be suitable for beginners who are just starting out with web development.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/Awsbill.jpeg" alt="Awsbill.jpeg"&gt;&lt;/p&gt;
&lt;p&gt;Apart from the complexity, there&amp;rsquo;s a cost factor too. You know that wordpress hosting is quite affordable nowadays and the websites who provide really good offers on shared hosting are a dime a dozen. However, you&amp;rsquo;re going to have to shell out a little bit more if you are going to opt for a headless CMS solution like Strapi. Not to mention the cost of development if you are not really well versed with the tooling.&lt;/p&gt;</description></item></channel></rss>