<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tjb</title>
    <description>The latest articles on DEV Community by tjb (@tjb).</description>
    <link>https://dev.to/tjb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F157241%2Ffc91ad44-e055-4597-86f5-efefe1addc96.png</url>
      <title>DEV Community: tjb</title>
      <link>https://dev.to/tjb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tjb"/>
    <language>en</language>
    <item>
      <title>LunarVim + vim-iced Configuration</title>
      <dc:creator>tjb</dc:creator>
      <pubDate>Mon, 12 Feb 2024 03:09:45 +0000</pubDate>
      <link>https://dev.to/tjb/lunarvim-vim-iced-configuration-47ik</link>
      <guid>https://dev.to/tjb/lunarvim-vim-iced-configuration-47ik</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq6j5gxszv2jo0h4fx9po.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq6j5gxszv2jo0h4fx9po.jpeg" alt="Image description" width="750" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wanted to create a quick blog post on how to get &lt;a href="https://github.com/liquidz/vim-iced"&gt;vim-iced&lt;/a&gt; to work with LunarVim since I struggled to get it working and the instructions on vim-iced's website are focused around vim or Neovim.&lt;/p&gt;

&lt;p&gt;Here are the steps that worked for me!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the following plugins to your &lt;code&gt;config.lua&lt;/code&gt; file.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  {
    "guns/vim-sexp"
  },
  {
    "liquidz/vim-iced",
    config = function()
      vim.g.iced_enable_default_key_mappings = true
    end
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: make sure to allow LunarVim to install the plugins! &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the &lt;code&gt;iced&lt;/code&gt; command to your &lt;code&gt;PATH&lt;/code&gt; by using the path to the &lt;code&gt;vim-iced&lt;/code&gt; plugin that LunarVim installed.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PATH=$PATH:~/.local/share/lunarvim/site/pack/lazy/opt/vim-iced/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the following command in your terminal to confirm you are able to use the &lt;code&gt;iced&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iced version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The installed version of &lt;code&gt;vim-iced&lt;/code&gt; will show after running this command. &lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpp2zfba0pa6lkwmfr4d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpp2zfba0pa6lkwmfr4d.png" alt="terminal image of iced version" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your Clojure project with LunarVim and navigate to a Clojure file. Open the terminal and run &lt;code&gt;iced repl&lt;/code&gt; to start the REPL. Navigate back to your Clojure file and run &lt;code&gt;:IcedConnect&lt;/code&gt; to connect to the REPL. You will see a message that says "Connected." &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Congrats! You are now able to use vim-iced with LunarVim!&lt;/p&gt;

</description>
      <category>lunarvim</category>
      <category>clojure</category>
      <category>usability</category>
    </item>
    <item>
      <title>API Server in Clojure - Part 1</title>
      <dc:creator>tjb</dc:creator>
      <pubDate>Wed, 22 Jul 2020 20:47:16 +0000</pubDate>
      <link>https://dev.to/tjb/api-server-in-clojure-part-1-4h0g</link>
      <guid>https://dev.to/tjb/api-server-in-clojure-part-1-4h0g</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5xbye6npv4bnc5xb7cic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5xbye6npv4bnc5xb7cic.png" alt="Alt Text" width="396" height="116"&gt;&lt;/a&gt;&lt;br&gt;
Disclaimer: I am new to Clojure.&lt;/p&gt;

&lt;p&gt;Now that everyone has read the disclaimer, some background on myself. &lt;/p&gt;

&lt;p&gt;By trade, I am a web developer. I primarily use Java, Spring Boot, Angular, and TypeScript. I was introduced to Clojure by a fellow teammate and I was instantly hooked. &lt;/p&gt;

&lt;p&gt;Being a web developer I thought the best way to learn Clojure was to create an API server. Needless to say, it took &lt;strong&gt;a lot&lt;/strong&gt; of scouring the internet, using the fantastic &lt;a href="http://clojurians.net/"&gt;Clojure Slack community&lt;/a&gt;, and lots of reading of dispersed blog posts to be able to glue all the pieces together. &lt;/p&gt;

&lt;p&gt;I finally understood how to get an API server up and running but I also understood a lot of people's frustration when trying to get into Clojure. Specifically web development with Clojure. There is no "one-stop-shop, show me how". &lt;/p&gt;

&lt;p&gt;So here we are. I am going to, hopefully, give all new Clojure peeps a resource that will help them get an API server up and running with a database. &lt;/p&gt;



&lt;p&gt;You will be using: Clojure (duh!), Leiningen, Compojure, Ring, Component, and HoneySQL. Don't worry if you do not know what all these things do, you will be by the end of this blog post. &lt;/p&gt;

&lt;p&gt;This is part of a three-part series on creating an API server with routes and database interactivity. &lt;/p&gt;

&lt;p&gt;Grab a glass of water (or your favorite beverage), a comfy chair, and let's get to work. &lt;/p&gt;
&lt;h2&gt;
  
  
  Series
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;API Server in Clojure - Part 1&lt;/li&gt;
&lt;li&gt;API Server in Clojure - Part 2&lt;/li&gt;
&lt;li&gt;API Server in Clojure - Part 3&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  JDK &amp;amp; Leiningen
&lt;/h2&gt;

&lt;p&gt;Before you do anything you need to install a Java Development Kid (JDK). I prefer using &lt;a href="https://sdkman.io/"&gt;SDKMAN!&lt;/a&gt; to manage my JDKs. &lt;/p&gt;

&lt;p&gt;After this, you will need to install Leiningen. Leiningen is an automation tool used for creating and managing Clojure projects. Instructions can be found on the &lt;a href="https://leiningen.org/"&gt;official Leiningen website&lt;/a&gt;. &lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a Clojure application
&lt;/h2&gt;

&lt;p&gt;You will need to create your Clojure application using Leiningen. Change into the directory where you want your project to live and then run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lein new app your-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Leiningen will create a base Clojure project for you to work in. Your project structure should look similar to this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;your-app
└─doc
└─resources    
└─src
│   └─your-app
|       └─core.clj 
└─test
└─.gitignore
└─.hgignore
└─CHANGELOG.md
└─LICENSE
└─project.clj
└─README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that you have your application, let's install the dependencies you will need to get your API server up and running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Dependencies
&lt;/h2&gt;

&lt;p&gt;There are several dependencies you will need to set up your API server for success. Below is a quick blurb for each and then instructions on where these dependencies go in your project.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/ring-clojure/ring"&gt;Ring - HTTP Server&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;A wildly used HTTP server library is Ring. It is a library with all the standard features you will need to get an HTTP server up and running. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/weavejester/compojure"&gt;Compojure - Routing&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Compojure will allow you to build out robust routes for your API server with all the standard RESTful methods like &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;PATCH&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/stuartsierra/component"&gt;Component - Lifecycle Management&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;This library allows us to manage some lifecycles that you will want in your application. The one you will want to use is when the application boots up, you want to create an HTTP server and when the HTTP server is created, you want to create a connection to our database. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt;, &lt;a href="https://github.com/seancorfield/next-jdbc"&gt;JDBC Connection&lt;/a&gt;, &amp;amp; &lt;a href="https://github.com/seancorfield/honeysql"&gt;Honey SQL&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;For this project you will be using PostgreSQL. Follow the link in the header to get it installed on your machine. &lt;/p&gt;

&lt;p&gt;Honey SQL helps you to generate SQL queries without having to handroll all your queries into strings. It is a fantastic DSL. It will also make your code more readable.&lt;/p&gt;

&lt;p&gt;To add dependencies you will need to open the &lt;code&gt;project.clj&lt;/code&gt; file and you will see a key called &lt;code&gt;:dependencies&lt;/code&gt;. This data structure is where you will add any dependencies you will need for your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;project.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defproject&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;your-app&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"0.1.0-SNAPSHOT
   ...
   :dependencies [[org.clojure/clojure "&lt;/span&gt;&lt;span class="mf"&gt;1.10&lt;/span&gt;&lt;span class="n"&gt;.1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
                  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ring&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"1.8.1"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
                  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;compojure&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"1.6.1"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
                  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;honeysql&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"1.0.444"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
                  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;seancorfield/next.jdbc&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"1.0.13"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
                  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;org.postgresql/postgresql&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"42.2.10.jre7"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
                  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;com.stuartsierra/component&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"0.4.0"&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, you will create a component that will be in charge of starting and stopping the server. &lt;/p&gt;

&lt;h2&gt;
  
  
  Spin up a Server
&lt;/h2&gt;

&lt;p&gt;Create a new directory called &lt;code&gt;system&lt;/code&gt;. The path to this newly created directory should be &lt;code&gt;src/your-app/system&lt;/code&gt;. Within this directory create a new file called &lt;code&gt;server.clj&lt;/code&gt;. In this file, you will create the HTTP server component. &lt;/p&gt;

&lt;p&gt;Create a &lt;a href="https://clojure.org/reference/namespaces"&gt;namespace&lt;/a&gt; for this Clojure file. You will need to import two additional namespaces from the dependencies you installed earlier. Those will be the jetty adapter that is bundled with Ring and the component namespace. Your namespace should look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;system.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ns&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;your-app.system.server&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;:require&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ring.adapter.jetty&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;jetty&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;com.stuartsierra.component&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;component&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create two functions: &lt;code&gt;start-server&lt;/code&gt; and &lt;code&gt;stop-server&lt;/code&gt;. These will be helper functions that your component will call in it's &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;stop&lt;/code&gt; lifecycle functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;system.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;start-server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s"&gt;"Helper function to start the server when the component's start function is called"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;jetty/run-jetty&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="no"&gt;:port&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Integer.&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)})]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, you are passing a port variable that the jetty server will run on. You might have noticed already that you will be getting an error on the keyword &lt;code&gt;handler&lt;/code&gt;. The &lt;code&gt;jetty/run-jetty&lt;/code&gt; requires a handler as its first argument. Ideally, in this argument, you would pass in a handler method for your routes. You will get to that in a bit but for now, let's create a function called &lt;code&gt;handler&lt;/code&gt; that responds with a map full of mocked request data. It will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;system.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="no"&gt;:status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="no"&gt;:headers&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
   &lt;/span&gt;&lt;span class="no"&gt;:body&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"Hello Clojure, Hello Ring!"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The handler will expect an argument but in this temporary example you will not be using the argument. &lt;/p&gt;

&lt;p&gt;Now for the &lt;code&gt;stop-server&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;system.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;stop-server&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="s"&gt;"Helper function to stop the server when the component's stop function is called"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;dissoc&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:server&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, you will want to disassociate the server when the application is stopped. &lt;/p&gt;

&lt;p&gt;Component time. You will now create a component that will call these two helper functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;system.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defrecord&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WebServer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;component/Lifecycle&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;assoc&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;start-server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;stop-server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;:server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;dissoc&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:server&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;defrecord&lt;/code&gt; creates a component named &lt;code&gt;WebServer&lt;/code&gt; that takes in a single argument that is the port number. As you can see, we have called the &lt;code&gt;start-server&lt;/code&gt; helper function on the &lt;code&gt;start&lt;/code&gt; function of the component's lifecycle, which is the running HTTP server. Identically you will call the &lt;code&gt;stop-server&lt;/code&gt; helper function on the &lt;code&gt;stop&lt;/code&gt; function of the component's lifecycle. &lt;/p&gt;

&lt;p&gt;The final function you will need is a function that kicks off the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;system.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;web-server&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="s"&gt;"Map web server to component"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map-&amp;gt;WebServer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="no"&gt;:port&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be the function that you will call in the main method. Speaking of the main method...let's create one!&lt;/p&gt;

&lt;p&gt;Navigate to &lt;code&gt;core.clj&lt;/code&gt;. You can remove the &lt;code&gt;foo&lt;/code&gt; function if you'd like. Now you will need to create a main method that calls the &lt;code&gt;web-server&lt;/code&gt; function within your &lt;code&gt;system.server&lt;/code&gt; namespace. You will need two namespaces in &lt;code&gt;core.clj&lt;/code&gt;. The namespace of &lt;code&gt;system.server&lt;/code&gt; as well as &lt;code&gt;component&lt;/code&gt; from the dependency you installed earlier. Your namespace should look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;core.clj&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ns&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;you-app.core&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;:require&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;your-app.system.server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;com.stuartsierra.component&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;component&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now create a main method that will take in a single argument for the port number and then call the function that will kick off the web server component. The method should look something similar to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;core.clj&lt;/span&gt;&lt;span class="w"&gt;

 &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;-main&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;component/start&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;component/system-map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;:web-server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;server/web-server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will use the &lt;a href="https://clojure.org/guides/threading_macros"&gt;thread macro&lt;/a&gt; to start the component and create a system map. The system map is a key-value pair of all components that are active. More information can be found about these functions by looking at the repositories provided in the links above. &lt;/p&gt;

&lt;p&gt;Now you should be able to get a server up and running! Head to your terminal and use the command &lt;code&gt;lein run 4000&lt;/code&gt;. In your browser head to &lt;code&gt;localhost:4000&lt;/code&gt;. You should see a message of "Hello Clojure, Hello Ring!" from your handler above. &lt;/p&gt;

&lt;p&gt;So you now have an HTTP server up and running. &lt;/p&gt;

&lt;p&gt;Yahoo! &lt;/p&gt;

&lt;p&gt;Stay tuned. In the next part of this series, you will hook up your HTTP server with some routes. &lt;/p&gt;

&lt;p&gt;There is a &lt;a href="https://github.com/tjb/clojure-web-api-skeleton"&gt;GitHub repository&lt;/a&gt; that has this in a skeleton ready to go. &lt;/p&gt;

&lt;p&gt;Always remember: Be kind. Be compassionate. Be positive. &lt;/p&gt;

</description>
      <category>clojure</category>
      <category>api</category>
      <category>webdev</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
