This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
74 lines (56 loc) · 2.38 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
$LOAD_PATH << "#{__dir__}/lib"
require "govuk_app_config/govuk_error"
require "govuk_app_config/govuk_healthcheck"
require "rack/logstasher"
require "gds_api/middleware/govuk_header_sniffer"
require "sinatra"
require "sinatra/reloader" if development?
require "content_config"
require "healthcheck/contentful_check"
require "publishing_api"
require "result"
require "webhook"
# This file will be auto reloaded in development and this can be configured twice
GovukError.configure unless GovukError.is_configured?
use Sentry::Rack::CaptureExceptions
configure :production do
# disable rack common logger so we can use a JSON one
set :logging, nil
# JSON logstash logging for production env
use Rack::Logstasher::Logger, Logger.new($stdout), extra_request_headers: { "GOVUK-Request-Id" => "govuk_request_id" }
# HTTP headers that are passed on to subsequent apps
use GdsApi::GovukHeaderSniffer, "HTTP_GOVUK_REQUEST_ID"
end
not_found { "Resource not found\n" }
get "/healthcheck/live" do
[200, { "Content-Type" => "text/plain" }, "OK"]
end
get "/healthcheck/ready" do
GovukHealthcheck.rack_response(Healthcheck::ContentfulCheck).call
end
post "/listener" do
begin
webhook = Webhook.new(request.env["HTTP_X_CONTENTFUL_TOPIC"], JSON.parse(request.body.read))
rescue JSON::ParserError
halt(400, "Invalid JSON payload")
end
halt(200, "No work done: #{webhook.environment} is not from the expected environment") unless webhook.expected_environment?
halt(200, "No work done: #{webhook.topic} is not an event that we track") unless webhook.event_of_interest?
# Potential async opportuntity
# It's unclear whether we'll be at risk of webhook's timing out if we try
# do too much with them. We may want to pass an id to a job.
affected_content = PublishingApi::AffectedContent.call(webhook.entity_id)
all_results = affected_content.flat_map do |item|
content_id = item.fetch(:content_id)
locale = item.fetch(:locale)
content_config = ContentConfig.find(content_id, locale)
next [Result.content_not_configured(content_id, locale)] unless content_config
results = []
results << PublishingApi::Updater.update_live(content_config) if webhook.live_change?
results << PublishingApi::Updater.update_draft(content_config)
results
end
all_results << Result.no_affected_content if all_results.empty?
status 200
body all_results.map(&:to_s).join("\n")
end