DEV Community

Cover image for Pointing A Domain to The Newest File In A Directory
Jack Harner πŸš€
Jack Harner πŸš€

Posted on • Originally published at harnerdesigns.com

Pointing A Domain to The Newest File In A Directory

This might not be any where near the best solution, but it worked for me in this one instance. More of a description of my thought process than an actual tutorial.

I wrote a python script for Shoolu.com to use the BigCommerce API to pull the latest added products and generate an XML RSS file. BigCommerce did have the functionality to automatically generate these RSS feeds but they have moonlighted support and have removed it for new merchants so it didn't seem like something to rely on.

PYTHON To The Rescue

I found the BigCommerce Python API and used that to fetch all the products with a created date of at most 7 days ago. I then looped through all those products to create the necessary items in the RSS File.

This worked well and good but I was basically just overwriting the one file and losing any previous weeks worth of New Products. I knew it would be easy enough to just write to a different file with the current date in the file name so it'd be new every time.

Trouble In Paradise

The plan was to use this RSS feed to generate a "What's new at Shoolu" email every week through MailChimp and their ability to work with RSS Feeds. The problem with the current approach of dating the files was that I was pointing Mailchimp at the one direct file so I'd have to update it every week which kind of defeats the purpose of all of this.

An Unexpected Solution

I tried digging around for a way to have a root domain (rss.shoolu.com) point to the latest file in the directory. There were several suggestions including using php to fetch the latest file and redirecting and some other less than elegant solutions.

None of them really tickled me the way a good solution does.

I finally decided to just update the python script to just output index.xml as well as latestProducts-[date].xml. That way index.xml would get overwritten with the currently accurate info and we'd also get the dated backups to log.

Then for simplicity's sake I just updated the Apache Virtual Host file for the domain:

<directory /var/www/rss/public/>
    ...
    DirectoryIndex index.xml
</directory>
Enter fullscreen mode Exit fullscreen mode

So the root domain would point directly to the file. Might not be the most elegant solution, but it works in this specific case so I'd figured I'd document it.

Originally Posted on my blog: Pointing A Domain to The Newest File In A Directory

Top comments (1)

Collapse
 
bgadrian profile image
Adrian B.G.

I think is the correct solution as well, but you can remove the duplicate data with a symlink.

You can update the symlink at each new xml creation, or put it as a background job.

#!/bin/bash

 ln -fs ./`ls -rt . | tail -n1` latest #symlink to the newst file
 #inotifywait -e attrib . #wait for the change or simple sleep
 sleep 0.1 #wait for the symlink to be updated

You can improve the ls with xml so you can avoid a circular symlink or other strange things.