handlers.scm 2.7 KB

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