2023-10-30 20:42:28 +00:00
|
|
|
#lang racket
|
|
|
|
|
2023-10-31 20:46:45 +00:00
|
|
|
(require racket/date
|
|
|
|
net/url
|
2023-10-30 20:42:28 +00:00
|
|
|
xml
|
|
|
|
web-server/web-server
|
|
|
|
web-server/servlet-dispatch
|
2023-10-31 20:46:45 +00:00
|
|
|
web-server/dispatchers/dispatch
|
2023-10-30 20:42:28 +00:00
|
|
|
web-server/dispatch
|
|
|
|
(prefix-in files: web-server/dispatchers/dispatch-files)
|
|
|
|
(prefix-in filter: web-server/dispatchers/dispatch-filter)
|
|
|
|
(prefix-in sequencer: web-server/dispatchers/dispatch-sequencer)
|
|
|
|
web-server/dispatchers/filesystem-map
|
|
|
|
web-server/http)
|
|
|
|
|
2023-11-01 22:32:17 +00:00
|
|
|
(define-namespace-anchor anc)
|
|
|
|
(define ns (namespace-anchor->namespace anc))
|
|
|
|
|
2023-10-31 20:46:45 +00:00
|
|
|
|
2023-10-30 20:42:28 +00:00
|
|
|
(define (html-response content)
|
|
|
|
(response/full
|
|
|
|
200
|
|
|
|
#"OK"
|
|
|
|
(current-seconds)
|
|
|
|
TEXT/HTML-MIME-TYPE
|
|
|
|
'()
|
|
|
|
(list content)))
|
|
|
|
|
|
|
|
|
2023-10-31 20:46:45 +00:00
|
|
|
;; takes a resource-type (corresponds to the REST resource and the system path or
|
|
|
|
;; an index resource in the source path)
|
|
|
|
;; and a resource-processor (-> file-handle . http-response)
|
|
|
|
;; and returns a valid html-response irrespective of hx/ type fragment of html or full page
|
|
|
|
(define respond-resource-with-processor
|
|
|
|
(lambda (type resource-processor)
|
|
|
|
(case-lambda
|
|
|
|
[(req) (let ([res-path (path-add-extension
|
2023-11-09 04:10:28 +00:00
|
|
|
(if (and (non-empty-string? type) (not (eq? type "root")))
|
|
|
|
(build-path "source" type "index")
|
|
|
|
(build-path "source" "index" ))
|
|
|
|
#".txt")])
|
|
|
|
(displayln res-path)
|
2023-10-31 20:46:45 +00:00
|
|
|
(if (file-exists? res-path)
|
|
|
|
(html-response (resource-processor res-path))
|
|
|
|
(html-response (resource-processor "source/not-found.txt"))))]
|
2023-11-09 04:10:28 +00:00
|
|
|
[(req id) (let ([res-path
|
|
|
|
(path-add-extension
|
|
|
|
(build-path
|
|
|
|
"source"
|
|
|
|
(if (eq? type "root")
|
|
|
|
(if (eq? id "")
|
|
|
|
;; if "root/" something weird has happened
|
|
|
|
"index"
|
|
|
|
;; allows for loading "/this" and other root resources
|
|
|
|
(~a id))
|
|
|
|
(if
|
|
|
|
;; if "type/" id will be an empty string
|
|
|
|
(or (not (string? id)) (non-empty-string? id))
|
|
|
|
(build-path type (~a id))
|
|
|
|
;; and should return index for that type
|
|
|
|
(build-path type "index"))))
|
|
|
|
#".txt")])
|
2023-10-31 20:46:45 +00:00
|
|
|
(if (file-exists? res-path)
|
|
|
|
(html-response (resource-processor res-path))
|
|
|
|
(html-response (resource-processor "source/not-found.txt"))))])))
|
2023-10-30 20:42:28 +00:00
|
|
|
|
2023-10-31 20:46:45 +00:00
|
|
|
;;; httpx
|
|
|
|
;; sends only the requested resource
|
2023-10-30 20:42:28 +00:00
|
|
|
(define (xexpr-file->xml file)
|
|
|
|
(string->bytes/utf-8
|
|
|
|
(xexpr->string (read (open-input-file file)))))
|
|
|
|
|
2023-10-31 20:46:45 +00:00
|
|
|
(define (404-hx request)
|
2023-10-30 20:42:28 +00:00
|
|
|
(html-response (xexpr-file->xml "source/not-found.txt")))
|
|
|
|
|
|
|
|
|
|
|
|
(define-values (httpx-app reverse-httpx-uri)
|
|
|
|
(dispatch-rules
|
2023-11-09 04:10:28 +00:00
|
|
|
[("hx" "home") (respond-resource-with-processor "root" xexpr-file->xml)]
|
|
|
|
[("hx" "settled") (respond-resource-with-processor "settled" xexpr-file->xml)]
|
|
|
|
[("hx" "settled" (integer-arg)) (respond-resource-with-processor "settled" xexpr-file->xml)]
|
|
|
|
[("hx" "unsettled") (respond-resource-with-processor "unsettled" xexpr-file->xml)]
|
|
|
|
[("hx" "unsettled" (integer-arg)) (respond-resource-with-processor "unsettled" xexpr-file->xml)]
|
2023-10-31 20:46:45 +00:00
|
|
|
[("hx" "tagged") (respond-resource-with-processor "tagged" xexpr-file->xml)]
|
2023-11-09 04:10:28 +00:00
|
|
|
[("hx" "tagged" (string-arg)) (respond-resource-with-processor "tagged" xexpr-file->xml)]
|
|
|
|
[("hx" (string-arg)) (respond-resource-with-processor "root" xexpr-file->xml)]
|
2023-10-31 20:46:45 +00:00
|
|
|
[else 404-hx]))
|
2023-10-30 20:42:28 +00:00
|
|
|
|
|
|
|
;;; page-app
|
|
|
|
;; constructs entire page for each response
|
|
|
|
(define (make-page resource)
|
|
|
|
(string->bytes/utf-8
|
|
|
|
(xexpr->string
|
|
|
|
`(html ((lang "en"))
|
|
|
|
,(read (open-input-file "source/head.txt"))
|
|
|
|
(body
|
|
|
|
,(read (open-input-file "source/header.txt"))
|
2023-11-09 04:10:28 +00:00
|
|
|
(main ,(read (open-input-file resource))))))))
|
2023-10-30 20:42:28 +00:00
|
|
|
|
|
|
|
(define-values (page-app reverse-page-uri)
|
|
|
|
(dispatch-rules
|
2023-11-09 04:10:28 +00:00
|
|
|
[("") (respond-resource-with-processor "" make-page)]
|
|
|
|
[("home") (respond-resource-with-processor "" make-page)]
|
|
|
|
[("settled") (respond-resource-with-processor "settled" make-page)]
|
|
|
|
[("settled" (integer-arg)) (respond-resource-with-processor "settled" make-page)]
|
|
|
|
[("unsettled") (respond-resource-with-processor "unsettled" make-page)]
|
|
|
|
[("unsettled" (integer-arg)) (respond-resource-with-processor "unsettled" make-page)]
|
2023-10-31 20:46:45 +00:00
|
|
|
[("tagged") (respond-resource-with-processor "tagged" make-page)]
|
2023-11-09 04:10:28 +00:00
|
|
|
[("tagged" (string-arg)) (respond-resource-with-processor "tagged" make-page)]
|
|
|
|
[((string-arg)) (respond-resource-with-processor "root" make-page)]
|
2023-10-31 20:46:45 +00:00
|
|
|
[else not-found]))
|
|
|
|
|
2023-10-30 20:42:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; from /static
|
|
|
|
(define url->path/static (make-url->path "static"))
|
|
|
|
|
|
|
|
(define static-dispatcher
|
|
|
|
(files:make #:url->path (lambda (u)
|
|
|
|
(url->path/static
|
|
|
|
(struct-copy url u [path (cdr (url-path u))])))))
|
|
|
|
|
2023-10-31 20:46:45 +00:00
|
|
|
;; rss feed
|
|
|
|
(define (xml-response content)
|
|
|
|
(response/full
|
|
|
|
200
|
|
|
|
#"OK"
|
|
|
|
(current-seconds)
|
|
|
|
#"application/atom+xml; charset=utf-8"
|
|
|
|
'()
|
|
|
|
(list content)))
|
|
|
|
|
|
|
|
|
|
|
|
(define (rss-feed request)
|
|
|
|
(xml-response
|
2023-11-09 04:10:28 +00:00
|
|
|
(string->bytes/utf-8 (file->string "source/feed.atom"))))
|
2023-10-31 20:46:45 +00:00
|
|
|
|
2023-10-30 20:42:28 +00:00
|
|
|
|
|
|
|
;;; 404
|
|
|
|
(define (not-found request)
|
|
|
|
(html-response (make-page "source/not-found.txt")))
|
|
|
|
|
|
|
|
|
|
|
|
;;; server
|
|
|
|
(define stop
|
|
|
|
(serve
|
|
|
|
#:dispatch (sequencer:make
|
|
|
|
(filter:make #rx"^/static/" static-dispatcher)
|
|
|
|
(dispatch/servlet #:regexp #rx"^/hx/" httpx-app)
|
2023-10-31 20:46:45 +00:00
|
|
|
(dispatch/servlet #:regexp #rx"^/feed.atom" rss-feed)
|
2023-10-30 20:42:28 +00:00
|
|
|
(dispatch/servlet page-app)
|
|
|
|
(dispatch/servlet not-found)
|
|
|
|
)
|
|
|
|
#:listen-ip "127.0.0.1"
|
|
|
|
#:port 8000))
|
|
|
|
|
|
|
|
(with-handlers ([exn:break? (lambda (e)
|
|
|
|
(stop))])
|
|
|
|
(sync/enable-break never-evt))
|
|
|
|
|