2019-09-05 18:56:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
# API para sitios
|
|
|
|
class SitesController < BaseController
|
2020-02-06 16:11:17 +00:00
|
|
|
http_basic_authenticate_with name: ENV['HTTP_BASIC_USER'],
|
|
|
|
password: ENV['HTTP_BASIC_PASSWORD']
|
|
|
|
|
2020-05-29 15:42:39 +00:00
|
|
|
# Lista de nombres de dominios a emitir certificados
|
2019-09-06 23:40:33 +00:00
|
|
|
def index
|
2023-04-19 17:56:20 +00:00
|
|
|
render json: alternative_names + api_names + www_names
|
2019-09-06 23:40:33 +00:00
|
|
|
end
|
|
|
|
|
2020-05-29 15:42:39 +00:00
|
|
|
private
|
2019-09-05 18:56:24 +00:00
|
|
|
|
2022-03-08 18:09:31 +00:00
|
|
|
def canonicalize(name)
|
|
|
|
name.end_with?('.') ? name[0..-2] : "#{name}.#{Site.domain}"
|
|
|
|
end
|
|
|
|
|
2023-04-19 17:56:56 +00:00
|
|
|
def subdomain?(name)
|
|
|
|
name.end_with? ".#{Site.domain}"
|
|
|
|
end
|
|
|
|
|
2020-05-29 15:42:39 +00:00
|
|
|
# Dominios alternativos
|
|
|
|
def alternative_names
|
2022-03-08 18:09:31 +00:00
|
|
|
(DeployAlternativeDomain.all.map(&:hostname) + DeployLocalizedDomain.all.map(&:hostname)).map do |name|
|
|
|
|
canonicalize name
|
2023-04-19 17:56:56 +00:00
|
|
|
end.reject do |name|
|
|
|
|
subdomain? name
|
2022-03-08 18:09:31 +00:00
|
|
|
end
|
2020-05-29 15:42:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Obtener todos los sitios con API habilitada, es decir formulario
|
|
|
|
# de contacto y/o colaboración anónima.
|
|
|
|
#
|
|
|
|
# TODO: Optimizar
|
|
|
|
def api_names
|
|
|
|
Site.where(contact: true)
|
|
|
|
.or(Site.where(colaboracion_anonima: true))
|
2022-03-08 18:09:31 +00:00
|
|
|
.select("'api.' || name as name").map(&:name).map do |name|
|
|
|
|
canonicalize name
|
2023-04-19 17:56:56 +00:00
|
|
|
end.reject do |name|
|
|
|
|
subdomain? name
|
2022-03-08 18:09:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-16 16:17:39 +00:00
|
|
|
# Todos los dominios con WWW habilitado
|
2022-03-08 18:09:31 +00:00
|
|
|
def www_names
|
2023-03-16 16:17:39 +00:00
|
|
|
Site.where(id: DeployWww.all.pluck(:site_id)).select("'www.' || name as name").map(&:name).map do |name|
|
|
|
|
canonicalize name
|
|
|
|
end
|
2019-09-05 18:56:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|