<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Mazhar's Blog</title>
    <description>Entrepreneur, Product Enthusiast, Tech Expert, Designer, Brander
</description>
    <link>http://www.mazharahmed.me/</link>
    <atom:link href="http://www.mazharahmed.me/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 14 May 2023 15:05:25 +0000</pubDate>
    <lastBuildDate>Sun, 14 May 2023 15:05:25 +0000</lastBuildDate>
    <generator>Jekyll v3.9.3</generator>
    
      <item>
        <title>Join Seedr</title>
        <description>&lt;p&gt;Seedr.cc is a cloud-based service that allows users to download and stream torrent files. It provides a convenient and secure way to access torrent content without the need for a traditional torrent client on your device. Here are some key points about Seedr.cc:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Account Creation: To use Seedr.cc, you need to create an account. You can create an account using your Google account or by signing up directly on the Seedr.cc website.&lt;/li&gt;
  &lt;li&gt;Storage and Tasks: With a free account, you initially get 2GB of storage, which can be increased by completing simple tasks provided on the website. These tasks likely involve activities such as referring friends, sharing content, or participating in promotional offers.&lt;/li&gt;
  &lt;li&gt;Premium Membership: Seedr.cc offers premium memberships with various options. These memberships provide additional storage space, faster download speeds, and other enhanced features. The specific details of premium memberships, such as pricing and benefits, can be found on the Seedr.cc website.&lt;/li&gt;
  &lt;li&gt;Downloading Torrents: Once you have created an account, you can either copy the torrent link or upload the torrent file to your Seedr.cc account. Seedr.cc will then download the torrent file to their servers, and you will be provided with a direct link to download the file to your device.&lt;/li&gt;
  &lt;li&gt;Streaming and Accessibility: Seedr.cc uses streaming technology that allows you to directly access and stream videos, music, and other content from your torrent collection. This can be done on any device with an internet connection, without the need to download the entire file.&lt;/li&gt;
  &lt;li&gt;Privacy and Security: Seedr.cc offers high-level transport encryption to ensure the privacy and security of your data. They claim to protect against malware, viruses, and external tracking, providing a secure environment for accessing and downloading torrent content.&lt;/li&gt;
  &lt;li&gt;Speed and Efficiency: Seedr.cc boasts fast download speeds due to their high-speed infrastructure. Torrents are typically downloaded to the cloud within a few minutes or even seconds, allowing for quick access to the content you desire.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s always a good practice to visit the official Seedr.cc website to get the most up-to-date and accurate information about their services, features, and terms of use.&lt;/p&gt;

&lt;p&gt;Join now:
Website URL: &lt;a href=&quot;https://www.seedr.cc/&quot;&gt;https://www.seedr.cc/&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 12 May 2023 05:45:52 +0000</pubDate>
        <link>http://www.mazharahmed.me/join-seedr</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/join-seedr</guid>
        
        <category>seedr</category>
        
        <category>torrent</category>
        
        <category>install</category>
        
        <category>web</category>
        
        <category>saas</category>
        
        
        <category>torrent</category>
        
      </item>
    
      <item>
        <title>Use Gearman Job Server in PHP</title>
        <description>&lt;p&gt;Gearman is a Job Server which has many language interfaces. If you are familiar with Python Celery then you will understand at a glance. Unlike Celery, Gearman is a task runner. The only difference is in Gearman, you have to program your worker too. I love Gearman because not only tasks but also the worker can be written with any suitable language. Also the core of Gearman is built with C/C++, that’s why it is very very fast. Say, you are working with PHP and you need a background task runner, go for Gearman. Don’t worry, many big companies are using it like  Craig’s List, Tumblr, Yelp, Etsy etc.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://www.mazharahmed.me/stack.png&quot; alt=&quot;How Does Gearman Work&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s create a Background Task Runner for our PHP Application. To do that we need to install gearman server on our PC / Server first. If we are using Ubuntu then it is very easy like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt install gearman-job-server&lt;/code&gt; and there are tons of article how to install gearman. Feel free to install it first. Then we have to install the PHP PECL extension for gearman. In Ubuntu we can simply run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo pecl install gearman&lt;/code&gt;. Again, there are tons of articles on how to install gearman libraries on your PHP. When you have done with the installations, let’s jump directly into coding. Don’t forget to run the &lt;strong&gt;gearman job server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First lets make the worker. Make a new file named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gearman-worker.php&lt;/code&gt; with:&lt;/p&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$worker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GearmanWorker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$worker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addServer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// additional server address can be passed here&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$worker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;ping&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;GearmanJob&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$job&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;received: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$job&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;sending data: ping &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            
            &lt;span class=&quot;nv&quot;&gt;$job&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sendData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ping &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;waiting&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// your other function defination goes here or you may call other files / modules etc&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;waiting&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$worker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s our simple worker with a function named &lt;strong&gt;ping&lt;/strong&gt;. You can add your other functions too. Now let’s make a client and call that function we just created. Make a file named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gearman-client.php&lt;/code&gt; with following content:&lt;/p&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GearmanClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addServer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setDataCallback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;GearmanTask&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;data: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;sending&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addTask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ping&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// you can also call tasks async using $client-&amp;gt;doBackground()&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;runTasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we have to keep the worker running. Simply just keep the worker running in our terminal like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;php gearman-worker.php&lt;/code&gt; and then run the client from another terminal like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;php gearman-client.php&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we look at the client terminal its output is like:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sending
data: ping 1
data: ping 2
data: ping 3
data: ping 4
data: ping 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And the worker terminal will output like:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;waiting
received: H:[network &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;:[task &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
sending data: ping 1
sending data: ping 2
sending data: ping 3
sending data: ping 4
sending data: ping 5
waiting
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: You have to keep the worker running.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a simple way of making own worker using Gearman. There are plenty of ways we can use gearman and that’s the fun of using it. Hope you guys like it.&lt;/p&gt;

&lt;p&gt;Thanks a lot.&lt;/p&gt;
</description>
        <pubDate>Thu, 28 Mar 2019 00:00:00 +0000</pubDate>
        <link>http://www.mazharahmed.me/use-gearman-job-server-in-php</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/use-gearman-job-server-in-php</guid>
        
        <category>php</category>
        
        
      </item>
    
      <item>
        <title>Generate REST API from DB Schema in Crystal</title>
        <description>&lt;p&gt;I had a MySQL schema from a booking.com like platform. I wanted to generate REST API very fast from my MySQL database tables. Like I have a table named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hotels&lt;/code&gt;. I want to generate following endpoints:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GET    /api/hotels
GET    /api/hotels/:id
POST   /api/hotels
PUT    /api/hotels/:id
DELETE /api/hotels/:id
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I tried to use &lt;strong&gt;Haskell&lt;/strong&gt; but the security was very low on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postgrest&lt;/code&gt; package in &lt;strong&gt;Haskell&lt;/strong&gt;.  The &lt;strong&gt;postgrest&lt;/strong&gt; is amazing, it generates the api endpoints from a database. But the problem is it will delete all the rows in a table if I perform a &lt;em&gt;DELETE&lt;/em&gt; request in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/api/hotels&lt;/code&gt;. So, I can not use it.&lt;/p&gt;

&lt;p&gt;Finally found that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Kemal Rest API&lt;/code&gt; is the perfect for doing this which is on &lt;strong&gt;Crystal Lang&lt;/strong&gt;. Suppose I have a table named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hotels&lt;/code&gt;. This is what I did. I installed &lt;strong&gt;Crystal Lang&lt;/strong&gt;. I ran &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shards init app hotel&lt;/code&gt; to generate an empty project. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shards&lt;/code&gt; is the dependency manager, task runner for crystal. So after this I have a folder named hotel which has the following structure:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.
├── LICENSE
├── README.md
├── db.sql
├── shard.lock
├── shard.yml
├── spec
│   ├── hotel_spec.cr
│   └── spec_helper.cr
└── src
    └── hotel.cr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I have modified the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shard.yml&lt;/code&gt; file and added following lines at the end.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;dependencies:
  db:
    github: crystal-lang/crystal-db
  kemal:
    github: kemalcr/kemal
  kemal-rest-api:
    github: blocknotes/kemal-rest-api
  mysql:
    github: crystal-lang/crystal-mysql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I have ran the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shards install&lt;/code&gt; to install the libraries and modified the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hotel.cr&lt;/code&gt; file like following:&lt;/p&gt;

&lt;div class=&quot;language-crystal highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;db&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;mysql&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;kemal&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;kemal-rest-api&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;json&quot;&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;DB_NAME&lt;/span&gt;       &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;hotel&quot;&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;DB_CONNECTION&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;mysql://hotel@localhost/&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;DB_NAME&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyModel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;KemalRestApi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Adapters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;CrystalDbModel&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;DB_CONNECTION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;hotels&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;WebApp&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;KemalRestApi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Resource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;MyModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;KemalRestApi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ALL_ACTIONS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;prefix: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;api&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;singular: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;hotel&quot;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Routes&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;content_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;application/json&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;name: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;--&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;info: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;--&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;version: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;0.0.1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_json&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generate_routes!&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Starts Kemal&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Kemal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;to generate these routes automatically:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GET    /api/hotels
GET    /api/hotels/:id
POST   /api/hotels
PUT    /api/hotels/:id
DELETE /api/hotels/:id
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At last I have ran &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shards build&lt;/code&gt; to make the binary executable file. And ran it like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./bin/hotel&lt;/code&gt;. And the API is working properly.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Crystal&lt;/strong&gt; code is just like &lt;strong&gt;Ruby&lt;/strong&gt; language but as fast as &lt;strong&gt;C&lt;/strong&gt;. What I need to do next is make an array of tables and it will generate the API endpoints. I don’t need to take hassle about images and users because we already have other microservices to handle those. Also, this API will never be exposed to clients rather another API wrapper will fetch data from it. So, I found it perfect fit for my job.&lt;/p&gt;

&lt;p&gt;Thus I have made my hotel API up in just couple of hours.&lt;/p&gt;
</description>
        <pubDate>Sat, 23 Feb 2019 00:00:00 +0000</pubDate>
        <link>http://www.mazharahmed.me/generate-rest-api-from-db-schema-in-crystal</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/generate-rest-api-from-db-schema-in-crystal</guid>
        
        <category>crystal</category>
        
        <category>haskell</category>
        
        
      </item>
    
      <item>
        <title>Your First Web Application with Spock</title>
        <description>&lt;p&gt;The Spock web framework for Haskell gives you a light but complete foundation to build web servers on, be it for traditional server-side rendered applications, or APIs for single-page applications. Compared to the Scotty framework, Spock is slightly richer in its feature set, and is more opinionated regarding the application architecture. In this episode we explore the basics of Spock and in-memory server state, as we build a note keeper application.&lt;/p&gt;

&lt;h5 id=&quot;show-notes&quot;&gt;Show Notes&lt;/h5&gt;
&lt;p&gt;We are starting with an empty project called notekeeper, and a Cabal file specifying an executable. To get started, we add dependencies for Spock, http-types, text, and mtl.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-cabal&quot;&gt;name:                notekeeper
version:             0.1.0.0
category:            Web
build-type:          Simple
cabal-version:       &amp;gt;=1.10

executable notekeeper
  main-is:            Main.hs
  build-depends:      base &amp;gt;=4.10 &amp;amp;&amp;amp; &amp;lt;4.11
                    , Spock
                    , http-types
                    , text
                    , mtl
  hs-source-dirs:     src
  default-language:   Haskell2010
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Main.hs&lt;/code&gt;, we start out by importing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Web.Spock&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Web.Spock.Config&lt;/code&gt; modules. Those are all we need to get a basic server running.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Main&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Web.Spock&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Web.Spock.Config&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The function we need in our definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runSpock&lt;/code&gt;, which has the type:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;runSpock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Middleware&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We run our server on port 8080, with the middleware returned by a function called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spock&lt;/code&gt;. It takes a configuration, and our application. We’ll leave those as typed holes for now, and see what we need.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;runSpock&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spock&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_cfg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_app&lt;/code&gt; hole tells us we need some value of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpockM conn0 sess0 st0 ()&lt;/code&gt;, which is the Spock monad for defining an application. It also tells us that the type variables used as parameters are ambiguous.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Found hole: _app :: SpockM conn0 sess0 st0 ()
Where: ‘conn0’ is an ambiguous type variable
       ‘sess0’ is an ambiguous type variable
       ‘st0’ is an ambiguous type variable
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_cfg&lt;/code&gt; hole tells us that we need a value of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpockCfg conn0 sess0 st0&lt;/code&gt;, and that the same type variables as we saw before are ambiguous. There is a connection between the type parameters of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpockM&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpockCfg&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Found hole: _cfg :: SpockCfg conn0 sess0 st0
Where: ‘conn0’ is an ambiguous type variable
       ‘sess0’ is an ambiguous type variable
       ‘st0’ is an ambiguous type variable
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We begin by defining &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app&lt;/code&gt; locally, not doing any routing, only returning ().&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;kr&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;runSpock&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spock&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_cfg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This would work, but the type variables are still ambiguous for the configuration. If we move the definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app&lt;/code&gt; to the top-level, and give it an explicit type signature, we can pin down those type arguments.&lt;/p&gt;

&lt;p&gt;The type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpockM&lt;/code&gt; is parameterized by four types:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the database connection type (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conn&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;the session type (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sess&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;the state type (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;st&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;the monadic return value’s type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Initially, we won’t have any database, sessions, or state, so we use the () type for all of them.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SpockM&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that we’ve specified which types we are using in our application, the typed hole &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_cfg&lt;/code&gt; has no ambiguous variables. To construct a configuration value, we use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;defaultSpockCfg&lt;/code&gt;, taking a session configuration, a pool or connection for a database, and an initial state. The database parameter cannot be a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt; value, and we’ll leave it as a typed hole to illustrate why.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;Found&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hole&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_db&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PoolOrConn&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;PoolOrConn is parameterized by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt;, and there is only one value with that type: the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PCNoDatabase&lt;/code&gt; constructor defined by Spock. Using that, we obtain a configuration for our Spock application, and we have a rather useless, but working web server.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defaultSpockCfg&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PCNoDatabase&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;runSpock&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spock&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To run the web server, and have it reload on source code changes, we can use the excellent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ghcid&lt;/code&gt; tool. By default, it only reloads your code and prints any type errors, but if we override the test command using -T, we can have it run the main function when the code successfully compiles.&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ghcid &lt;span class=&quot;nt&quot;&gt;-T&lt;/span&gt; :main
Running test...

Spock is running on port 8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In our web browser, we can see that we get a “404 Not Found” when requesting http://localhost:8080. We’ll keep ghcid running in the background.&lt;/p&gt;

&lt;h5 id=&quot;defining-routes&quot;&gt;Defining Routes&lt;/h5&gt;

&lt;p&gt;Before we begin defining routes, we’ll create a type alias for our application type to factor out the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpockM&lt;/code&gt; details. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Server&lt;/code&gt; takes a single type argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;, the monadic return value type. We use it as the type of our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app&lt;/code&gt; definition.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;SpockM&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We define a route for GET requests to the root path, responding with a text greeting.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we get a type error, explaining that we can’t pass a regular &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;text&lt;/code&gt; function. We enable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OverloadedStrings&lt;/code&gt; to construct a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt; value instead.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;{-# LANGUAGE OverloadedStrings #-}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Reloading the web browser, we see our greeting “Hello!” displayed.&lt;/p&gt;

&lt;h5 id=&quot;rendering-html&quot;&gt;Rendering HTML&lt;/h5&gt;

&lt;p&gt;Responding with plain text in a web application is a little dull. Instead, we’ll respond with HTML using the html function, and some lovingly hand-crafted markup.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;h1&amp;gt;Hello!&amp;lt;/h1&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now get a proper heading, at least.&lt;/p&gt;

&lt;p&gt;Constructing HTML markup in strings is not great, though. Instead, we’ll use Lucid, an embedded DSL for HTML markup. In our Cabal file, we add the dependency lucid, along with Spock-lucid, a small library integrating Spock and Lucid.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-cabal&quot;&gt;executable notekeeper
  main-is:            Main.hs
  build-depends:      base &amp;gt;=4.10 &amp;amp;&amp;amp; &amp;lt;4.11
                    , Spock
                    , http-types
                    , text
                    , mtl
                    , lucid
                    , Spock-lucid
  hs-source-dirs:     src
  default-language:   Haskell2010
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Back in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Main.hs&lt;/code&gt;, we import the lucid function from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Web.Spock.Lucid&lt;/code&gt;, along with the entire &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Lucid&lt;/code&gt; module.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Web.Spock.Lucid&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;lucid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Lucid&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can use lucid instead of html, and the Lucid DSL to construct our markup. Using do notation, we can define sibling elements, which in our case will be a heading and a paragraph.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lucid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;h1_&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello!&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;p_&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;How are you today?&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Reloading the web browser, we see the two elements rendered.&lt;/p&gt;

&lt;h5 id=&quot;keeping-notes-with-server-state&quot;&gt;Keeping Notes with Server State&lt;/h5&gt;

&lt;p&gt;We are ready to implement the note keeping functionality of our application. Instead of using a server state of type (), we’ll define and use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ServerState&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Note&lt;/code&gt; data types.&lt;/p&gt;

&lt;p&gt;The server state consists of an IORef holding a list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Note&lt;/code&gt; values. An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IORef&lt;/code&gt; is a variable that can be mutated atomically in the IO monad.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ServerState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ServerState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IORef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Note&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A note has an author, and contents, both of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To use Text and IORef we need to import them.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.IORef&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, we change the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Server&lt;/code&gt; type alias to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ServerState&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-haskel&quot;&gt;type Server a = SpockM () () ServerState a
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Navigating to the next type error we get, we see that we cannot use an application requiring a state of type ServerState with a configuration with state of type ().&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Couldn't match type ‘ServerState’ with ‘()’
      Expected type: SpockM () () () ()
      Actual type: Server ()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We construct a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ServerState&lt;/code&gt; by mapping the constructor over a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;newIORef&lt;/code&gt; action, with an empty list of notes, and use it in our configuration.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ServerState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newIORef&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defaultSpockCfg&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PCNoDatabase&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;runSpock&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spock&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It compiles again! To have something that we can render, we add two notes to the initial state.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ServerState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;newIORef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Alice&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Must not forget to walk the dog.&quot;&lt;/span&gt;
             &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Bob&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Must. Eat. Pizza!&quot;&lt;/span&gt;
             &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defaultSpockCfg&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PCNoDatabase&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;runSpock&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8080&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spock&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we have something to render, so we change the greeting markup to instead list the notes in our state. We’ll use an unordered list, constructed using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ul_&lt;/code&gt;, and for each note render a list item, containing the note author and contents.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lucid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;h1_&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Notes&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ul_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forM_&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notes'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;note&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;li_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;toHtml&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;note&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;: &quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;toHtml&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contents&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;note&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forM_&lt;/code&gt; function is not in scope, so we need to import it.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Control.Monad&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;forM_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Also, we don’t have a list of notes available. We need to read it from the IORef in our server state. The getState action in Spock returns our server state, from which we can extract and read the IORef.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;notes'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readIORef&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lucid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;h1_&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Notes&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ul_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forM_&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notes'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;note&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;li_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;toHtml&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;author&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;note&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;&quot;: &quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;toHtml&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contents&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;note&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The usage of getState might remind you of ask from the Reader monad.&lt;/p&gt;

&lt;p&gt;The readIORef action has to be lifted into the ActionCtxt, which is the monadic type for Spock routes. Thus, we import and use liftIO.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Control.Monad.IO.Class&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;liftIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;No more errors, so we refresh the web browser and see our list of notes rendered.&lt;/p&gt;
</description>
        <pubDate>Fri, 11 Jan 2019 00:00:00 +0000</pubDate>
        <link>http://www.mazharahmed.me/your-first-web-application-with-spock</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/your-first-web-application-with-spock</guid>
        
        <category>haskell</category>
        
        
      </item>
    
      <item>
        <title>How to call a C function in Golang</title>
        <description>&lt;p&gt;I’m writting this article after a very long time. This happens due to the work pressure in my daily life. There is not enough free time to write any article, even customize my own sites / portfolios.&lt;/p&gt;

&lt;p&gt;Okay, today I will be showing you guys how to call native C (you can call C++ methods too) functions using Golang. To accomplish this, I would think that you guys have this things installed on your system:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;go&lt;/li&gt;
  &lt;li&gt;gcc&lt;/li&gt;
  &lt;li&gt;pkg-config&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s create a project in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$GOPATH/src&lt;/code&gt;. I have created my project in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$GOPATH/src/github.com/mazhar266/goc&lt;/code&gt;. There I’ve added a file named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square.c&lt;/code&gt; with the following function:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we will call this function from our &lt;strong&gt;go&lt;/strong&gt; program. Let’s create a blank &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main.go&lt;/code&gt; file. Let’s just start writting the basic structure like:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fmt&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; package and include our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square.c&lt;/code&gt; file which contains our &lt;strong&gt;square&lt;/strong&gt; function. Add this line after package declaration:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;//#include&amp;lt;square.c&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;C&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This first line imports our &lt;strong&gt;C&lt;/strong&gt; program (the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square.c&lt;/code&gt; file, you will understand if you are a C programmer). The second line imports the basic functionality in &lt;strong&gt;Go&lt;/strong&gt; to talk with &lt;strong&gt;C&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now make a method / function which will call the &lt;strong&gt;C&lt;/strong&gt; function and return the result. This is for making the main method clean.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;makeSquare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;//Convert Go ints to C ints&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;aC&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;//Convert C.int result to Go int&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can easily understand what this method does. It converts integer argument &lt;strong&gt;a&lt;/strong&gt; for &lt;strong&gt;Go&lt;/strong&gt; to integer argument for &lt;strong&gt;C&lt;/strong&gt;. And also converts the returned result from &lt;strong&gt;C&lt;/strong&gt; integer to &lt;strong&gt;Go&lt;/strong&gt; integer.&lt;/p&gt;

&lt;p&gt;So, we can call the function from the main function like:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;makeSquare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now if we run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go run main.go&lt;/code&gt; we will see the output &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;81&lt;/code&gt; in the console.&lt;/p&gt;

&lt;p&gt;That’s the basic of calling a &lt;strong&gt;C&lt;/strong&gt; function from &lt;strong&gt;Golang&lt;/strong&gt;. For dealing with complex &lt;strong&gt;C&lt;/strong&gt; programs which uses thirdparty libraries, your have to maintain &lt;strong&gt;CFLAGS&lt;/strong&gt; and &lt;strong&gt;LDFLAGS&lt;/strong&gt; from your go application. We will see that in another article.&lt;/p&gt;

&lt;p&gt;You can find the sample source code here &lt;a href=&quot;https://github.com/mazhar266/goc&quot;&gt;https://github.com/mazhar266/goc&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Wed, 26 Dec 2018 00:00:00 +0000</pubDate>
        <link>http://www.mazharahmed.me/how-to-call-a-c-function-in-golang</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/how-to-call-a-c-function-in-golang</guid>
        
        <category>go</category>
        
        <category>c</category>
        
        
      </item>
    
      <item>
        <title>Welcome to Jekyll!</title>
        <description>&lt;p&gt;You’ll find this post in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jekyll serve&lt;/code&gt;, which launches a web server and auto-regenerates your site when a file is updated.&lt;/p&gt;

&lt;p&gt;To add new posts, simply add a file in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory that follows the convention &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YYYY-MM-DD-name-of-post.ext&lt;/code&gt; and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.&lt;/p&gt;

&lt;p&gt;Jekyll also offers powerful support for code snippets:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hi, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Tom'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; prints 'Hi, Tom' to STDOUT.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check out the &lt;a href=&quot;http://jekyllrb.com/docs/home&quot;&gt;Jekyll docs&lt;/a&gt; for more info on how to get the most out of Jekyll. File all bugs/feature requests at &lt;a href=&quot;https://github.com/jekyll/jekyll&quot;&gt;Jekyll’s GitHub repo&lt;/a&gt;. If you have questions, you can ask them on &lt;a href=&quot;https://talk.jekyllrb.com/&quot;&gt;Jekyll Talk&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Thu, 09 Feb 2017 07:35:52 +0000</pubDate>
        <link>http://www.mazharahmed.me/welcome-to-jekyll</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/welcome-to-jekyll</guid>
        
        
        <category>jekyll</category>
        
        <category>update</category>
        
      </item>
    
      <item>
        <title>Reset MySQL Root Password on Ubuntu</title>
        <description>&lt;p&gt;&lt;strong&gt;First please ensure that you really forgot the root password&lt;/strong&gt;.&lt;/p&gt;
&lt;pre class=&quot;brush:shell&quot;&gt;mysql -u root -p&lt;/pre&gt;

&lt;p&gt;If you really forgot the password then resetting the password is needed. Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dpkg-reconfigure&lt;/code&gt; to reconfigure the root user.&lt;/p&gt;

&lt;p&gt;####Check the MySQL version
First  of all check the MySQL server version like this:&lt;/p&gt;

&lt;pre class=&quot;brush:shell&quot;&gt;apt-cache policy mysql-server&lt;/pre&gt;

&lt;p&gt;the above command gave me output like:&lt;/p&gt;

&lt;pre&gt;Installed: 5.5.37-0ubuntu0.12.04.1&lt;/pre&gt;

&lt;p&gt;####Start reconfiguring the MySQL
Start using the following shell command:&lt;/p&gt;

&lt;pre class=&quot;brush:shell&quot;&gt;sudo dpkg-reconfigure mysql-server-*.*&lt;/pre&gt;

&lt;p&gt;Please, put your MySQL version name instead of mysql-server-&lt;em&gt;.&lt;/em&gt; . For me I have used mysql-server-5.5 . It will stop the database and promt for reconfiguration.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://www.mazharahmed.me/assets/images/2016/Feb/GHEyY.png&quot; alt=&quot;dpkg-reconfigure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can now enter your new password for the root user and so on. After the reconfiguration is completed the MySQL server will be automatically started.&lt;/p&gt;

&lt;p&gt;Now you can login using your new password like this:&lt;/p&gt;

&lt;pre class=&quot;brush:shell&quot;&gt;mysql -u root -p&lt;/pre&gt;

&lt;p&gt;That’s all for now. Thank you.&lt;/p&gt;
</description>
        <pubDate>Sun, 28 Feb 2016 12:58:48 +0000</pubDate>
        <link>http://www.mazharahmed.me/reset-mysql-root-password-on-ubuntu</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/reset-mysql-root-password-on-ubuntu</guid>
        
        <category>mysql</category>
        
        <category>linux</category>
        
        
      </item>
    
      <item>
        <title>How to setup a Cron job in Ubuntu</title>
        <description>&lt;p&gt;Most of my friends ask me how to setup a cron job in Ubuntu. They often face this situation when they have to setup a cron job for their application. I myself also work with the cron job feature according to my application’s demand. Here is how to setup a cron job in Ubuntu in less than a minute.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://www.mazharahmed.me/assets/images/2016/Feb/ubuntu-wallpaper-hq-wallpaper.jpg&quot; alt=&quot;Ubuntu&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can put a shell script in one of these folders: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/cron.daily&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/cron.hourly&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/cron.monthly&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/cron.weekly&lt;/code&gt;. This should run your script according to the folder name. Such as the scripts in cron.daily folder will be run on daily basis.&lt;/p&gt;

&lt;p&gt;If these are not enough for you then you can define your own using the crontab.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ crontab -e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will open personal crontab (cron configuration file) for you. Every lines are tasks that will be run according to the configuration written on that line. The lines are written in this format:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;minute hour day-of-month month day-of-week command
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For every number you can define a list by separating the numbers using a comma (,) . You can also use intervals like */20 which will run every 20th. If we put */20 at the minutes field then it will be equivalent to 0,20,40.&lt;/p&gt;

&lt;p&gt;Suppose:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* 0 * * * path/to/command
*/15 * * * * path/to/command2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first command will be run once a day when the time is 0 that means 12am. The second command will be run on every 15 minutes interval.&lt;/p&gt;

&lt;p&gt;That’s all guys, that’s how to setup a cron job in Ubuntu. Thank you.&lt;/p&gt;
</description>
        <pubDate>Thu, 18 Feb 2016 02:52:20 +0000</pubDate>
        <link>http://www.mazharahmed.me/how-to-setup-a-cron-job-in-ubuntu</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/how-to-setup-a-cron-job-in-ubuntu</guid>
        
        <category>linux</category>
        
        
      </item>
    
      <item>
        <title>Change the Bootstrap NavBar Breakpoint</title>
        <description>&lt;p&gt;&lt;img src=&quot;http://www.mazharahmed.me/assets/images/2015/Oct/Components-B7-Bootstrap.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I use mostly Bootstrap framework for my every projects. Although I like the Foundation framework. If the provided design does not seem proper for any of these framework I used to hard code the rest of the work. I often face a situation like designer wants a different breakpoint to trigger the mobile menu. Suppose a designer wants the mobile menu from the “md” size. This is quite easy in bootstrap. There are two ways of doing this. One is to modify the less source file and recompile it. The second one is directly write CSS codes.&lt;/p&gt;

&lt;p&gt;###Modifying the LESS file
Change the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@navbarCollapseWidth&lt;/code&gt; variable in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;less/variables.less&lt;/code&gt; file to your desired value. Then recompile the less file to produce the updated CSS file.&lt;/p&gt;

&lt;p&gt;###Modifying the CSS file
This is quite easy and I thing most of the people will use this method. Just Place this section in your stylesheet to overwrite the default behaviour.&lt;/p&gt;

&lt;pre class=&quot;brush:css;&quot;&gt;
/* showing the mobile menu */
@media (max-width: 1070px) {
    .navbar-header {
        float: none;
    }
    .navbar-toggle {
        display: block;
    }
    .navbar-collapse {
        border-top: 1px solid transparent;
        box-shadow: inset 0 1px 0 rgba(255,255,255,0.1);
    }
    .navbar-collapse.collapse {
        display: none!important;
    }
    .navbar-nav {
        float: none!important;
        margin: 7.5px -15px;
    }
    .navbar-nav&amp;gt;li {
        float: none;
    }
    .navbar-nav&amp;gt;li&amp;gt;a {
        padding-top: 10px;
        padding-bottom: 10px;
    }
}
&lt;/pre&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Note: Replace 1070px to your desired value.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That’s it, that’s how to change the default Bootstrap NavBar breakpoint.&lt;/p&gt;
</description>
        <pubDate>Thu, 29 Oct 2015 02:36:05 +0000</pubDate>
        <link>http://www.mazharahmed.me/change-the-bootstrap-navbar-breakpoint</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/change-the-bootstrap-navbar-breakpoint</guid>
        
        <category>css3</category>
        
        <category>css</category>
        
        <category>bootstrap</category>
        
        <category>less</category>
        
        <category>sass</category>
        
        
      </item>
    
      <item>
        <title>How to Install JAVA 8 on CentOS, RHEL and Fedora</title>
        <description>&lt;p&gt;Recently I was instructed to install Java 8 on our servers. There are plenty of reasons for doing that. Here’s how I have done that. Of-course you will need root access to your server. If your are using a shared hosting, then it will be wise to leave this thing to your hosting authority.&lt;/p&gt;

&lt;p&gt;###Download the Java 8
Lets keep the java source to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;####For 64 Bit Servers&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# cd /opt/
# wget --no-cookies --no-check-certificate --header &quot;Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie&quot; &quot;http://download.oracle.com/otn-pub/java/jdk/8u40-b25/jdk-8u40-linux-x64.tar.gz&quot;

# tar xzf jdk-8u40-linux-x64.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;####For 32Bit Servers&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# cd /opt/
# wget --no-cookies --no-check-certificate --header &quot;Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie&quot; &quot;http://download.oracle.com/otn-pub/java/jdk/8u40-b25/jdk-8u40-linux-i586.tar.gz&quot;

# tar xzf jdk-8u40-linux-i586.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;###Install Java 8
Now install it from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jdk1.8.0_40&lt;/code&gt; directory.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# cd /opt/jdk1.8.0_40/
# alternatives --install /usr/bin/java java /opt/jdk1.8.0_40/bin/java 2
# alternatives --config java


There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*  1           /opt/jdk1.8.0/bin/java
 + 2           /opt/jdk1.8.0_25/bin/java
   3           /opt/jdk1.8.0_40/bin/java

Enter to keep the current selection[+], or type selection number: 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now install rest of the utilities.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_40/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_40/bin/javac 2
# alternatives --set jar /opt/jdk1.8.0_40/bin/jar
# alternatives --set javac /opt/jdk1.8.0_40/bin/javac 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At this moment we have installed Java 8. So lets verify it if it’s working properly.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@tecadmin ~# java -version

java version &quot;1.8.0_40&quot;
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;###Setup system variables
Setup JAVA_HOME Variable&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# export JAVA_HOME=/opt/jdk1.8.0_40
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Setup JRE_HOME Variable&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# export JRE_HOME=/opt/jdk1.8.0_40/jre
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Setup PATH Variable&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# export PATH=$PATH:/opt/jdk1.8.0_40/bin:/opt/jdk1.8.0_40/jre/bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s the end. Now Java 8 is installed on your server.&lt;/p&gt;
</description>
        <pubDate>Sun, 09 Aug 2015 02:30:21 +0000</pubDate>
        <link>http://www.mazharahmed.me/how-to-install-java-8-on-centos-rhel-and-fedora</link>
        <guid isPermaLink="true">http://www.mazharahmed.me/how-to-install-java-8-on-centos-rhel-and-fedora</guid>
        
        <category>shell</category>
        
        <category>linux</category>
        
        
      </item>
    
  </channel>
</rss>
