example-2.scm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. (use-modules (web server))
  2. (use-modules (web request)
  3. (web response)
  4. (web uri))
  5. (define (request-path-components request)
  6. ;; just for showing what the functions do
  7. (display (simple-format #f "(request-uri request): ~a\n"
  8. (request-uri request)))
  9. (display (simple-format #f "(uri-path ...): ~a\n"
  10. (uri-path (request-uri request))))
  11. (display (simple-format #f "(split-and-decode-uri-path ...): ~a\n"
  12. (split-and-decode-uri-path (uri-path (request-uri request)))))
  13. ;; actual logic
  14. ;; split the string that represents the uri and decode any url-endoced things
  15. (split-and-decode-uri-path
  16. ;; get the uri path as a string from the request struct
  17. (uri-path
  18. ;; get the request struct
  19. (request-uri request))))
  20. (define (not-found request)
  21. (values (build-response #:code 404)
  22. (string-append "Resource not found: "
  23. (uri->string (request-uri request)))))
  24. (define (hello-hacker-handler request body)
  25. (if (equal? (request-path-components request) '("hacker"))
  26. ;; answer in plain text "Hello hacker!"
  27. (values '((content-type . (text/plain)))
  28. "Hello hacker!")
  29. (not-found request)))
  30. (run-server hello-hacker-handler)