LINUX.ORG.RU

Racket, как рендерить HTML файл в web-server?

 , ,


0

2

Я пробовал делать так:

(define (start req)
  (include-template "templates/main.html"))
 
(serve/servlet start)

Но выдвало такую ошибку:
. . ../../../../../collects/racket/contract/private/blame.rkt:159:0: Servlet (@ /servlets/standalone.rkt) exception:
serve/servlet: contract violation
  expected: can-be-response?
  given: "<!DOCTYPE <!DOCTYPE html>\n<html>\n<head>\n    <meta charset=\"utf-8\" />\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    <title>Page Title</title>\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\...
  in: the range of
      the 1st argument of
      (->*
       ((-> request? can-be-response?))
       (#:banner?
        boolean?
        #:command-line?
        boolean?
        #:connection-close?
        boolean?
        #:extra-files-paths
        (listof path-string?)
        #:file-not-found-responder
        (-> request? can-be-response?)
        #:launch-browser?
        boolean?
        #:listen-ip
        (or/c #f string?)
        #:log-file
        (or/c #f path-string?)
        #:log-format
        (or/c
         (or/c
          'parenthesized-default
          'extended
          'apache-default)
         (-> request? string?))
        #:manager
        manager?
        #:max-waiting
        natural?
        #:mime-types-path
        path-string?
        #:port
        listen-port-number?
        #:quit?
        boolean?
        #:server-root-path
        path-string?
        #:servlet-current-directory
        path-string?
        #:servlet-loading-responder
        (-> url? any/c can-be-response?)
        #:servlet-namespace
        (listof module-path?)
        #:servlet-path
        string?
        #:servlet-regexp
        regexp?
        #:servlet-responder
        (-> url? any/c can-be-response?)
        #:servlets-root
        path-string?
        #:ssl-cert
        (or/c #f path-string?)
        #:ssl-key
        (or/c #f path-string?)
        #:ssl?
        boolean?
        #:stateless?
        boolean?
        #:stuffer
        (stuffer/c
         (-> serializable? bytes?)
         (-> bytes? serializable?)))
       void)
  contract from: 
      <pkgs>/web-server-lib/web-server/servlet-env.rkt
  blaming: /home/aprokurov/Programming/Racket/web.rkt
   (assuming the contract is correct)
  at: <pkgs>/web-server-lib/web-server/servlet-env.rkt:43.2


Сам html файл:
<!DOCTYPE <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    
</head>
<body>
    <h3>"Press that button!"</h3>
    <button type="button" onclick="alert("Clicked!")">"Click it"</button>
</body>
</html>

★★

Как-то так:

#lang racket/base

(require web-server/servlet
         web-server/servlet-env
         web-server/templates)

(define (render-mainpage request)
  (send/suspend/url
   (lambda (req)
     (response/output
      (lambda (op)
        (write-bytes (string->bytes/utf-8 (include-template "templates/main.html")) op)
        (void))))))

(define-values (site-dispatch site-url)
  (dispatch-rules
   (("") render-mainpage)
   ))

(serve/servlet site-dispatch
               #:servlet-path ""
               #:servlet-regexp #rx""
               )
anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.