upselling.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) '("submit.scm"))
  65. (respond
  66. '((body (h1 "hello")))
  67. )
  68. ]
  69. ((equal? (request-path-components request) '("about"))
  70. (respond
  71. '((body
  72. (p "About")))))
  73. ((equal? (request-path-components request) '("contact"))
  74. (respond
  75. '((h1 "contact")
  76. (body
  77. (p "contact")))))
  78. ((equal? (string-length (uri->string (request-uri request))) 1)
  79. (respond
  80. #:style "style.css"
  81. #:sxml
  82. '((body
  83. (main (@ (class body))
  84. (div (@ (class "content"))
  85. (h1 "Upselling")
  86. (form (@ (action "/submit.scm")
  87. (method "post"))
  88. (ul
  89. (li
  90. (label (@ (for "confirmation"))
  91. "Confirmation #:")
  92. (input (@ (type "text") (id "confirmation") (name "confirmation"))))
  93. (li
  94. (label (@ (for "los"))
  95. "Length of Stay:")
  96. (input (@ (type "text") (id "los") (name "los")))
  97. (input (@ (type "submit") (id "los") (name "los")))))))
  98. )))
  99. ))
  100. ;;The next three are probably not necessary.
  101. ;;I'll probably just have nginx serve these kinds of files
  102. ((regexp-exec css-pattern (uri->string (request-uri request)))
  103. (values (build-response)
  104. "style.css file content goes here."))
  105. ((regexp-exec js-pattern (uri->string (request-uri request)))
  106. (values (build-response)
  107. "javascript.js file content goes here."))
  108. ((regexp-exec sxml-pattern (uri->string (request-uri request)))
  109. (values (build-response)
  110. "sxml->xml is run on file content."))
  111. (else (not-found request))))
  112. (run-server serve-page)
  113. ;; building URIs
  114. ;; (uri->string
  115. ;; (build-uri 'http #:host "www.gnu.org"
  116. ;; #:port 55
  117. ;; #:path "/documentation/emacs/index.scm"
  118. ;; #:query "hello=5"
  119. ;; #:fragment "cool-spot"
  120. ;; ))