Skip to content

Commit

Permalink
make the test blog work
Browse files Browse the repository at this point in the history
  • Loading branch information
euhmeuh committed Apr 11, 2018
1 parent 9f517f7 commit 0c3e29f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# racket compiled files
compiled/
2 changes: 2 additions & 0 deletions web-galaxy-lib/web-galaxy/entities.rkt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#lang racket/base

(provide
(all-from-out "renderer.rkt")

(struct-out link)
render-link

Expand Down
20 changes: 12 additions & 8 deletions web-galaxy-lib/web-galaxy/response.rkt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#lang racket/base

(provide
current-response-error
current-response-not-found
current-error-responder
current-not-found-responder
req
response-page
define-response)
Expand Down Expand Up @@ -34,15 +34,19 @@
body ...)))]))

(define-response (not-found)
(response #:code 404
#:message #"Not Found"))
(response/full
404 #"Not found"
(current-seconds) TEXT/HTML-MIME-TYPE '()
'(#"404 - Not found")))

(define (response-error url exception)
(log-error "~s" `((exn ,(exn-message exception))
(uri ,(url->string url))
(time ,(current-seconds))))
(response #:code 500
#:message #"Internal server error"))
(response/full
500 #"Internal server error"
(current-seconds) TEXT/HTML-MIME-TYPE '()
'(#"500 - Internal server error")))

(define current-response-not-found (make-parameter response-not-found))
(define current-response-error (make-parameter response-error))
(define current-not-found-responder (make-parameter response-not-found))
(define current-error-responder (make-parameter response-error))
18 changes: 14 additions & 4 deletions web-galaxy-lib/web-galaxy/serve.rkt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#lang racket/base

(provide
number-arg
integer-arg
real-arg
string-arg
symbol-arg
current-server-listen-ip
current-server-port
current-server-root-path
Expand All @@ -15,23 +20,28 @@
"site-mode.rkt"
"response.rkt")

(define current-server-listen-ip (make-parameter (if-debug "127.0.0.1" #f)))
(define current-server-port (make-parameter (if-debug 8000 80)))
(define current-server-root-path (make-parameter (current-directory)))
(define current-server-static-paths (make-parameter '()))

(define-syntax-rule (serve/all ((endpoint arg ...) response) ...)
(begin
(define-values
(dispatcher url-maker)
(dispatch-url ((endpoint arg ...) response) ...))
(dispatch-rules ((endpoint arg ...) response) ...))
(serve/servlet
dispatcher
#:command-line? #t
#:banner? #t
#:servlet-regexp #rx""
#:listen-ip (current-server-listen-ip)
#:port (current-server-port)
#:manager (create-none-manager (current-response-not-found))
#:servlet-responder (if-debug servlet-error-responder (current-response-error))
#:manager (create-none-manager (current-not-found-responder))
#:servlet-responder (if-debug servlet-error-responder (current-error-responder))
#:server-root-path (current-server-root-path)
#:extra-files-paths (current-server-static-paths)
#:file-not-found-responder (current-response-not-found)
#:file-not-found-responder (current-not-found-responder)
;;#:log-file (current-output-port)
;;#:log-format 'extended
)))
7 changes: 5 additions & 2 deletions web-galaxy-test/tests/web-galaxy/pony-blog/blog.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

(require
web-galaxy/response
web-galaxy/serve)
web-galaxy/serve
"models/article.rkt")

(define articles '())

(define-response (index)
(local-require "pages/index.rkt")
Expand All @@ -20,4 +23,4 @@
(serve/all
[("") response-index]
[("article" (string-arg)) response-article]
[("tags" (string-arg)) response-tags])
[("tags" (symbol-arg)) response-tags])
21 changes: 21 additions & 0 deletions web-galaxy-test/tests/web-galaxy/pony-blog/models/article.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#lang racket/base

(provide
(struct-out article)
get-recent-articles
filter-articles-by-tag)

(struct article (title timestamp tags content))

(define (last-month)
(- (current-seconds) (* 31 24 60 60)))

(define (get-recent-articles articles)
(filter (lambda (article)
(> (article-timestamp article) (last-month)))
articles))

(define (filter-articles-by-tag articles tag)
(filter (lambda (article)
(member tag (article-tags article)))
articles))

0 comments on commit 0c3e29f

Please sign in to comment.