1. David Krentzlin
  2. chicken-stuff

Commits

certainty  committed 4d10d1b

added sack, the rack-like thing

  • Participants
  • Parent commits 898f45d
  • Branches default

Comments (0)

Files changed (1)

File sack.scm

View file
  • Ignore whitespace
+;; 
+;; %%HEADER%%
+;;
+
+;; (sackup
+;;  (lambda (answer)
+;;    (answer 200 '((content-type "text/plain")) "My content")))
+
+;; To test this just run it with csi -s and access localhost:4000 with your browser
+
+
+(use spiffy)
+
+(define (test-binding _)
+  (values
+   (lambda (proc)
+     (proc "This is my input"))
+   (lambda (code headers text)
+     (printf "code: ~A headers: ~A text: ~A~%" code headers text))))
+
+
+(define ((sack-spiffy-handler handler not-found) _)
+  ;;current-request would have to be normalized to a sack-request or
+  ;;sth. 
+  (handler (current-request)))
+
+(define ((sack-spiffy-listener port) handler)
+  (handle-not-found
+   (let ((old-not-found (handle-not-found)))
+     (sack-spiffy-handler handler old-not-found)))
+  (start-server port: port))
+
+(define (sack-spiffy-responder code headers body)
+  (send-response code: code body: body header: headers))
+
+(define (sack-spiffy-binding port)
+  (values (sack-spiffy-listener port) sack-spiffy-responder))
+
+(define (sackup application #!key (server-binding test-binding) (port 8080))
+  (receive (listener responder) (server-binding port)
+    (listener
+     (lambda (request)
+       (application request responder)))))
+
+(define ((add-middleware middleware application) request respond)
+  (middleware request respond application))
+
+;; First test
+(define (my-app request respond)
+  (respond 200 '((content-type  "text/plain")) (string-downcase request)))
+
+;; middleware altering the request
+(define (modify-request request respond next)
+  (next "other request" respond))
+
+;; middleware altering the response
+(define (modify-response request respond next)
+  (next request (lambda (code headers body)
+                  (respond 500 headers body))))
+
+(sackup (add-middleware modify-response (add-middleware modify-request my-app)) server-binding: sack-spiffy-binding port: 4000)
+
+
+
+
+
+