Last active
June 22, 2021 09:00
-
-
Save turbo/d6c5021625f334e458854e85ba11cdc5 to your computer and use it in GitHub Desktop.
Write a Fennel Web App using Lapis and Turbo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 | |
(local turbo (require "turbo")) | |
(local render_html (. (assert (require "lapis.html")) :render_html)) | |
(fn render [parent expr] | |
(parent:add_header "Content-Type" "text/html") | |
(parent:write (render_html #(html expr)))) | |
; Handler that takes no argument, just like in the hello world example | |
(local IndexHandler (doto (class :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' | |
(local UserHandler (doto (class :UserHandler turbo.web.RequestHandler) | |
(tset :get (fn [self name] | |
(self:write (.. "Username is " name)))))) | |
; Handler that takes two integers as arguments and adds them.. | |
(local AddHandler (doto (class :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