web.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. (use-modules (web server)
  2. (web request)
  3. (web response)
  4. (web uri)
  5. (sxml simple)
  6. (ice-9 regex))
  7. ;; this is probably not necessary. I'd just have nginx serve these files
  8. (define css-pattern (make-regexp ".*css$" regexp/extended))
  9. (define js-pattern (make-regexp ".*js$" regexp/extended))
  10. (define sxml-pattern (make-regexp ".*sxml$" regexp/extended))
  11. (define (not-found request)
  12. (values (build-response #:code 404)
  13. (string-append "Resource not found: "
  14. (uri->string (request-uri request))
  15. " length is "
  16. (number->string (string-length (uri->string (request-uri request)))))))
  17. (define* (templatize title body #:optional style)
  18. `(html (head (title ,title)
  19. ,(when style
  20. '(link (@ (rel "stylesheet") (href "style.css") (type "text/css")))))
  21. (body ,@body)))
  22. (define* (respond #:optional body #:key
  23. (style #f)
  24. (status 200)
  25. (title "Hello hello!")
  26. (doctype "<!DOCTYPE html>\n")
  27. (content-type-params '((charset . "utf-8")))
  28. (content-type 'text/html)
  29. (extra-headers '())
  30. (sxml (and body (templatize title body))))
  31. (values (build-response
  32. #:code status
  33. #:headers `((content-type
  34. . (,content-type ,@content-type-params))
  35. ,@extra-headers))
  36. (lambda (port)
  37. (if sxml
  38. (begin
  39. (if doctype (display doctype port))
  40. (sxml->xml sxml port))))))
  41. (define (request-path-components request)
  42. (split-and-decode-uri-path (uri-path (request-uri request))))
  43. (define (serve-page request body)
  44. (cond
  45. ((equal? (request-path-components request) '("hacker"))
  46. (respond
  47. #:sxml
  48. '((h1 "hello world!")
  49. (body
  50. (p "Hey there!"))
  51. ;; (table
  52. ;; (tr (th "header") (th "value"))
  53. ;; ,@(map (lambda (pair)
  54. ;; `(tr (td (tt ,(with-output-to-string
  55. ;; (lambda () (display (car pair))))))
  56. ;; (td (tt ,(with-output-to-string
  57. ;; (lambda ()
  58. ;; (write (cdr pair))))))))
  59. ;; (request-headers request)))
  60. )))
  61. ((equal? (request-path-components request) '("about"))
  62. (respond
  63. '((body
  64. (p "About")))))
  65. ((equal? (request-path-components request) '("contact"))
  66. (respond
  67. '((h1 "contact")
  68. (body
  69. (p "contact")))))
  70. ((equal? (string-length (uri->string (request-uri request))) 1)
  71. (respond
  72. #:style "style.css"
  73. #:sxml
  74. '((body
  75. (div (@ (class body))
  76. (div (@ (class "content"))
  77. (h1 title)
  78. (p (@ (class "blue")) "Do not push me ok?" ))
  79. (aside (@ (class "aside"))
  80. (p "Hello")
  81. (p "Yes!")))))
  82. ))
  83. ;;The next three are probably not necessary.
  84. ;;I'll probably just have nginx serve these kinds of files
  85. ((regexp-exec css-pattern (uri->string (request-uri request)))
  86. (values (build-response)
  87. "style.css file content goes here."))
  88. ((regexp-exec js-pattern (uri->string (request-uri request)))
  89. (values (build-response)
  90. "javascript.js file content goes here."))
  91. ((regexp-exec sxml-pattern (uri->string (request-uri request)))
  92. (values (build-response)
  93. "sxml->xml is run on file content."))
  94. (else (not-found request))))
  95. (run-server serve-page)
  96. ;; building URIs
  97. ;; (uri->string
  98. ;; (build-uri 'http #:host "www.gnu.org"
  99. ;; #:port 55
  100. ;; #:path "/documentation/emacs/index.scm"
  101. ;; #:query "hello=5"
  102. ;; #:fragment "cool-spot"
  103. ;; ))