• CORS woes on Heroku

    ,

    After spending the past 4 hours attempting to solve what boiled down to a rather simple problem, I figure I’d better blog about it to save someone else the time and effort.

    If you’ve been leveraging Passenger’s new –nginx-config-template command line option to add CORS headers to static assets served from a Rails app hosted on Heroku, and the CORS headers recently disappeared under mysterious circumstances… read on.

    I’ve been using the method described here to add CORS headers to custom fonts served from a Heroku-hosted Rails app that’s proxied by Nginx which handles serving static files. I recently updated to Rails 4.2.2 and suddenly, my custom fonts (.woff and .woff2 files) no longer had CORS headers on them.

    After the aforementioned hours spent scratching my head, I discovered that the latest version of the sprockets gem is generating asset digests that are 64 chars in length, where previously they had been 32. Nginx’s default regexp for identifying requests for static assets assumes the digest will be 32 chars long, like so:

    # Rails asset pipeline support.
    location ~ "^/assets/.+-[0-9a-f]{32}\..+" {
      error_page 490 = @static_asset;
      error_page 491 = @dynamic_request;
      recursive_error_pages on;</code>
    
      if (-f $request_filename) {
        return 490;
      }
      if (!-f $request_filename) {
        return 491;
      }
    }
    

    Changing the regexp to recognize digests that are 64 chars in length immediately solved the problem:

    location ~ "^/assets/.+-[0-9a-f]{64}\..+" {
       ...
    }
    

    I had to laugh after something so stupid and silly cost me a good chunk of my Saturday to debug. But at least it’s working now. My statically served custom fonts have the correct CORS headers and Chrome and Firefox are happy again.


Need help?

I’m an independent software developer available for consulting, contract work, or training. Contact me if you’re interested.


  • Ruby Hoedown 2012 recap

    Ruby Hoedown 2012This year’s Ruby Hoedown was at the Scarritt Bennet Center in Nashville. Per his usual, Jeremy McAnally organized a top-notch, free, two day regional Ruby conference that was a pleasure to attend. A lot of work goes into organizing this type of thing. I doff my proverbial hat to Jeremy for making the Hoedown a reality for six years straight.

    I wasn’t able to attend the Hoedown last year so this was my first experience at Scarritt Bennett. The gothic architecture was quite beautiful and made for some lovely ad-hoc photos taken with my iPhone. The presentation room was comfortable enough and just the right size for the nearly 250 developers who attended. Power was in short supply the first day, but the problem was quickly rectified (EE pun) by the appearance of a plethora of extension cords and power strips. By the end of the day there was enough power for everyone who wanted it. Wi-fi remained stable throughout the conference.

    Brad Winfrey gave the first talk of the day, titled “gem install erlang” [slides]. I’m not a functional language guy, but Brad’s talk made me want to look at erlang again. What was most impressive to me was his demonstration of erlang’s built-in pattern matching. I can see how someone could get addicted to that kind of power.

    Next was Phil Harvey with “REST & Hypermedia” [slides]. If you’ve ever wanted to change an API without breaking things for your existing users, Phil’s talk gave a solution in hypermedia. He demonstrated various ways to link together resources using calls that return link relations. The server essentially builds URLs dynamically for the user. He also made the point that if you aren’t using hypermedia, you aren’t really using REST.

    GitHub was well represented at the conference. Brandon Keepers, one of their developers, presented on “Why Our Code Smells” [slides]. I always appreciate suggestions on how to make my code better and Brandon did not disappoint. The biggest idea I took away from his talk was to strive for clean separation between the ORM and business models. In other words, reduce coupling to the framework (e.g. ActiveRecord).

    Jeffrey Baird gave a talk titled “Growing Your Own Developers: Hiring Programmers with Little to No Experience” [slides]. I really appreciated this talk since a big catalyst to pursuing programming as a career was an 8-month apprenticeship at RoleModel Software during my sophomore year of college. Jeffrey made the point that computer science majors are not predicted to meet labor demands through 2016. One way companies can find the talent they need is to hire motivated, passionate beginners and give them the tools and training they need to grow into experts.

    To conclude day 1, Dave Worth presented “Static Analysis in Ruby Applications with Brakeman” [slides] which I unfortunately missed.

    Jeremy Holland kicked off day 2 with “Using System V Shared Memory in Ruby Projects” [slides], a highly technical but very enjoyable mini-tutorial on how to use and manage shared memory with C. The problem he was trying to solve was to quickly search a massive binary tree. Ruby has no concept of shared memory, requiring C to be brought into the equation. It was nice hearing about another tool on the programmer tool-belt that can be used to solve problems like this.

    Will Farrington introduced us to “The Setup” [slides] which is GitHub’s answer to the problem of managing an army of developer laptops. The Setup uses the CLI and Puppet and has been in development for 6 months. It enables a developer to script a configuration for his laptop (e.g. Apache, Ruby, RVM, a text editor, custom Bash aliases, etc) and have that configuration automatically installed on a new MacBook.

    “Adhearsion: Telephony Through Ruby-colored Lenses” [slides] by Ben Klang was another presentation I unfortunately had to miss. But I’m sure he knocked it out of the park, to borrow the colloquialism.

    Lance Ball presented “Sleep Better with TorqueBox” [slides], an introduction to the Java-based JBoss 7 application server. He quickly pointed out that you don’t need to know Java to use it, and that no instrumentation is required for Rails apps. In fact, the server supports any Rack-based application and provides scheduled jobs, robust background processing, long-running daemons, caching, messaging, web sockets, and clustering.

    Lightning talks have been an important part of the conference each year and 2012 was no exception. Talks were given by Will Farrington, Brandon Valentine, Ernie Miller, Cameron Dukes, Yossef Mendelssohn, Frank Rietta, Chad Taylor, Loren Norman, Edward Anderson, Winston Hearn, and Jeremy McAnally.

    Anthony Eden gave the keynote which wrapped up day 2 and the conference itself. His presentation was a nice mix of nerdy technical content (Lisp, Clojure, Erlang) along with some plain old motivational talk. He encouraged us to keep building things, to expand our toolbox by learning new programming languages, to share our experiences with others, and to never stop having fun.

    This has been my first Ruby conference in over a year. It was great to reconnect with the community, make some new friends, and learn about various software projects people are working on. I left Nashville feeling recharged and ready to put into practice what I had learned.

    You should consider attending the Hoedown in 2013. Maybe I’ll see you there.

  • Quote of the Week: F.A. Hayek

    “Freedom and liberty are now words so worn with use and abuse that one must hesitate to employ them to express the ideals for which they [used to stand.]” — F.A. Hayek

  • Homesteading at indieconf

    indieconf, Raleigh’s first conference for freelancers, took place Saturday, November 13th at the McKimmon Center near NCSU. It was, in a word, exceptional. And they had brownies.

    I was only able to attend the afternoon sessions, but the content was fantastic. It was even better getting a chance to interact with fellow attendees. Being a Ruby developer, it’s easy to get tied down with a single local network. Attending indieconf gave me the opportunity to interact with an entirely different network of people. The smaller nature of the conference made it feel like I actually had a chance to meet everyone. It was a great experience!

    My session was Homesteading for Freelancers. You can read the brief at the link so I won’t repeat it. For those who attended, please download the slides and also consider rating the talk. Any feedback you can provide will help me improve. I appreciate it!

    Kudos to Michael Kimsal for organizing the conference. He obviously put a lot of thought, care, and elbow grease into making indieconf a success. Little touches like the smiley/frowney whiteboard for providing feedback were really nice. This is definitely something I want to attend next year. My only regret this year is that I wasn’t able to be there for the full day.

  • Announcing youtube_tags extension for Radiant

    A recent project required the latest YouTube videos from a specific user to be listed inside a Radiant page. There wasn’t an existing extension that did this so I built my own. youtube_tags is based on the excellent twitter_tags extension. It enables inclusion of YouTube videos within Radiant pages using a series of Radius tags. It leverages the youtube-g gem to pull data directly from the GData API.

    For example, this is how you would display linked titles for the top 5 videos from my YouTube account (“pelargir”) using the Radius tags provided by the extension:

    <ul>
      <r:youtube user="pelargir">
        <r:videos count="5">
          <li><a href="<r:video:url />"><r:video:title /></a></li>
        </r:videos>
      </r:youtube>
    </ul>
    

    To install in your own project, visit the youtube_tags profile in the Radiant extension registry.