Last active
September 4, 2018 15:04
-
-
Save alexander-yakushev/cda17049d08de063ecd9e0169d4db43e to your computer and use it in GitHub Desktop.
Clojure livecoding @ Grammarly
This file contains 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
(set-env! | |
:dependencies | |
'[[http-kit "LATEST"] | |
[hiccup "LATEST"] | |
[cheshire "LATEST"] | |
[compojure "LATEST"]]) |
This file contains 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
(ns meetup | |
(:require [org.httpkit.server :as http] | |
[hiccup2.core :as html] | |
[cheshire.core :as json] | |
[compojure.core :refer [defroutes GET]] | |
[compojure.route :refer [not-found]] | |
[ring.middleware.params :refer [wrap-params]] | |
[ring.middleware.cookies :refer [wrap-cookies]] | |
[clojure.string :as str])) | |
;; Chuck Norris | |
(defn get-joke [] | |
(-> (slurp "http://api.icndb.com/jokes/random") | |
(json/decode true) | |
(get-in [:value :joke]))) | |
;; (get-joke) | |
;; Turtles block | |
(def turtles ["Leonardo" "Raphael" | |
"Michelangelo" "Donatello"]) | |
(def turtles-imgs | |
{"Leonardo" "https://pre00.deviantart.net/9045/th/pre/i/2016/175/9/0/tmnt_leonardo__colors__by_le0arts-da7hjun.jpg" | |
"Raphael" "https://upload.wikimedia.org/wikipedia/en/7/72/Raphael_%28Teenage_Mutant_Ninja_Tutles%29.jpg" | |
"Michelangelo" "https://i.pinimg.com/736x/7d/78/c6/7d78c6c2a15adcac4ccfc9dc29cfafad--alonso-michelangelo.jpg" | |
"Donatello" "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTa8t8JM0fDDPC1k-l1Z3LYT4ew50lHXhLQnIQgZ299HBQQByYH"}) | |
(def votes (atom (zipmap turtles (repeat 4 0)))) | |
(defn valid-turtle? [turtle] | |
(contains? (set turtles) turtle)) | |
;; (valid-turtle? "Raphael") | |
;; (valid-turtle? "Bebop") | |
(defn cast-vote [turtle] | |
(when (valid-turtle? turtle) | |
(swap! votes (fn [votes-state] | |
(update votes-state turtle inc))))) | |
;; Usernames block | |
(def animals (str/split-lines (slurp "animals.txt"))) | |
(def colors | |
(map (fn [line] | |
(str/split line #" ")) | |
(str/split-lines (slurp "colors.txt")))) | |
(def usernames | |
(ref | |
(cycle | |
(shuffle | |
(for [animal animals | |
color colors] | |
{:name (str (first color) " " animal) | |
:hex-color (second color)}))))) | |
(def users (ref {})) | |
(defn get-user [user-id] | |
(dosync | |
(when-not (get @users user-id) | |
(let [username (first @usernames)] | |
(alter users (fn [users-state] | |
(assoc users-state user-id username))) | |
(alter usernames rest))) | |
(get @users user-id))) | |
(defn index-page [req] | |
(let [user-id (or (get-in req [:cookies "user-id" :value]) | |
(str (rand-int 10000000))) | |
user (get-user user-id)] | |
{:body (str | |
(html/html | |
[:html | |
[:head | |
[:meta | |
{:name "viewport" | |
:content "width=device-width, initial-scale=1.0"}]] | |
[:body | |
[:h1 "Hello, " [:font {:color (:hex-color user)} | |
(:name user)]] | |
[:a {:href "/"} [:h2 "Reload"]]]])) | |
:cookies {"user-id" user-id}})) | |
(defn not-found-page [] | |
{:body (str | |
(html/html | |
[:html | |
[:head | |
[:meta | |
{:name "viewport" | |
:content "width=device-width, initial-scale=1.0"}]] | |
[:body | |
[:center | |
[:h1 "I HAZ EATEN UR PAGE."] | |
[:img {:src "http://en.bcdn.biz/Images/2018/6/12/27565ee3-ffc0-4a4d-af63-ce8731b65f26.jpg"}] | |
[:a {:href "/"} [:h2 "Take me home!"]]]]]))}) | |
(defroutes main-handler | |
(GET "/" [:as req] (index-page req)) | |
(not-found (not-found-page))) | |
(def server-killswitch (atom nil)) | |
(defn start-server [port] | |
(when @server-killswitch (@server-killswitch)) ;; On restart, kill the previous server. | |
(let [ks (http/run-server (wrap-params (wrap-cookies #'main-handler)) | |
{:port port})] | |
(reset! server-killswitch ks))) | |
;; (start-server 8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment