This is step 4 in the Perl web development series. In this step we focus on setting up the Dancer2 app. After the setup we get to run it to see our first results!
After following along the previous steps we’ve come to the point to install Dancer2.
Finally we can start to see the fruits of our efforts so far, but we have work to do to get our app completed. For now, we can just enjoy the moment of a perfect little ‘Hello World’. It’s always fun to fire up your app for the first time right?
Init the Dancer2 app
Dancer2 comes with a little command line utility that can do a couple of things for you. The most important one is setting up a new Dancer2 app. Because we use Carton as our package manager we need to call everything from the Carton context to make sure all modules needed are available. To install a new app go to the top dir of your app (the one that holds the local/ directory used by carton) and run:
$ carton exec dancer2 -a MovieCollection
+ MovieCollection
+ MovieCollection/.dancer
+ MovieCollection/config.yml
+ MovieCollection/cpanfile
+ MovieCollection/Makefile.PL
+ MovieCollection/MANIFEST.SKIP
+ MovieCollection/bin
+ MovieCollection/bin/app.psgi
+ MovieCollection/environments
+ MovieCollection/environments/development.yml
+ MovieCollection/environments/production.yml
+ MovieCollection/lib
+ MovieCollection/lib/MovieCollection.pm
+ MovieCollection/public
+ MovieCollection/public/dispatch.cgi
+ MovieCollection/public/dispatch.fcgi
+ MovieCollection/public/404.html
+ MovieCollection/public/500.html
+ MovieCollection/public/favicon.ico
+ MovieCollection/public/css
+ MovieCollection/public/css/error.css
+ MovieCollection/public/css/style.css
+ MovieCollection/public/images
+ MovieCollection/public/images/perldancer-bg.jpg
+ MovieCollection/public/images/perldancer.jpg
+ MovieCollection/public/javascripts
+ MovieCollection/public/javascripts/jquery.js
+ MovieCollection/t
+ MovieCollection/t/001_base.t
+ MovieCollection/t/002_index_route.t
+ MovieCollection/views
+ MovieCollection/views/index.tt
+ MovieCollection/views/layouts
+ MovieCollection/views/layouts/main.tt
As you can see a whole bunch of files and directories are created for you. We will dive into those later because we need to know a lot more about them, but for now I can just say congratulations, you’ve installed your Dancer2 app.
Fire up your app
Time to fire up your app! Let’s see what it looks like. Dancer2 is PSGI compatible framework so we can use the plackup utitlity to create a standalone server. Make sure you’re in the top directory and run:
$ carton exec plackup -p 5000 MovieCollection/bin/app.psgi
HTTP::Server::PSGI: Accepting connections at http://0:5000/
The ‘-p 5000’ here means that it listens to all web requests on port 5000.
All looks good! Now launch your browser and point it to ‘localhost:5000’. The result should look like this:
Got a similar result? Great! You have done it! There is your Dancer2 app!