Last active
December 17, 2015 15:39
-
-
Save juliangruber/5633248 to your computer and use it in GitHub Desktop.
traffic-light written in wisp
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
; /server.js | |
(:require [express :as express]) | |
(:require [request :as request]) | |
(:require ["./lib/color" :as color]) | |
(def app (express)) | |
(.use app (.favicon express)) | |
(.get app "/:url" (fn [req res] | |
(def url (.-url (.-params req))) | |
(def start (.now Date)) | |
(def timeout (.MAX_VALUE Number)) | |
(if (.-timeout (.-query req)) | |
(set! timeout (.-timeout (.-query req)))) | |
(request url {timeout 5000} (fn [err _res] | |
(if (not err) (do | |
(if (.-regex (.-query req)) | |
(set! err (not (.match (.-body _res) (RegExp. (.-regex (.-query req))))))) | |
(if (.-not-regex (.-query req)) | |
(set! err (.match (.-body _res) (RegExp. (.-not-regex (.-query req)))))))) | |
(def dt (- timeout (.now Date) start)) | |
(.sendfile res | |
(+ __dirname "/images/" (color err (if _res (.-statusCode _res)) dt) ".png")))))) | |
; /lib/color.js | |
(fn [err status dt] ( | |
(if (or err (not (= status 200))) "red") | |
(if (< dt 0) "yellow") | |
"green" | |
)) | |
; /bin/traffic-light.js | |
(:require [".." :as traffic]) | |
(:require [optimist :as optimist]) | |
(.usage optimist "Start traffic-light server. | |
Usage: $0 [options]") | |
(.describe optimist "port" "Port to listen on") | |
(.default optimist "port" 4000) | |
(.alias optimist "p" "port") | |
(.describe optimist "help" "Print usage instructions") | |
(def argv (.-argv optimist)) | |
(if (.-help argv) | |
(.showHelp optimist) | |
(.listen traffic (.-port argv) (fn [] | |
(.log console (+ "traffic-light listening on port " (.-port argv))) | |
))) | |
; /test/server.js | |
(:require ["express" :as "express"]) | |
(def app (express)) | |
(.get app "/green" (fn [req res] | |
(.send res "ok"))) | |
(.get app "/red" (fn [req res] | |
(.status res 500) | |
(.end res "ok"))) | |
(.get app "/regex-bad" (fn [req res] | |
(.end res "an error occured"))) | |
(.get app "/regex-good" (fn [req res] | |
(.end res "ok"))) | |
; /test/test.js | |
(:require ["../lib/color" :as color]) | |
(:require [should :as should]) | |
(:require [express :as express]) | |
(:require [supertest :as request]) | |
(:require [".." :as light]) | |
(:require [fs :as fs]) | |
(def red (.readFileSync fs (+ __dirname "/../images/red.png"))) | |
(def yellow (.readFileSync fs (+ __dirname "/../images/yellow.png"))) | |
(def green (.readFileSync fs (+ __dirname "/../images/green.png"))) | |
(def server (require "./server")) | |
(.listen server 9990) | |
(after (fn [] (.close server))) | |
(describe "color" (fn [] | |
(it "should show red" (fn [] | |
(.equal (.-should (color {} 200 -1)) "red") | |
(.equal (.-should (color null 500 -1)) "red"))) | |
(it "should show yellow" (fn [] | |
(.equal (.-should (color null 200 -1)) "yellow"))) | |
(it "should show green" (fn [] | |
(.equal (.-should (color null 200 0)) "green"))))) | |
(describe "regex" (fn [] | |
(it "should be red when the regex isn't matched" (fn [done] | |
(def req (request light)) | |
(.get req (+ "/" (encodeURIComponent "http://localhost:9990/regex-bad") "/?regex=ok")) | |
(.expect req "Content-Type" "image/png") | |
(.expect req 200) | |
(.end req (fn [err res] | |
(if err | |
(done err) | |
(do | |
(.equal (.-should (.-text res))(.toString red)) | |
(done))))))))) | |
; etc... | |
(describe "favicon" (fn [] | |
(it "should have one" (fn [done] | |
(def req (request light)) | |
(.get req "/favicon.ico") | |
(.expect req "Content-Type" "image/x-icon") | |
(.expect req 200 done))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment