Finding Joy: First Steps

Lately I've been having fun playing around with the Janet programming language. Janet is a small Lisp with a Clojure-inspired syntax. A few of my favorite things about it: Janet apps start instantaneously, consume little memory, and can be compiled to a single binary for easy deployment. For someone with a personal web app creation habit and a simple vps, Janet seems like a great choice.

Since most of my personal projects are web apps, I quickly found the Joy web framework by Sean Walker. Joy is new and changing, but already has quite a bit of functionality: routing, middleware, database support, migrations, an html dsl with form helpers, etc.

This post is me grokking Joy by writing a small tutorial.

A First App

Joy uses the Halo http server, which acts as a bridge between your Janet code and the Sandbird embeddable http server.

To understand how Joy works, we'll build up to it by starting with a basic Halo app. After installing Janet, we need to create a directory to put our project in:

$ mkdir hello && cd $_

Janet comes with the jpm build tool for managing project dependencies and making builds. To use it, we need to create a project.janet file and include Halo as a dependency:

(declare-project
  :name "hello"
  :description "a project for learning Joy"
  :dependencies ["https://github.com/joy-framework/halo"])

Now let's create a simple Halo app in a file called hello.janet:

(import halo)

(defn hello [request]
  {:status 200 :body "Hello, world!" :headers {"Content-Type" "text/plain"}})

(halo/server hello 8080)

With those 2 files in place, we can install the Halo dependency with jpm and use Janet to run our app:

$ jpm deps
# bunch of install stuff removed
$ janet hello.janet 
Server listening on [localhost:8080] ...

Wow, that started fast! Now we can make requests:

$ curl -i http://localhost:8080
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 13

Hello, world!

Nice and responsive and easy on memory:

Activity Monitor

Taking a Break

To keep things moving, these posts will be small and focused. The next one will be on handler functions.

Check these out:

comments powered by Disqus