start-guile-server.scm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (import
  2. (scheme base)
  3. (scheme read)
  4. (scheme write)
  5. (scheme process-context)
  6. (scheme load)
  7. (scheme cxr)
  8. (web server)
  9. (web request)
  10. (web response)
  11. (web uri)
  12. (ice-9 iconv)
  13. (macduffie sxml)
  14. (macduffie json))
  15. (include "./web-server.scm")
  16. (include "./canvas.js")
  17. (define default-port 43334)
  18. (define (not-found request)
  19. (values (build-response #:code 404)
  20. (string-append "Resource not found: "
  21. (uri->string (request-uri request)))))
  22. (define (request-path-components request)
  23. (split-and-decode-uri-path (uri-path (request-uri request))))
  24. (define (hello-hacker-handler request body)
  25. (define path-components (request-path-components request))
  26. (when (null? path-components)
  27. (set! path-components '("index.html")))
  28. (case (string->symbol (string-downcase (car path-components)))
  29. ((index.html)
  30. (values '((content-type . (text/html)))
  31. (sxml->xml index-page)))
  32. ((draw.js)
  33. (values '((content-type . (application/javascript)))
  34. script-page))
  35. ((api)
  36. (if (null? (cdr path-components))
  37. (not-found request)
  38. (case (string->symbol (string-downcase (cadr path-components)))
  39. ((newid)
  40. (values '((content-type . (application/json)))
  41. (api-new-id!)))
  42. ((pushbuffer)
  43. (values '((content-type . (application/json)))
  44. (api-push-buffer! (bytevector->string body "utf-8"))))
  45. ((pushbulkbuffer)
  46. (values '((content-type . (application/json)))
  47. (api-push-bulk-buffer! (bytevector->string body "utf-8"))))
  48. ((pullbuffer)
  49. (values '((content-type . (application/json)))
  50. (json-write-string (api-pull-buffer! (bytevector->string body "utf-8")))))
  51. ((clearbuffer)
  52. (values '((content-type . (text/plain)))
  53. (api-clear-buffer!)))
  54. (else
  55. (not-found request)))))
  56. (else
  57. (not-found request))))
  58. (define (main args)
  59. (define current-port
  60. (if (null? (cdr args))
  61. default-port
  62. (string->number (cadr args))))
  63. (run-server hello-hacker-handler 'http `(#:port ,current-port)))
  64. (main (command-line))