Class: Cloudflare::Email::DevTunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare/email/dev_tunnel.rb

Overview

‘bin/rails cloudflare:email:dev` — spins up a cloudflared tunnel and updates the deployed Worker’s RAILS_INGRESS_URL secret so inbound mail can flow through to this local Rails server. No wrangler required.

Constant Summary collapse

INGRESS_PATH =
"/rails/action_mailbox/cloudflare/inbound_emails".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port:, io: $stdout) ⇒ DevTunnel

Returns a new instance of DevTunnel.



15
16
17
18
19
# File 'lib/cloudflare/email/dev_tunnel.rb', line 15

def initialize(port:, io: $stdout)
  @port       = port
  @io         = io
  @tunnel_pid = nil
end

Class Method Details

.call(port: 3000, io: $stdout) ⇒ Object



11
12
13
# File 'lib/cloudflare/email/dev_tunnel.rb', line 11

def self.call(port: 3000, io: $stdout)
  new(port: port, io: io).call
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cloudflare/email/dev_tunnel.rb', line 21

def call
  check_prerequisites

  require "cloudflare/email/credentials"
  deployer = Cloudflare::Email::WorkerDeployer.new(
    account_id: Cloudflare::Email::Credentials.,
    api_token:  Cloudflare::Email::Credentials.management_token,
  )

  start_tunnel
  tunnel_url = wait_for_tunnel_url
  @io.puts "  Tunnel:  #{tunnel_url}"

  ingress_url = "#{tunnel_url}#{INGRESS_PATH}"
  deployer.put_secret("RAILS_INGRESS_URL", ingress_url)
  @io.puts "  Worker '#{deployer.script_name}' RAILS_INGRESS_URL updated → #{ingress_url}"
  @io.puts ""
  @io.puts "  Send mail to your routed address; it'll land in this Rails app."
  @io.puts "  Ctrl-C to stop."
  @io.puts ""

  trap("INT")  { cleanup; exit 0 }
  trap("TERM") { cleanup; exit 0 }

  # Stay alive so the tunnel keeps running. The user stops us with Ctrl-C.
  sleep
ensure
  cleanup
end