janet-lang / circlet

HTTP server library for janet
Other
82 stars 10 forks source link

Circlet

Circlet is an HTTP and networking library for the janet language. It provides an abstraction out of the box like Clojure's ring, which is a server abstraction that makes it easy to build composable web applications.

Circlet uses mongoose as the underlying HTTP server engine. Mongoose is a portable, low footprint, event based server library. The flexible build system requirements of mongoose make it very easy to embed in other C programs and libraries.

Installing

You can add Circlet as a dependency in your project.janet:

(declare-project
  :name "web framework" :description "A framework for web development"
  :dependencies ["https://github.com/janet-lang/circlet.git"])

You can also install it system-wide with jpm:

sh jpm install circlet

Usage

Creating a server

You can create a HTTP server using the circlet/server function. The function is of the form (circlet/server handler port &opt ip-address) and takes the following parameters:

The server runs immediately after creation.

Request

The handler function takes a single parameter representing the request. The request is a Janet table containing all the information about the request. It contains the following keys:

Response

The return value of the handler function must be a Janet table containing at least the status key with an integer value that corresponds to the HTTP status of the response (e.g. 200 for success).

Other possible keys include:

There is also special key :kind you can use. There are two possible values for this key:

Middleware

Circlet also allows for the creation of different “middleware”. Pieces of middleware can be thought of as links in a chain of functions that are used to consume the HTTP request. The handler function can be thought of as a piece of middleware and other middleware should match the signature and return type of the handler function, i.e. accept and return a Janet table.

Middleware can be created in one of two ways. A user can define a function with the appropriate signature and return type or use Circlet’s circlet/middleware function to coerce an argument into a piece of middleware. Middleware pieces are often higher-order functions (meaning that they return another function). This allows for parameterization at creation time.

Provided middleware

There are three basic pieces of middleware provided by Circlet:

Example

The below example starts a very simple web server on port 8000.

(import circlet)

(defn myserver
 "A simple HTTP server" [request]
 {:status 200
  :headers {"Content-Type" "text/html"} :body "<!doctype html><html><body><h1>Hello.</h1></body></html>"})

(circlet/server myserver 8000)

Development

Building

Building requires Janet to be installed on the system, as well as the jpm tool (installed by default with Janet).

jpm build

You can also just run jpm to see a list of possible build commands.

Testing

Run a server on localhost with the following command

jpm test

This example is more involved, and shows all the functionality described in this document.

License

Unlike janet, Circlet is licensed under the GPL license in accordance with mongoose.