Skip to content

Instantly share code, notes, and snippets.

Created Oct 18, 2019
What would you like to do?
; vi: ft=clojure
; THIS IS A Fennel SOURCE FILE (.fnl) !NOT! clojure!
; you need to use luajit to run this, after using fennel --compile
; install lapis and turbo from luarocks
; When running with Fennel directly, you need to --globals all used HTML tags
(local turbo (require "turbo"))
(local render_html (. (assert (require "lapis.html")) :render_html))
(macro makedo [receiver classtable ...]
`(local ,receiver (doto (class ,(tostring receiver) ,classtable)
(fn render [parent expr]
(parent:add_header "Content-Type" "text/html")
(parent:write (render_html #(html expr))))
; ----------- real app starts below ------------------------------------
; Handler that takes no argument, just like in the hello world example
(makedo IndexHandler turbo.web.RequestHandler
(tset :get (fn [self]
(render self
#(div { :class "main container" } #(do
(h1 #(do
(text "SuperApp")
(sup "2019.3")))
(p #(do
(text "This is the debug interface. Use the ")
(a { :href "#" } "API")
(text " to build stuff. Don't rely on this interface. If you're not a developer, you shouldn't be here.")))))))))
; Handler that takes a single argument 'username'
(makedo UserHandler turbo.web.RequestHandler
(tset :get (fn [self name]
(self:write (.. "Username is " name)))))
; Handler that takes two integers as arguments and adds them..
(makedo AddHandler turbo.web.RequestHandler
(tset :get (fn [self a1 a2]
(self:write (.. "Result is " (tostring (+ a1 a2)))))))
(let [app (turbo.web.Application:new [
; No arguments, will work for 'localhost:8888' and 'localhost:8888/'
["/$" IndexHandler]
; Use the part of the url after /user/ as the first argument to
; UserHandler:get
["/user/(.*)$" UserHandler]
; Find two int's separated by a '/' after /add in the url
; and pass them as arguments to AddHandler:get
["/add/(%d+)/(%d+)$" AddHandler]
(app:listen 8888)
(: (turbo.ioloop.instance) :start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment