Skip to content

Commit

Permalink
First go at reverse proxy for plausible.io
Browse files Browse the repository at this point in the history
  • Loading branch information
mlandauer committed Nov 6, 2024
1 parent 503adee commit fde001b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ def compress(string)
end
end

# This is for proxying requests to this server to plausible.io for analytics.
# For some reason didn't work putting this in config/initializers/proxy.rb
# TODO: Fix this
class PlausibleProxy < Rack::Proxy
def perform_request(env)
request = Rack::Request.new(env)

# use rack proxy for anything hitting plausible analytics endpoints
if request.path =~ %r{^/js/script\.} || request.path =~ %r{^/api/event}
# most backends required host set properly, but rack-proxy doesn't set this for you automatically
# even when a backend host is passed in via the options
env["HTTP_HOST"] = "plausible.io"

# don't send your sites cookies to target service, unless it is a trusted internal service that can parse all your cookies
env['HTTP_COOKIE'] = ''

env['content-length'] = nil

super(env)
else
@app.call(env)
end
end
end

module PlanningalertsApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
Expand All @@ -46,6 +71,7 @@ class Application < Rails::Application
# config.i18n.default_locale = :de

config.middleware.use Rack::Attack
config.middleware.use PlausibleProxy

config.action_dispatch.tld_length = 2

Expand Down

0 comments on commit fde001b

Please sign in to comment.