Writing web applications should be easy and fun
Catmandu integrates very well with Perl frameworks such as Dancer for easy and fun webapp development. In the text below we will use our Catmandu tools to create small demo application: a web search engine that can be fed with CSV files.
This second part of the tutorial assumes some familiarity with Catmandu. If you have not yet read part 1, now would be a good time to do so: Getting Started.
Dancer provides an intuitive and minimalistic approach to create web applications. If you have already installed Task::Catmandu, then you are good to go. Otherwise, Dancer can be easily installed if you have cpanminus on your server:
Creating a web application is as easy as running the command line dancer command.
Change your working directory to DemoApp and boot up the build-in web server. Now you should have a running server on http://0.0.0.0:3000.
In the following section we are going to create a web form to upload a CSV file to the webserver. Open your editor and load the file 'lib/DemoApp.pm' which contains all the controller code of your application.
We add a new route '/upload' to the application which will display the content of the views/upload.tt template using the following code snippet. For this route to work we need to add a new template file views/upload.tt:
Restart your webapplication (perl bin/app.pl) and view your web form at http://localhost:3000/upload.
When you click on the submit button a file of your choice will be uploaded via a HTTP POST to the webserver. The contents of this file can be easily displayed by adding the following code to the DemoApp.pm controller:
Restart your webapplication (perl bin/app.pl) and try it out. Your complete code should look now like this:
Now that we are able to upload a file to the webserver we can add Catmandu code to parse this data and send it to a Solr search engine. We first create a file catmandu.yml in the home directory of the DemoApp which contains the location of our Solr engine and the parameters for the Catmandu importer we are going to use to parse the data (CSV-files in our case):
Reading from a CSV importer and indexing into a Solr Store can be easily added to the DemoApp.pm controller by adding the following lines at the top of the DemoApp.pm module:
It is now very easy to parse CSV data. We change our POST /upload route into: If you restart the web server and upload a CSV file you should see a Data::Dumper output of all the fields. What we did in the code was to send the path of the uploaded file ($file->tempname) to a CSV importer configured in the catmandu.yml file. Every importer has a to_array method to read all the data into a Perl array. This array is then converted to a Data::Dumper string by the to_dumper call.
Indexing all the fields in the CSV file into a Solr index is straight forward (read your Solr manual how to setup a Solr search engine with your own indexable fields [hint: schema.xml file]). All we need is to send the importer to a Solr Catmandu Store with the add_many command and commit the results.
Add this stage you'll have a basic website where you can upload a CSV-file into a Solr search engine. Next we are going to add a /search route to query the Solr index.
We start by creating a GET route /search in DemoApp.pm and provide it with a template to display a search box: Here is the views/search.tt template it will read:
Next we are going to provide a GET route /results which will query the Solr index and display all the results: In the first lines we read the query parameters from the request. In case we have a query string we execute a Solr query and read all the results into a Catmandu::Hits iterator which will be converted into an array. We use the views/results.tt template to display all the results: To run this code we need a little change in the Dancer configuration file config.yml to activate the Template Toolkit language for all template files:
Restart the webserver and browse to http://0.0.0.0:3000/search to view the search box. When you execute the query *:* you should see a list of all your documents:
This concludes our very short introduction into Catmandu and Dancer. We hope we have provided you with enough material and inspiration for new and exciting projects ;). Please find in the included DemoApp.zip ZIP file an elaborate example of the code used in this tutorial.