The backend consists of two parts:
- A Ruby application that periodically add new order reports to the sqlite3 database, and remove old reports.
- A PHP script through which the user can query the sqlite3 database to find out the delivery details of their order.
The PHP script can be used in three ways:
- The user can access the PHP script directly, passing their order reference as
the URL parameter called
order_ref. - The PHP script can return the tracking information as a JSON string by
passing
tas the URL parameterjson. - The PHP script can optionally return the JSON object inside a callback in a
Javascript string by passing the name of the callback as the URL parameter
callback. This is useful for JSONP cross-site requests. See the front-end example files.
- A PHP enabled webserver on a Unix operating system. At time of writing, we are using Nginx and PHP-fpm on Debian 7.
- Ruby. At time of writing we are using 2.0.0, managed with RVM.
- Ruby gem dependencies, as specified in the Gemfile. Install by running
bundlefrom the project directory.
- Provision your PHP capable webserver. A guide on installing a LEMP stack (Linux, Nginx, MySQL, PHP) on Debian 7 can be found here. We are using an sqlite3 database, so the MySQL section can be safely skipped.
- Install RVM or similar, along with a suitable version of Ruby.
- Clone this repository into the Nginx webroot directory, or another directory Nginx will serve content from.
- Install the Ruby dependancies by running
bundle installfrom the project directory. - Set up your webserver not to serve anything but HTML and CSS files from the project directory, so that users cannot download the code or database. See below for an example.
- From the project directory run
bin/setup_db.rbto create the sqlite3 database. - Make a yaml file at
config/ftp.ymlwith the details of the FTP site to read the reports from. You can find an example atconfig/ftp.yml.example. - Populate the database with reports for the first time by running the
following from the project directory:
bin/fetch_new_reports.rbbin/parse_fetched_reports.rb
- Now we need to use Cron to
periodically add new reports and to remove old ones. We shall do this
indirectly using the
wheneverRuby gem, rather than by directly editing the crontab. From the project directory runbundle exec whenever -w. Ensure that the crontab has been written to withcrontab -l.
Here is an example server block for nginx
server {
server_name www.perivansolutions.co.uk perivansolutions.co.uk;
root /usr/share/nginx/www/perivansolutions/;
index index.html index.htm;
location ~ /order-tracking {
root /usr/share/nginx/www/;
# Whitelist. Serve these files extensions only
if ($request_filename !~* \.(css|js|php|html)$ ) {
return 403;
break;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location / {
try_files $uri $uri/ index.html;
}
}
For more examples see this repo