Skip to content

A repo of scripts and mini guides for running Clojure as a cgi-script on shared hosts with Babashka

License

Notifications You must be signed in to change notification settings

jaidetree/clj-cgi-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clojure on Shared Hosts with Babashka Example

How does it work?

A static Babashka binary uploaded to a shared host can run Clojure files as a cgi-script. Essentially we can use Clojure as if it were PHP!

Rationale

Create a MVP or simple web resource using Clojure on a cheap shared account. It can easily support multiple projects!

Live Demo

https://cgi.eccentric-j.com/

Learn More

An article that covers the rationale, requirements, and setup in more detail:

https://eccentric-j.com/blog/clojure-like-its-php.html

Benchmarks

Using multitime 1.4 within the development docker setup, the tests ran the example entrypoint metal.clj script 20 times.

multitime -n20 -s0 ./metal.clj

Results

MeanStd.Dev.MinMedianMax
real0.2110.0130.1870.2100.239
user0.0920.0080.0770.0920.103
sys0.0520.0070.0450.0500.071

What’s Included

  1. Live Example Source
  2. A guide to building static pods with Docker
  3. Depjar: A Babashka CLI script to create single jars of a clojars artifact
  4. A Docker environment to test babashka web scripts

Live Example Source

The source files that are on the live demo. The libs and binaries are only place-holders. Download or build the libs, replace the place-holders, and upload those to a shared host.

If you are using this as a starting template be sure to replace any <username> references in example/public_html/cgi.eccentric-j.com/metal.clj as well as

Building Static Pods

While some pods offer a static build in their releases page like https://github.com/babashka/babashka-sql-pods/releases. It may be necessary to build them locally then upload it to the server. This directory contains a docker file and instructions to help speed the process up.

Depjar

Since tools like `clj` or `lein` will not be available on the shared host, bb can create uberjars we can upload. Depjar is a script that takes a deps.edn files, uses Babashka to install deps, then create an uberjar for each library.

./depjar/depjar.clj ./deps.edn

Docker Development Environment

If you are having trouble getting it to work on a shared host you can at least use the development docker environment to try it out.

See the Readme to learn how to run the site.

Credit

In the Doom Emacs Discord, it was Taco (Elais Player) that came up with the idea and rushsteve1 that helped me understand how it would work.

Special Thanks

Big thanks in the Clojurians’ Slack to Borkdude (Michiel Borkent) for creating Babashka and Didibus for helping me figure out the first test Clojure web script.

About

A repo of scripts and mini guides for running Clojure as a cgi-script on shared hosts with Babashka

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published