Skip to content

Commit

Permalink
Merge pull request #1357 from alphagov/remove-router-api
Browse files Browse the repository at this point in the history
Remove route registration to Router API
  • Loading branch information
theseanything authored Dec 16, 2024
2 parents c32bce6 + 719a67e commit 1d9db41
Show file tree
Hide file tree
Showing 19 changed files with 4 additions and 769 deletions.
1 change: 0 additions & 1 deletion app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def update
def destroy
content_item = ContentItem.find_by!(base_path: encoded_base_path)

content_item.delete_routes
content_item.destroy!

render status: :ok
Expand Down
49 changes: 0 additions & 49 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def self.revert(previous_item:, item:)

def self.create_or_replace(base_path, attributes, log_entry)
previous_item = ContentItem.where(base_path:).first
item_state_before_change = previous_item.dup

lock = UpdateLock.new(previous_item)

Expand Down Expand Up @@ -46,7 +45,6 @@ def self.create_or_replace(base_path, attributes, log_entry)
begin
transaction do
item.save!
item.register_routes(previous_item: item_state_before_change)
end
rescue StandardError
result = false
Expand Down Expand Up @@ -105,10 +103,6 @@ def description
end
end

def redirect?
schema_name == "redirect"
end

def gone?
# we've overloaded gone a bit by adding explanation and alternative
# url to the schema to support Whitehall unpublishing. We need to consider
Expand All @@ -119,21 +113,6 @@ def gone?
schema_name == "gone" && details_is_empty?
end

def router_rendering_app
# This is an extension of the hack in `gone?` method.
#
# For items that are registered in the content store which are not redirects
# or gones we need to have a rendering_app. This is fine for all but the
# "gone but not gone" exception defined in `gone?` where rendering_app is
# not part of the schema for a gone but is required to register the route.
#
# This rather nastily fallsback to government frontend for gones that are
# not gone and lack a rendering_app
return rendering_app if schema_name != "gone" || gone?

rendering_app || "government-frontend"
end

def valid_auth_bypass_id?(auth_bypass_id)
return false unless auth_bypass_id
return true if auth_bypass_ids&.include?(auth_bypass_id)
Expand All @@ -156,40 +135,12 @@ def access_limited?
access_limited_user_ids.any? || access_limited_organisation_ids.any?
end

def register_routes(previous_item: nil)
return unless should_register_routes?(previous_item:)

tries = Rails.application.config.register_router_retries
begin
route_set.register!
rescue GdsApi::BaseError
tries -= 1
tries.positive? ? retry : raise
end
end

def delete_routes
return unless should_register_routes?

route_set.delete!
end

def base_path_without_root
base_path&.sub(%r{^/}, "")
end

def route_set
@route_set ||= RouteSet.from_content_item(self)
end

private

def should_register_routes?(previous_item: nil)
return previous_item.route_set != route_set if previous_item

true
end

def access_limited_user_ids
access_limited.fetch("users", [])
end
Expand Down
12 changes: 0 additions & 12 deletions app/models/publish_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ def self.find_by_path(path)
validates :publish_time, presence: true
validates :rendering_app, presence: true, format: /\A[a-z0-9-]*\z/

after_save :register_routes

def as_json(options = nil)
super(options).tap do |hash|
hash["errors"] = errors.as_json.stringify_keys if errors.any?
Expand All @@ -55,14 +53,4 @@ def self.cleanup_expired
def base_path_without_root
base_path&.sub(%r{^/}, "")
end

private

def route_set
@route_set ||= RouteSet.from_publish_intent(self)
end

def register_routes
route_set.register!
end
end
130 changes: 0 additions & 130 deletions app/models/route_set.rb

This file was deleted.

2 changes: 0 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,5 @@ class Application < Rails::Application
config.minimum_ttl = [config.default_ttl, 5.seconds].min

config.paths["log"] = ENV["LOG_PATH"] if ENV["LOG_PATH"]

config.register_router_retries = 3
end
end
9 changes: 4 additions & 5 deletions docs/route_registration.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
## Registering routes with the router.
## How router loads routes

After saving an item, the content store will register routes with the router.
All items listed in the routes array will be created as routes pointing at the
`rendering_app`. The routes for a content item must contain a route for the
`base_path`.
Router will query the content store database to load routes. All items listed
in the routes array will be created as routes pointing at the `rendering_app`.
The routes for a content item must contain a route for the `base_path`.

All entries in the routes array must be under the `base_path` (ie either a
subpath of the `base_path`, or the `base_path` with an extension)
Expand Down
13 changes: 0 additions & 13 deletions lib/tasks/register_backends.rake

This file was deleted.

13 changes: 0 additions & 13 deletions lib/tasks/routes.rake

This file was deleted.

23 changes: 0 additions & 23 deletions spec/factories/route_set.rb

This file was deleted.

12 changes: 0 additions & 12 deletions spec/integration/deleting_a_content_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
context "when the content item exists" do
before do
FactoryBot.create(:content_item, base_path:)

@delete_stubs = ContentItem.find_by(
base_path:,
).routes.map do |route|
stub_route_deleted(route["path"], hard_delete: true)
end
end

it "deletes the content item" do
Expand All @@ -32,12 +26,6 @@
expect(ContentItem.where(base_path:).count).to eq(0)
end

it "deletes the routes" do
delete "/content/vat-rates"

@delete_stubs.each { |stub| assert_requested(stub, times: 1) }
end

it "returns a 200" do
delete "/content/vat-rates"

Expand Down
Loading

0 comments on commit 1d9db41

Please sign in to comment.