points.scm 3.9 KB

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