Methods for breaking up web-server files

While writing a web-app, I've found my server.rkt file getting a bit unwieldy so I'm trying to break it up some. My first attempt was to put all the rendering functions in to a file separate from the file with the dispatch-rules and handler functions. So I went from a file like this:

server.rkt

#lang racket
(require web-server/dispatch "model.rkt" ...)

(define-values (dispatch url-generator)
 (dispatch-rules
    (("") welcome)
    (...) ...))

(define (welcome req)
 ...
 (render-welcome-page req ...))

...

(define (render-welcome-page req ...)
   (response/xexpr
       `(html ...
           (body
              (a ((href ,(url-generator login))) "Login") 
              ...))))

(serve/servlet dispatch ...)

to a pair of files like this
server.rkt

#lang racket
(require web-server/dispatch "model.rkt"  "view.rkt" ...)

(define-values (dispatch url-generator)
 (dispatch-rules
    (("") welcome)
    (...) ...))

(define (welcome req)
 ...
 (render-welcome-page req ...))
...
(serve/servlet dispatch ...)

view.rkt

#lang racket
(require web-server/dispatch "model.rkt" ...)
(define (render-welcome-page req ...)
    (response/xexpr
        `(html ...
            (body
               (a ((href ,(url-generator login))) "Login") 
               ...))))
...

I immediately realized that view.rkt needed to have url-generator, so I tried redefining my renderers to take it as an argument, but then I realized (url-generator login) needs login to be defined in its scope.

Assuming I want my renderers to be able to dynamically generate urls, and assuming I want to use url-based dispatching, is there any way to split up my server file in the manner I attempted?

1 Like