Re: Question about generating urls with dispatch-rules from imported modules

Yury Bulka <setthemfree-tGttlkr9q/[email protected]> Sun, 26 Apr 2020 12:16:04 +0300
Newsgroups gmane.comp.lang.racket.user,gmane.lisp.scheme.plt
Message-ID <[email protected]>
Thank you for sharing this!

I'm wondering, if this is a common use case, maybe it is worth adding to
web-server/dispatch in some form?

Or, if not, maybe we can extend the documentation to include some hints
on how to approach this otherwise?

It is great to have powerful libraries outside the main distribution,
but the main distribution is the place where newcomers like me are going
to look first.

--
Yury Bulka
https://mamot.fr/@setthemfree
#NotOnFacebook



Bogdan Popa <[email protected]> writes:

> I solved this in koyo by defining an alternate version[1] of
> `dispatch-rules' whose generated `reverse-uri' functions take route
> names rather than functions as arguments:
>
> https://koyoweb.org/dispatch/index.html#%28form._%28%28lib._koyo%2Fdispatch..rkt%29._dispatch-rules%2Broles%29%29
>
> The generated function is installed into a parameter on app init and
> another function[2] knows how to look it up and apply it when called:
>
> https://koyoweb.org/url/index.html#%28def._%28%28lib._koyo%2Furl..rkt%29._current-reverse-uri-fn%29%29
>
> I hope that helps!
>
> [1]: https://github.com/Bogdanp/koyo/blob/331701e9e8d7f553955ea5a950df424ee13ce9d4/koyo-lib/koyo/dispatch.rkt
> [2]: https://github.com/Bogdanp/koyo/blob/331701e9e8d7f553955ea5a950df424ee13ce9d4/koyo-lib/koyo/url.rkt#L60
>
> Yury Bulka writes:
>
>> Oops, small correction:
>>
>>> The x-expressions need to include hyperlinks to urls
>>> defined via dispatch-rules in main.rkt, i.e., call the url-generating
>>> function.
>>
>> Should be `routing.rkt` instead of `main.rkt`.
>>
>> --
>> Yury Bulka
>> https://mamot.fr/@setthemfree
>> #NotOnFacebook
>>
>>
>>
>> Yury Bulka <setthemfree-tGttlkr9q/[email protected]> writes:
>>
>>> Dear Racket community,
>>>
>>> First of all I want to say hello since this is my first post here.
>>>
>>> I have a question about using the url dispatch library:
>>> https://docs.racket-lang.org/web-server/dispatch.html
>>>
>>> More specifically about the url-generation function in the context of
>>> inter-module dependencies.
>>>
>>> Let's say in `routing.rkt` I define some request handlers and then map
>>> them to urls using dispatch-rules.
>>>
>>> The request handlers call functions from another module that are
>>> responsible for generating the x-expressions (let's call this module
>>> `templates.rkt`). The x-expressions need to include hyperlinks to urls
>>> defined via dispatch-rules in main.rkt, i.e., call the url-generating
>>> function.
>>>
>>> The url-generating function, in turn, needs to be provided the request
>>> handler as the first argument.
>>>
>>> That means that the xexpr-generating function needs to have access to
>>> the url-generating function and all the request handlers it might
>>> need to include hyperlinks to, or it may need to be provided with all
>>> the generated urls in advance.
>>>
>>> Something like this wouldn't work if render-post needed to generate a url:
>>>
>>> ;; A request handler associated with a url
>>> (define (single-post-view request post-id)
>>>   (response/xexpr (render-post (post-id))) ;; render-post is in another file/module
>>>
>>> I wouldn't want to make templates.rkt require routing.rkt (I'm not sure
>>> this is technically possible to have modules depend on each other like
>>> that), nor would I want to provide all the request handlers as arguments
>>> to `render-post`. I also thought about putting the url-generating
>>> function into a parameter, but `render-post` also needs access to the
>>> request handlers.
>>>
>>> What approach should I take?
>>>
>>> --
>>> Yury Bulka
>>> https://mamot.fr/@setthemfree
>>> #NotOnFacebook

-- 
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/87r1waip4b.fsf%40privacyrequired.com.