Skip to content

Instantly share code, notes, and snippets.

@ordnungswidrig
Created December 10, 2019 11:12
Show Gist options
  • Save ordnungswidrig/a88157c29006ad5e3a4b0dc48acc9b70 to your computer and use it in GitHub Desktop.
Save ordnungswidrig/a88157c29006ad5e3a4b0dc48acc9b70 to your computer and use it in GitHub Desktop.
Webserver in clojurescript on espruino
(set! *warn-on-infer* true)
(def ^js wifi (js/require "Wifi"))
(def ^js http (js/require "http"))
(def n (atom 0))
(defn build-response []
(str
"<html>"
"<h1>Hello Clojure</h1>"
"<p>This is request no. " (swap! n (fn [^number x] (inc x))) "</p>\n"
"<p>Running ClojureScript with Espruino on a ESP32 Wrover</p>\n"
"<p>Free Heap: " (.-free (js/process.memory)) " Bytes</p>\n"
"</html>"
))
(defn ^:export handle-request [^js req, ^js res]
(.writeHead res 200 #js{"Content-Type" "text/html"})
(.write res (build-response))
(.end res ""))
(defn ^:export start-server []
(js/print "Starting server...")
(-> (.createServer http handle-request) (.listen 80))
(js/print "Webserver Created on port 80"))
(defn start-wifi []
(.restore wifi))
(defn ^:export init []
(js/print "Connecting to Wifi...");
(start-wifi)
(start-server))
(js/print "Starting...")
(init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment