球拍 URL 调度

发布于 2024-09-02 01:25:48 字数 1156 浏览 4 评论 0原文

我正在尝试将 URL 调度与 Racket(以前称为 PLT 方案)连接起来。我已经查看了教程和服务器文档。我不知道如何将请求路由到相同的 servlet。

具体示例:

#lang scheme

(require web-server/servlet)
(require web-server/dispatch)
(provide/contract (start (request? . -> . response/c)))

(define (start request)
  (blog-dispatch request))

(define-values (blog-dispatch blog-url)
  (dispatch-rules
   (("") list-posts)
   (("posts" (string-arg)) review-post)
   (("archive" (integer-arg) (integer-arg)) review-archive)
   (else list-posts)))

(define (list-posts req) `(list-posts))
(define (review-post req p) `(review-post ,p))
(define (review-archive req y m) `(review-archive ,y ,m))

(require web-server/servlet-env)
(serve/servlet start
               #:launch-browser? #t
               #:quit? #f
               #:listen-ip #f
               #:port 8080
               #:extra-files-paths (list (build-path "js")
                                         (build-path "css"))
               #:servlet-path "")

假设上面的代码,localhost:8080/ 转到一个显示“list-posts”的页面。转到 localhost:8080/posts/test 会转到 Racket“未找到文件”页面(我希望它会转到显示“review-post test”的页面)。

感觉就像我错过了一些小而明显的东西。谁能给我提示吗?

I'm trying to hook up URL dispatch with Racket (formerly PLT Scheme). I've taken a look at the tutorial and the server documentation. I can't figure out how to route requests to the same servlets.

Specific example:

#lang scheme

(require web-server/servlet)
(require web-server/dispatch)
(provide/contract (start (request? . -> . response/c)))

(define (start request)
  (blog-dispatch request))

(define-values (blog-dispatch blog-url)
  (dispatch-rules
   (("") list-posts)
   (("posts" (string-arg)) review-post)
   (("archive" (integer-arg) (integer-arg)) review-archive)
   (else list-posts)))

(define (list-posts req) `(list-posts))
(define (review-post req p) `(review-post ,p))
(define (review-archive req y m) `(review-archive ,y ,m))

(require web-server/servlet-env)
(serve/servlet start
               #:launch-browser? #t
               #:quit? #f
               #:listen-ip #f
               #:port 8080
               #:extra-files-paths (list (build-path "js")
                                         (build-path "css"))
               #:servlet-path "")

Assuming the above code, localhost:8080/ goes to a page that says "list-posts". Going to localhost:8080/posts/test goes to a Racket "file not found" page (I'd expect it to go to a page that says "review-post test").

It feels like I'm missing something small and obvious. Can anyone give me a hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏已如深秋| 2024-09-09 01:25:49

你写的不是一个完整的程序,所以我无法调试它。

这是一个带有注释的程序,可能可以实现您想要的功能:

#lang scheme ; specify the right language
; include the correct libraries
(require web-server/servlet
         ; this one gets "serve/servlet"
         web-server/servlet-env)

(define (start request)
  (blog-dispatch request))

(define-values (blog-dispatch blog-url)
  (dispatch-rules
   (("") list-posts)
   (("posts" (string-arg)) review-post)
   (("archive" (integer-arg) (integer-arg)) review-archive)
   (else list-posts)))

(define (list-posts req) `(list-posts))
(define (review-post req p) `(review-post ,p))
(define (review-archive req y m) `(review-archive ,y ,m))

; starts a web server where...
(serve/servlet start ; answers requests
               #:servlet-path "" ; is the default URL
               #:port 8080 ; is the port
               #:servlet-regexp #rx"") ; is a regexp decide
                                       ; if 'start' should
                                       ; handle the request

因为函数 list-postsreview-postreview-archive不返回 HTML 的合理 xexpr 编码,您必须查看源代码才能正确看到它们。

请随时直接给我发电子邮件或向 PLT 方案邮件列表发送电子邮件。 (注意:我们将 PLT 方案重命名为“Racket”,以便您在发帖时可以看到它。)

Jay McCarthy

What you've written is not a whole program, so I cannot debug it.

Here is a program with annotations that does what you want, probably:

#lang scheme ; specify the right language
; include the correct libraries
(require web-server/servlet
         ; this one gets "serve/servlet"
         web-server/servlet-env)

(define (start request)
  (blog-dispatch request))

(define-values (blog-dispatch blog-url)
  (dispatch-rules
   (("") list-posts)
   (("posts" (string-arg)) review-post)
   (("archive" (integer-arg) (integer-arg)) review-archive)
   (else list-posts)))

(define (list-posts req) `(list-posts))
(define (review-post req p) `(review-post ,p))
(define (review-archive req y m) `(review-archive ,y ,m))

; starts a web server where...
(serve/servlet start ; answers requests
               #:servlet-path "" ; is the default URL
               #:port 8080 ; is the port
               #:servlet-regexp #rx"") ; is a regexp decide
                                       ; if 'start' should
                                       ; handle the request

Because the functions list-posts, review-post, and review-archive don't return sensible xexpr encodings of HTML, you'll have to view source to see them right.

Please feel free to email me directly or email the PLT Scheme mailing list. (Note: We are renaming PLT Scheme to "Racket" so you may see that when you post.)

Jay McCarthy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文