Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of using Eliom_service.register_eliom_module? #805

Closed
shonfeder opened this issue Sep 22, 2024 · 4 comments
Closed

Example of using Eliom_service.register_eliom_module? #805

shonfeder opened this issue Sep 22, 2024 · 4 comments

Comments

@shonfeder
Copy link
Contributor

shonfeder commented Sep 22, 2024

I am trying to complete the migration of a simple site to Ocsigen and Eliom. I've nearly completed the migration, but I think I am now running up against the complexity of static liking eliom modules. E.g., trying to run the server defined using Ocsigen_server.start, I hit

Fatal error: exception Failure("This function cannot be called here because it needs information about the request or the site.\nCalled from Eliom_request_info.get_request_cache in file \"src/lib/eliom_request_info.server.ml\" (inlined), line 202, characters 11-33\nCalled from Eliom_reference.Volatile.modify in file \"src/lib/eliom_reference.server.ml\", line 117, characters 34-44\nCalled from Eliom_syntax.client_value in file \"src/lib/eliom_syntax.server.ml\", line 141, characters 2-78\nCalled from Eliom_form.Make_links.a in file \"eliom_form.eliom\", line 125....

I believe I am hitting the problem discussed in https://ocsigen.org/eliom/latest/api/server/Eliom_service#2_StaticloadingofEliommodules . I expect I can eventually figure out how to work around this by trial and error, or studying the implementation, but it would be very helpful if there were an example of how to work around this kind of problem.

@shonfeder
Copy link
Contributor Author

shonfeder commented Sep 22, 2024

I've got a workaround that may be useful to report:

The problem I hit is this: while it is obvious how to register an initialization function, it is not at all obvious (to me) how to get that registered init function to be called.

From digging in to ocsigen a git, it seems like the only time these init functions get called is during the parsing of a configuration file (https://github.com/ocsigen/ocsigenserver/blob/7950e0a462164398a4b6af780d2981b0ab7b2149/src/server/ocsigen_parseconfig.ml#L379-L380). Since I am trying to port an existing site (that used opium), and have been trying to use the (iiuc) newer "static" API via Ocsigen_server.start I don't have any such config file to parse. As such, my init function is never getting called.

Based on the docs, I tried to add various XML values to a config file, hoping to get the init function triggered, using ~configfile label to tell start where to find it, but the file was either ignored of failed silently.

Ultimately, this hack fixed the problem for me: shonfeder/ocsigenserver@75c300b

Of course, I lose the nifty dynamic reloading functionality, but this is not worse off than I was before.

It would be very nice to expose the dynamic reloading functionality via the Ocsigen_server.start API, without having to add an XML file just for this purpose. However, I also think an API to just tell start which services it needs to register, or which init function it needs to all, would be a user-friendly way to make using the static API easier.

As an example, it would be convenient to be able to call something like:

  Ocsigen_server.start
    ~ports:([`All, port])
    ~init:(my_init_function:unit -> unit)
    [ Ocsigen_server.host ~port
        [ Eliom.run () ]]

Tho I can imagine some APIs that may be even cleaner (e.g., just taking a list of services and registering them).

@shonfeder shonfeder changed the title Exampel of using Eliom_service.register_eliom_module? Example of using Eliom_service.register_eliom_module? Sep 23, 2024
@balat
Copy link
Member

balat commented Sep 28, 2024

Hello Shon,
This error message usually happens when a function is called during the initialisation phase of the server (before the server starts) while it should be during a request. It means that this function needs to know the exact configuration (path usually) for a service or a scoped reference.
This happens for example if you try to get some session values (Eliom references) outside a function. The easy fix is to turn your value into a function which is called during page generation.
In your case it seems to be getting some information about the request (which can be done obviously only during a request). Maybe a reference with scope `Request in a form?

You shouldn't need to call register_eliom_module yourself. This is an internal function.

Does it help to solve your issue? Don't hesitate to ask more questions. I will try to check more frequently.

Our next huge documentation improvement is going to be about make the API documentation simpler to read, but working on tools and separating low level functions from the function you actually need for every day use. But this takes time.

@shonfeder
Copy link
Contributor Author

Hello! It looks like I may have gotten a bit confused somewhere between my own migration to Ocsigen and my (misreading) of the docs on static liking eliom modules 😅

Your clarification here, and my own further investigations, have cleared the way. I have turned some fragments of html in thunks, made some targeted use of lazy values to avoid needless recomputations.

Thank you for the guidance, and I'm sorry for the noise here!

@balat
Copy link
Member

balat commented Sep 30, 2024

You're welcome. This error is vey frequent anyway. I will find a way to document it better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants