Run playframework as a console app to serve a single page web app

Hello.

I would like to run a play app as a console app that starts automatically the default browser and opens the single page of the app. Is it doable? If yes, could you please give me some pointers of where to look to implement this?

Thanks

I don’t have any solutions but I’m curious if you found any @costa

Play in itself could probably do it but sounds like what you need is just a bootstrap script.

Basically a script that will launch your server, wait a bit then launch the default browser on the page you want.

That’s platform dependant tho. But doable at least on windows and linux (I never used mac but I guess it’s like linux).

Hi,

Here is an option. When you run sbt dist inside your project folder (or just dist when you are in sbt), sbt builds a zip file with all the files needed for deployment. The zip file is created under your project directory under target\universal. The zip file contains all the jar files needed to run the app, including a bin subfolder where you have the scripts that run the java command. They included a bat script for windows, and a linux script. You could write another script - as suggested by @PierreAdam - that simply launches the app (without waiting for it to start) and then opens the browser to the url that serves the app (something like http://localhost:9000/) . Of course, things might get more complicated, i.e. the port number might not be available on the local computer. How is this handled? I am not sure. You can put the onus on the user to make sure the port number is available (he/she may have to configure a different port number that is available), but then the script that launches the browser has to be aware of that new port number. Or, you could handle it in a setup program that handles the deployment of your app (the setup would have to check that the port number is available or allow the user to pick a different port number that is available).

The entry point for the play app is: play.core.server.ProdServerStart.

These are just ideas. I haven’t tried it yet.

See also this: https://www.playframework.com/documentation/2.8.x/Deploying.

PS. To eliminate any confusion, when I wrote “project folder”, I meant the folder where you have your entire application, not the subfolder called project (which contains the plugins.sbt & build.properties files)

1 Like