Skip to content

Commit

Permalink
Merge pull request #23 from qedi-r/rproxy-support
Browse files Browse the repository at this point in the history
Improve support for hosting behind a reverse proxy
  • Loading branch information
ryanckulp authored Feb 12, 2025
2 parents c1a6ad5 + bcbc660 commit 175e3c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
17 changes: 12 additions & 5 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'sinatra'
require 'sinatra/activerecord'
require 'debug'
require 'uri'

require_relative 'config/initializers/tailwind_form'
require_relative 'config/initializers/explicit_forme_plugin'
Expand All @@ -13,6 +14,11 @@
set :bind, '0.0.0.0'
set :port, 4567

configure :production do
base_url = URI.parse(ENV['BASE_URL'])
use Rack::Protection::HostAuthorization, permitted_hosts: [base_url.host]
end

# make model, service classes accessible
%w[models services].each do |sub_dir|
Dir["#{Dir.pwd}/#{sub_dir}/*.rb"].each { |file| require file }
Expand All @@ -38,6 +44,7 @@ def create_forme(model, _is_edit, attrs = {}, options = {})

configure do
set :json_encoder, :to_json
set :base_url, ENV['BASE_URL']
end

configure :development, :test, :production do
Expand All @@ -47,8 +54,8 @@ def create_forme(model, _is_edit, attrs = {}, options = {})
# DEVICE MANAGEMENT
def devices_form(device)
create_forme(device, device.persisted?,
{ autocomplete: 'off', action: '/devices' },
{ namespace: 'device' })
{autocomplete:"off", action: "#{ENV['BASE_URL']}/devices"},
{namespace: "device"})
end

get '/devices/?' do
Expand All @@ -65,7 +72,7 @@ def devices_form(device)
get '/devices/:id/delete' do
@device = Device.find(params[:id])
@device.destroy
redirect to('/devices')
redirect to("#{ENV['BASE_URL']}/devices")
end

get '/devices/:id/edit' do
Expand All @@ -77,12 +84,12 @@ def devices_form(device)
patch '/devices/:id' do
device = Device.find(params[:id])
device.update(params[:device])
redirect to('/devices')
redirect to("#{ENV['BASE_URL']}/devices")
end

post '/devices' do
Device.create!(params[:device])
redirect to('/devices')
redirect to("#{ENV['BASE_URL']}/devices")
end

get '/' do
Expand Down
8 changes: 4 additions & 4 deletions views/devices/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</div>
</div>
<div>
<a href="/devices/new" class="cursor-pointer font-medium rounded-lg text-sm px-3 py-2 inline-flex items-center transition duration-150 justify-center shrink-0 gap-1.5 whitespace-nowrap text-white bg-blue-500 hover:bg-blue-600 focus:outline-none">
<a href="<%= settings.base_url %>/devices/new" class="cursor-pointer font-medium rounded-lg text-sm px-3 py-2 inline-flex items-center transition duration-150 justify-center shrink-0 gap-1.5 whitespace-nowrap text-white bg-blue-500 hover:bg-blue-600 focus:outline-none">
Add New
</a>
</div>
Expand All @@ -15,7 +15,7 @@

<div class="flex flex-col overflow-hidden rounded-xl border border-gray-300 bg-gray-50">
<% @devices.each do |device| %>
<div class="flex border-b last:border-b-0 border-gray-100 transition hover:bg-white dark:hover:bg-gray-750 dark:border-gray-700" data-turbo="false" href="/devices/<%= device.id %>/edit">
<div class="flex border-b last:border-b-0 border-gray-100 transition hover:bg-white dark:hover:bg-gray-750 dark:border-gray-700" data-turbo="false" href="<%= settings.base_url %>/devices/<%= device.id %>/edit">
<div class="flex grow flex-row items-center justify-between px-5 py-5">
<div>
<h3 class="font-medium text-gray-900"><%= device.name %></h3>
Expand All @@ -32,8 +32,8 @@
</div> -->
</div>
<div class="">
<a href="/devices/<%= device.id %>/delete" class="font-medium text-red-500 rounded-lg text-xs px-2 py-1 inline-flex items-center transition duration-150 justify-center shrink-0 gap-1.5 whitespace-nowrap text-primary-500 bg-primary-100 dark:bg-primary-900 hover:bg-primary-200 dark:hover:bg-primary-800 focus:outline-none">Delete</a>
<a href="/devices/<%= device.id %>/edit" class="font-medium text-blue-500 rounded-lg text-xs px-2 py-1 inline-flex items-center transition duration-150 justify-center shrink-0 gap-1.5 whitespace-nowrap text-primary-500 bg-primary-100 dark:bg-primary-900 hover:bg-primary-200 dark:hover:bg-primary-800 focus:outline-none">Edit</a>
<a href="<%= settings.base_url %>/devices/<%= device.id %>/delete" class="font-medium text-red-500 rounded-lg text-xs px-2 py-1 inline-flex items-center transition duration-150 justify-center shrink-0 gap-1.5 whitespace-nowrap text-primary-500 bg-primary-100 dark:bg-primary-900 hover:bg-primary-200 dark:hover:bg-primary-800 focus:outline-none">Delete</a>
<a href="<%= settings.base_url %>/devices/<%= device.id %>/edit" class="font-medium text-blue-500 rounded-lg text-xs px-2 py-1 inline-flex items-center transition duration-150 justify-center shrink-0 gap-1.5 whitespace-nowrap text-primary-500 bg-primary-100 dark:bg-primary-900 hover:bg-primary-200 dark:hover:bg-primary-800 focus:outline-none">Edit</a>
</div>
</div>
</div>
Expand Down

0 comments on commit 175e3c5

Please sign in to comment.