handlers.scm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (library (handlers)
  2. (export not-found-404-handler
  3. debug-handler
  4. hello-world-handler
  5. minimal-static-asset-handler)
  6. (import
  7. (except (rnrs base) let-values)
  8. (only (guile) lambda* λ error when display sleep)
  9. ;; Guile modules
  10. ;; alist->hash-table
  11. (prefix (ice-9 hash-table) ice9-hash-table:)
  12. ;; Guile exception handling
  13. (ice-9 exceptions)
  14. (ice-9 session)
  15. ;; for bytevector operations
  16. (ice-9 binary-ports)
  17. ;; SRFIs
  18. ;; hash tables
  19. (prefix (srfi srfi-69) srfi-69:)
  20. ;; receive form
  21. (prefix (srfi srfi-8) srfi-8:)
  22. ;; let-values
  23. (prefix (srfi srfi-11) srfi-11:)
  24. ;; list utils
  25. (prefix (srfi srfi-1) srfi-1:)
  26. ;; web server, concurrent
  27. (fibers web server)
  28. ;; standard web library
  29. (web request)
  30. (web response)
  31. (web uri)
  32. (sxml simple)
  33. ;; custom modules
  34. (path-handling)
  35. (web-path-handling)
  36. (file-reader)
  37. (mime-types)
  38. (prefix (logging) log:)
  39. (templates)
  40. (response-utils)))
  41. ;; Next we define some handlers, which take care of handling
  42. ;; specific routes.
  43. (define not-found-404-handler
  44. (λ (request body)
  45. "Answer with a 404 HTTP status code."
  46. (values (build-response #:code 404)
  47. (string-append "requested resource not found: "
  48. (uri->string (request-uri request))))))
  49. (define debug-handler
  50. (lambda (request body)
  51. "The debug-handler will put all request headers into the
  52. rendered HTML, so that we can see them on the page."
  53. (log:debug "responding using debug handler")
  54. (respond
  55. ;; Inside respond the SXML will be put into a template,
  56. ;; so there is no need to add html or body tags.
  57. (debug-table-template request body))))
  58. (define hello-world-handler
  59. (lambda (request request-body)
  60. "A handler for a route."
  61. ;; A handler must return 2 values: The header and body
  62. ;; of the response.
  63. (values
  64. ;; Return the headers as first value (the bare
  65. ;; minimum).
  66. '((content-type . (text/plain)))
  67. ;; Then the response body. This is an example for
  68. ;; returning a string as second value, instead of a
  69. ;; procedure, which takes an output port.
  70. "Hello World!")))
  71. (define minimal-static-asset-handler
  72. (λ (request body)
  73. (log:debug "responding using minimal-static-asset-handler")
  74. (let ([status 200]
  75. [content-type-params '((charset . "utf-8"))]
  76. [content-type 'text/css]
  77. [extra-headers '()])
  78. (values (build-response #:code status
  79. ;; headers are an alist
  80. #:headers
  81. `((content-type . (,content-type ,@content-type-params))
  82. ,@extra-headers))
  83. "body {margin: none}"))))