#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../lib/fakebook' require 'optparse' require 'rack' begin server = Rack::Handler::Mongrel rescue LoadError => e server = Rack::Handler::WEBrick end rack_options = { :Port => 5000, :Host => "0.0.0.0" } fakebook_options = { :callback => "http://0.0.0.0:3000/", :canvas => "myapp", :secret => "secret", :fb_params => { :user => 1, :session_key => 'session_key', :friends => [2, 3, 4] } } config_file = "config/fakebook.yml" OptionParser.new do |opts| opts.banner = "Fakebook #{Fakebook::Version}. Usage: fakebook [options]" opts.separator "" opts.separator "Options:" opts.on("-f", "--config [PATH]", "path to a YAML config file (default: #{config_file})") { |v| config_file = v } opts.on("-c", "--callback [URL]", "the callback URL of your application (default: #{fakebook_options[:callback]})") { |v| fakebook_options[:callback] = v } opts.on("-a", "--canvas [PATH]", "the canvas path of your application (default: #{fakebook_options[:canvas]})") { |v| fakebook_options[:canvas] = v } opts.on("-s", "--secret [SECRET]", "the application secret (default: #{fakebook_options[:secret]})") { |v| fakebook_options[:secret] = v } opts.on("-u", "--user [USER_ID]", "the fb id of the user (default: #{fakebook_options[:fb_params][:user]})") { |v| fakebook_options[:fb_params][:user] = v } opts.on("-k", "--session [SESSION_KEY]", "the user's session key (default: #{fakebook_options[:fb_params][:session_key]})") { |v| fakebook_options[:fb_params][:session_key] = v } opts.on("--friends [FRIENDS]", "the user's friends (default: #{fakebook_options[:fb_params][:friends]})") { |v| fakebook_options[:fb_params][:friends] = v } opts.on("-h", "--host [HOST]", "host to listen on (default: #{rack_options[:Host]})") { |host| rack_options[:Host] = host } opts.on("-p", "--port [PORT]", "port to use (default: #{rack_options[:Port]})") { |port| rack_options[:Port] = port } opts.on("--server [SERVER]", "web server to (e.g., webrick, mongrel. default: mongrel)") { |s| case s when "mongrel" server = Rack::Handler::Mongrel when "webrick" server = Rack::Handler::WEBrick end } opts.on_tail("--help", "Show this message") do puts opts exit end end.parse! if File.exists?(config_file) config = YAML.load_file(config_file) || {} fakebook_options[:callback] = config['callback'] if config['callback'] fakebook_options[:canvas] = config['canvas'] if config['canvas'] fakebook_options[:secret] = config['secret'] if config['secret'] fakebook_options[:fb_params][:user] = config['user'] if config['user'] fakebook_options[:fb_params][:session_key] = config['session'] if config['session'] fakebook_options[:fb_params][:friends] = config['friends'] if config['friends'] rack_options[:Host] = config['host'] if config['host'] rack_options[:Port] = config['port'] if config['port'] end fakebook = Fakebook.new(fakebook_options) app = Rack::Builder.new { use Rack::CommonLogger, STDERR use Rack::ShowExceptions use Rack::Lint run fakebook } STDERR.puts "Starting Fakebook proxy from http://#{rack_options[:Host]}:#{rack_options[:Port]}/#{fakebook.canvas}/ to #{fakebook.callback}" STDERR.puts " Using fb_params: #{fakebook.fb_params.inspect}" server.run app, rack_options