Skip to content

Commit

Permalink
Add an http-path option
Browse files Browse the repository at this point in the history
For sj26#227, allow specifying an http prefix to mount mailcatcher at.
  • Loading branch information
sj26 authored and serg-kovalev committed Dec 25, 2017
1 parent 9ee7a7b commit f3d9edb
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 44 deletions.
23 changes: 14 additions & 9 deletions assets/javascripts/mailcatcher.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MailCatcher
e.preventDefault()
if confirm "You will lose all your received messages.\n\nAre you sure you want to clear all messages?"
$.ajax
url: "/messages"
url: new URL("messages")
type: "DELETE"
success: =>
@unselectMessage()
Expand Down Expand Up @@ -100,7 +100,7 @@ class MailCatcher
id = @selectedMessage()
if id?
$.ajax
url: "/messages/" + id
url: new URL("messages/#{id}")
type: "DELETE"
success: =>
messageRow = $("""#messages tbody tr[data-message-id="#{id}"]""")
Expand Down Expand Up @@ -221,7 +221,7 @@ class MailCatcher
messageRow.addClass("selected")
@scrollToRow(messageRow)

$.getJSON "/messages/#{id}.json", (message) =>
$.getJSON "messages/#{id}.json", (message) =>
$("#message .metadata dd.created_at").text(@formatDate message.created_at)
$("#message .metadata dd.from").text(message.sender)
$("#message .metadata dd.to").text((message.recipients || []).join(", "))
Expand All @@ -230,7 +230,7 @@ class MailCatcher
$el = $(el)
format = $el.attr("data-message-format")
if $.inArray(format, message.formats) >= 0
$el.find("a").attr("href", "/messages/#{id}.#{format}")
$el.find("a").attr("href", "messages/#{id}.#{format}")
$el.show()
else
$el.hide()
Expand All @@ -248,7 +248,7 @@ class MailCatcher
else
$("#message .metadata .attachments").hide()

$("#message .views .download a").attr("href", "/messages/#{id}.eml")
$("#message .views .download a").attr("href", "messages/#{id}.eml")

@loadMessageBody()

Expand All @@ -263,7 +263,7 @@ class MailCatcher
$("""#message .views .tab:not([data-message-format="#{format}"]).selected""").removeClass("selected")

if id?
$("#message iframe").attr("src", "/messages/#{id}.#{format}")
$("#message iframe").attr("src", "messages/#{id}.#{format}")

decorateMessageBody: ->
format = $("#message .views .tab.format.selected").attr("data-message-format")
Expand All @@ -280,7 +280,7 @@ class MailCatcher
message_iframe.find("html").html("<html><body>#{text}</html></body>")

refresh: ->
$.getJSON "/messages", (messages) =>
$.getJSON "messages", (messages) =>
$.each messages, (i, message) =>
unless @haveMessage message
@addMessage message
Expand All @@ -294,8 +294,13 @@ class MailCatcher

subscribeWebSocket: ->
secure = window.location.protocol is "https:"
protocol = if secure then "wss" else "ws"
@websocket = new WebSocket("#{protocol}://#{window.location.host}/messages")
url = new URL("messages", document.baseURI)
console.log(url)
url.protocol = if secure then "wss" else "ws"
console.log(url)
console.log(url.toString())
@websocket = new WebSocket(url.toString())
console.log(@websocket)
@websocket.onmessage = (event) =>
@addMessage $.parseJSON event.data

Expand Down
2 changes: 1 addition & 1 deletion assets/stylesheets/mailcatcher.css.sass
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ body > header
margin-left: 6px
padding: 6px
padding-left: 30px
background: url(/assets/logo.png) left no-repeat
background: url(logo.png) left no-repeat
font-size: 18px
font-weight: bold
a
Expand Down
51 changes: 34 additions & 17 deletions lib/mail_catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@ def self.reactor_running?
end
end

require "mail_catcher/events"
require "mail_catcher/mail"
require "mail_catcher/smtp"
require "mail_catcher/web"
require "mail_catcher/version"

module MailCatcher extend self
autoload :Events, "mail_catcher/events"
autoload :Mail, "mail_catcher/mail"
autoload :Smtp, "mail_catcher/smtp"
autoload :Web, "mail_catcher/web"

def env
ENV.fetch("MAILCATCHER_ENV", "production")
end

def development?
env == "development"
end

def which?(command)
ENV["PATH"].split(File::PATH_SEPARATOR).any? do |directory|
File.executable?(File.join(directory, command.to_s))
end
end

def mac?
RbConfig::CONFIG['host_os'] =~ /darwin/
RbConfig::CONFIG["host_os"] =~ /darwin/
end

def windows?
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
end

def macruby?
Expand All @@ -62,7 +71,7 @@ def browse url

def log_exception(message, context, exception)
gems_paths = (Gem.path | [Gem.default_dir]).map { |path| Regexp.escape(path) }
gems_regexp = %r{(?:#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)}
gems_regexp = %r{(?:#{gems_paths.join("|")})/gems/([^/]+)-([\w.]+)/(.*)}
gems_replace = '\1 (\2) \3'

puts "*** #{message}: #{context.inspect}"
Expand All @@ -72,10 +81,11 @@ def log_exception(message, context, exception)
end

@@defaults = {
:smtp_ip => '127.0.0.1',
:smtp_port => '1025',
:http_ip => '127.0.0.1',
:http_port => '1080',
:smtp_ip => "127.0.0.1",
:smtp_port => "1025",
:http_ip => "127.0.0.1",
:http_port => "1080",
:http_path => "/",
:verbose => false,
:daemon => !windows?,
:browse => false,
Expand Down Expand Up @@ -116,6 +126,12 @@ def parse! arguments=ARGV, defaults=@defaults
options[:http_port] = port
end

parser.on("--http-path PATH", String, "Add a prefix to all HTTP paths") do |path|
clean_path = Rack::Utils.clean_path_info("/#{path}")

options[:http_path] = clean_path
end

parser.on("--no-quit", "Don't allow quitting the process") do
options[:quit] = false
end
Expand All @@ -128,22 +144,22 @@ def parse! arguments=ARGV, defaults=@defaults
end

unless windows?
parser.on('-f', '--foreground', 'Run in the foreground') do
parser.on("-f", "--foreground", "Run in the foreground") do
options[:daemon] = false
end
end

if browseable?
parser.on('-b', '--browse', 'Open web browser') do
parser.on("-b", "--browse", "Open web browser") do
options[:browse] = true
end
end

parser.on('-v', '--verbose', 'Be more verbose') do
parser.on("-v", "--verbose", "Be more verbose") do
options[:verbose] = true
end

parser.on('-h', '--help', 'Display this help information') do
parser.on("-h", "--help", "Display this help information") do
puts parser
exit
end
Expand All @@ -167,7 +183,8 @@ def run! options=nil

puts "Starting MailCatcher"

Thin::Logging.silent = (ENV["MAILCATCHER_ENV"] != "development")
Thin::Logging.debug = development?
Thin::Logging.silent = !development?

# One EventMachine loop...
EventMachine.run do
Expand Down Expand Up @@ -216,7 +233,7 @@ def smtp_url
end

def http_url
"http://#{@@options[:http_ip]}:#{@@options[:http_port]}"
"http://#{@@options[:http_ip]}:#{@@options[:http_port]}#{@@options[:http_path]}"
end

def rescue_port port
Expand Down
2 changes: 1 addition & 1 deletion lib/mail_catcher/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MailCatcher
VERSION = "0.6.5"
VERSION = "0.7.0.beta1"
end
13 changes: 9 additions & 4 deletions lib/mail_catcher/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ module MailCatcher
module Web extend self
def app
@@app ||= Rack::Builder.new do
if ENV["MAILCATCHER_ENV"] == "development"
require "mail_catcher/web/assets"
map("/assets") { run Assets }
map(MailCatcher.options[:http_path]) do
if MailCatcher.development?
require "mail_catcher/web/assets"
map("/assets") { run Assets }
end

run Application
end

map("/") { run Application }
# This should only affect when http_path is anything but "/" above
run lambda { |env| [302, {"Location" => MailCatcher.options[:http_path]}, []] }
end
end

Expand Down
14 changes: 6 additions & 8 deletions lib/mail_catcher/web/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class Sinatra::Request
module MailCatcher
module Web
class Application < Sinatra::Base
set :development, ENV["MAILCATCHER_ENV"] == "development"
set :environment, MailCatcher.env
set :prefix, MailCatcher.options[:http_path]
set :asset_prefix, File.join(prefix, "assets")
set :root, File.expand_path("#{__FILE__}/../../../..")

if development?
Expand All @@ -25,7 +27,7 @@ class Application < Sinatra::Base
require "mail_catcher/web/assets"
Sprockets::Helpers.configure do |config|
config.environment = Assets
config.prefix = "/assets"
config.prefix = settings.asset_prefix
config.digest = false
config.public_path = public_folder
config.debug = true
Expand All @@ -37,12 +39,8 @@ class Application < Sinatra::Base
end
else
helpers do
def javascript_tag(name)
%{<script src="/assets/#{name}.js"></script>}
end

def stylesheet_tag(name)
%{<link rel="stylesheet" href="/assets/#{name}.css">}
def asset_path(filename)
File.join(settings.asset_prefix, filename)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ENV["MAILCATCHER_ENV"] = "test"
ENV["MAILCATCHER_ENV"] ||= "test"

require "minitest/autorun"
require "mail_catcher"
Expand Down
7 changes: 4 additions & 3 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<html class="mailcatcher">
<head>
<title>MailCatcher</title>
<link href="/favicon.ico" rel="icon" />
<%= stylesheet_tag "mailcatcher" %>
<%= javascript_tag "mailcatcher" %>
<base href="<%= settings.prefix.chomp("/") %>/">
<link href="favicon.ico" rel="icon">
<script src="<%= asset_path("mailcatcher.js") %>"></script>
<link rel="stylesheet" href="<%= asset_path("mailcatcher.css") %>">
</head>
<body>
<header>
Expand Down

0 comments on commit f3d9edb

Please sign in to comment.