Search code examples
rascal

Running a salix webApp through an IDE menu


I have a bit of code that creates a salix webapp and runs it from an IDE popup menu by making use of util::Webserver. In order to allow for the command to be used multiple times, I try to shutdown any existing webserver at that address first but it doesn't seem to be working. No matter what it always comes up with an illegal argument error stating "shutdown" not possible.

void run_game(Tree t, loc s){
    t = annotate(t);
    PSGAME g = ps_implode(t);
    Checker c = check_game(g);
    Engine engine = compile(c);
    
    loc host = |http://localhost:9050/|;
    
    try { util::Webserver::shutdown(host);} catch: ;
    util::Webserver::serve(host, load_app(engine)().callback, asDaemon = true);
    println("Serving content at <host>");
}

What I expect to happen is that the first time this function it run, shutdown throws an error that is silenced because no webserver exists and then serve starts the webserver. If the user tries to run the function again then shutdown successfully runs, clearing the address bind and serve binds successfully to the address.

What actually happens the second time, is that shutdown still errors, the error is silenced and then serve complains that the address is already in use.

I'm looking for any solution that would allow me to start a salix app through the IDE's popup menu (previously registered) at the same address.

PS_contributions =
  {
    PS_style,
    popup(
        menu(
            "PuzzleScript",
            [
                action("Run Game", run_game)
            ]
        )
    )
  };

registerContributions(PS_NAME, PS_contributions);

Solution

  • Right; we ran into similar issues and decided to special case actions that run web apps. So we added this:

    data Menu = interaction(str label, Content ((&T <: Tree) tree, loc selection) server)
    

    See https://github.com/usethesource/rascal-eclipse/blob/bb70b0f6e8fa6f8c227e117f9d3567a0c2599a54/rascal-eclipse/src/org/rascalmpl/eclipse/library/util/IDE.rsc#L119

    Content comes from the Content module which basically wraps any Response(Request) servlet.

    So you can wrap your salix webApp in a Content and return it given a current selection and the current tree.

    The IDE will take care to start and also shutdown the server. It does that every time an interaction with the same label is created or after 30 minutes of silence on the given HTTP port.