Skip to content

Instantly share code, notes, and snippets.

@vosechu
Last active December 13, 2015 20:49
Show Gist options
  • Save vosechu/4972929 to your computer and use it in GitHub Desktop.
Save vosechu/4972929 to your computer and use it in GitHub Desktop.
Simple contact form Rack middleware for use in middleman #pcs_code_example
# Modified version of TryStatic, from rack-contrib
# https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/try_static.rb
# Serve static files under a `build` directory:
# - `/` will try to serve your `build/index.html` file
# - `/foo` will try to serve `build/foo` or `build/foo.html` in that order
# - missing files will try to serve build/404.html or a tiny default 404 page
use Rack::Reloader, 0
require './rack_emailer'
use Rack::Emailer
require './trystatic'
use Rack::TryStatic, :root => "build", :urls => %w[/], :try => ['.html', 'index.html', '/index.html']
# Run your own Rack app here or use this one to serve 404 messages:
run lambda{ |env|
not_found_page = File.expand_path("../build/404.html", __FILE__)
if File.exist?(not_found_page)
[ 404, { 'Content-Type' => 'text/html'}, [File.read(not_found_page)] ]
else
[ 404, { 'Content-Type' => 'text/html' }, ['404 - page not found'] ]
end
}
require 'mail'
module Rack
class Emailer
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.path == '/send-email' && request.post?
honeypot = request.params['b6314b059ffe32e6b84df0616d4bdbbe']
if honeypot && !honeypot.empty?
puts 'spammer attempt'
return [406, {'Location' => request.base_url + '/contact', 'Content-Type' => 'text/html'}, ['']]
else
deliver_mail(request.params)
return [301, {'Location' => request.base_url + '/contact/success', 'Content-Type' => 'text/html'}, ['']]
end
end
@app.call(env)
end
def deliver_mail(params)
begin
message = ''
message << "from: #{params['sender']}\n"
message << "body: #{params['message']}\n"
to = 'Portland Code School <[email protected]>'
from = 'Portland Code School <[email protected]>'
Mailer.deliver(
from, to, params['subject'], message)
rescue Exception => e
puts "Mail send failure #{e}"
# TODO: Log this error somewhere intelligent
end
end
end
end
Mail.defaults do
delivery_method :smtp,
{
:address => "smtp.sendgrid.net",
:port => 587,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => 'plain',
:enable_starttls_auto => true,
:domain => "portlandcodeschool.com" # I usually set this as an env. var too
}
end
module Mailer
def self.deliver(msg_sender, msg_receiver, msg_subject, msg_body)
Mail.deliver do
from msg_sender
to msg_receiver
subject msg_subject
text_part do
body msg_body
end
end
end
end
module Rack
class TryStatic
def initialize(app, options)
@app = app
@try = ['', *options.delete(:try)]
@static = ::Rack::Static.new(lambda { [404, {}, []] }, options)
end
def call(env)
orig_path = env['PATH_INFO']
found = nil
@try.each do |path|
resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
break if 404 != resp[0] && found = resp
end
found or @app.call(env.merge!('PATH_INFO' => orig_path))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment