Subscribe to my Feed, follow me on Twitter, recommend me on Working With Rails or see my code on GitHub
There's a Gopher on the Rails

In the beginning of the Internet, before there was even a Web 0.1, there was Gopher. It was awesome. Oh, wait, no it wasn’t. That’s why no-one uses it anymore. But it didn’t suck, and most decent Web browsers still support it. So, how can you integrate this with your Rails app to make your very own phlog? You can’t really, because Rails is HTTP and Gopher is not. But you can write a simple Gopher server that uses ActiveRecord to fetch your blog posts:
require File.dirname(__FILE__)+'/../config/environment'
require 'socket'
hostname = "tore.darell.no"
port = 70
server = TCPServer.new('0.0.0.0', port)
while session = server.accept
request = session.gets.chomp
begin
if request.blank? #index
session.puts "iWelcome to my phlog.\tfake\t(NULL)\t0"
session.puts "i \tfake\t(NULL)\t0"
Post.find_published(:all, :order => "created_at DESC").each do |post|
session.puts "0#{post.title}\t#{post.slug}\t#{hostname}\t#{port}"
end
else #a post
post = Post.find_by_slug(request)
session.puts "#{post.title} by Tore Darell on #{post.created_at.strftime('%Y-%m-%d %H:%M')}"
session.puts
session.puts post.summary
session.puts
session.puts post.body
end
rescue Exception => e
session.puts request.blank? ? "iFAIL\tfake\t(NULL)\t0" : "OH NOES :("
ensure
session.close
end
end
It loads the entire Rails environment, which probably certainly isn’t necessary, but it’s just easier that way. To start the server, you probably have to be root. Also, remember to set RAILS_ENV if necessary:
RAILS_ENV=production sudo ruby script/gopher &
And just like that your blog has been transformed into a living relic from the early 90s. Take that, Web 2.0!

Comments
Atom feed
Leave a comment