<commit_before>module PublishingAPI
  # To be set in dev mode so that this can run when the draft content store isn't running.
  cattr_accessor :swallow_connection_errors

  def self.register_service(name:, client:)
    @services ||= {}

    @services[name] = client
  end

  def self.service(name)
    @services[name] || raise(ServiceNotRegisteredException.new(name))
  end

  class ServiceNotRegisteredException < RuntimeError; end
end

PublishingAPI.register_service(
  name: :draft_content_store,
  client: ContentStoreWriter.new(Plek.find('draft-content-store'))
)

PublishingAPI.register_service(
  name: :live_content_store,
  client: ContentStoreWriter.new(Plek.find('content-store'))
)

if ENV['DISABLE_QUEUE_PUBLISHER'] || (Rails.env.test? && ENV['ENABLE_QUEUE_IN_TEST_MODE'].blank?)
  rabbitmq_config = { noop: true }
elsif ENV["RABBITMQ_URL"] && ENV["RABBITMQ_EXCHANGE"]
  rabbitmq_config = { exchange: ENV["RABBITMQ_EXCHANGE"] }
else
  rabbitmq_config = Rails.application.config_for(:rabbitmq).symbolize_keys
end

PublishingAPI.register_service(
  name: :queue_publisher,
  client: QueuePublisher.new(rabbitmq_config)
)

if Rails.env.development?
  PublishingAPI.swallow_connection_errors = true
end

# Statsd "the process" listens on a port on the provided host for UDP
# messages. Given that it's UDP, it's fire-and-forget and will not
# block your application. You do not need to have a statsd process
# running locally on your development environment.
statsd_client = Statsd.new("localhost")
statsd_client.namespace = "govuk.app.publishing-api"
PublishingAPI.register_service(name: :statsd, client: statsd_client)<commit_msg>Add bearer tokens for content store authentication<commit_after>module PublishingAPI
  # To be set in dev mode so that this can run when the draft content store isn't running.
  cattr_accessor :swallow_connection_errors

  def self.register_service(name:, client:)
    @services ||= {}

    @services[name] = client
  end

  def self.service(name)
    @services[name] || raise(ServiceNotRegisteredException.new(name))
  end

  class ServiceNotRegisteredException < RuntimeError; end
end

PublishingAPI.register_service(
  name: :draft_content_store,
  client: ContentStoreWriter.new(
    Plek.find('draft-content-store'),
    { bearer_token: ENV['DRAFT_CONTENT_STORE_BEARER_TOKEN'] },
  )
)

PublishingAPI.register_service(
  name: :live_content_store,
  client: ContentStoreWriter.new(
    Plek.find('content-store'),
    { bearer_token: ENV['CONTENT_STORE_BEARER_TOKEN'] },
  )
)

if ENV['DISABLE_QUEUE_PUBLISHER'] || (Rails.env.test? && ENV['ENABLE_QUEUE_IN_TEST_MODE'].blank?)
  rabbitmq_config = { noop: true }
elsif ENV["RABBITMQ_URL"] && ENV["RABBITMQ_EXCHANGE"]
  rabbitmq_config = { exchange: ENV["RABBITMQ_EXCHANGE"] }
else
  rabbitmq_config = Rails.application.config_for(:rabbitmq).symbolize_keys
end

PublishingAPI.register_service(
  name: :queue_publisher,
  client: QueuePublisher.new(rabbitmq_config)
)

if Rails.env.development?
  PublishingAPI.swallow_connection_errors = true
end

# Statsd "the process" listens on a port on the provided host for UDP
# messages. Given that it's UDP, it's fire-and-forget and will not
# block your application. You do not need to have a statsd process
# running locally on your development environment.
statsd_client = Statsd.new("localhost")
statsd_client.namespace = "govuk.app.publishing-api"
PublishingAPI.register_service(name: :statsd, client: statsd_client)
{'fraction_non_alphanumeric': 0.1023417172593235, 'fraction_numerical': 0.0, 'mean_word_length': 5.641074856046065, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5876538', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1126, 'n_words': 261}