3

I figured that I can manage multiple apps using location in nginx.

But it seems like I cannot run multiple dancer2 apps in same server with different ports (like localhost:3000, localhost:4000).

Anyway I'm putting this here with a hope that experts can show me some light.

Yasiru G
  • 6,886
  • 6
  • 23
  • 43
  • 2
    You can deploy several PSGI apps within the same PSGI file and run them on the same plackup on one port. D2 is designed so you can even have multiple instances of the same application in one PSGI file. But that has the risk that if one crashes, it takes the others with it. The general approach would be to run each one seperate on its own port (or socket). Why is that a problem? – simbabque Dec 05 '17 at 11:27
  • *But it seems like I cannot run multiple `dancer2` apps in same server with different ports* I think this is wrong. I can't see why this would be a problem. Can you please expand your question to explain what goes wrong when you try this. – Dave Cross Dec 05 '17 at 11:46
  • I just started leaning RESTapi and Dancer. If I code 2 dancer scripts like app1.pl and app2.pl there is no way to start both apps in same server because dancer designed to use with single port. So always the first app reserve the port and second app will fail to listen to it since the port already in use. – Yasiru G Dec 05 '17 at 11:56
  • 2
    *dancer designed to use with single port* That is not true. It is easy to run multiple Dancer2 apps on different ports. Dancer2 apps know nothing about the port they are running on - that is set by the deployment mechanism you use. It depends on how you run the apps, but with `plackup`, for example, you could run `plackup -p 3000 app1.psgi` and `plackup -p 4000 app2.psgi`. – Dave Cross Dec 05 '17 at 12:47
  • @Dave it seems this is a simple issue of not reading the docs. You should flesh that out into an answer. – simbabque Dec 05 '17 at 16:03
  • Thanks @DaveCross ! I just tested and it worked. I couldn't find a clear doc about this. Please put your comment as an answer, so I can mark it as the answer. – Yasiru G Dec 06 '17 at 02:58
  • I've put my comment into an answer. – Dave Cross Dec 06 '17 at 10:36

2 Answers2

4

When you have multiple Dancer2 applications, you can compose them together using either Plack::App::URLMap or the wrapper syntax for it available in Plack::Builder:

use MyApp::Main;
use MyApp::Admin;

builder {
    mount '/'      => MyApp::Main->to_app;
    mount '/admin' => MyApp::Admin->to_app;
};

The effect of the mounting is that these applications will be completely separate and Plack::Builder will assure only the appropriate application handles a given request.

Source: http://advent.perldancer.org/2014/9

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • 1
    As I said in my comment above, this has the problem of sharing a parent process. If one of the applications crashes, it might take all of them with it. That might or might not be a big deal. Certainly depends on how many machines there are running those, what the product is and so on. But it's a risk. – simbabque Dec 05 '17 at 16:02
2

But it seems like I cannot run multiple dancer2 apps in same server with different ports (like localhost:3000, localhost:4000).

This isn't true. Dancer (and, obviously, Dancer2) apps know nothing about the port that they are listening to. That is all handled by your deployment environment. If, for example, you have two Dancer apps called app1.psgi and app2.psgi and you are starting them with plackup, then you can get them running on different ports using the -p command line option.

$ plackup -p 3000 app1.psgi
$ plackup -p 4000 app2.psgi
Dave Cross
  • 68,119
  • 3
  • 51
  • 97