5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2025-02-23 20:01:52 +00:00
panel/app/controllers/api/v1/sites_controller.rb

94 lines
2.5 KiB
Ruby
Raw Normal View History

2019-09-05 18:56:24 +00:00
# frozen_string_literal: true
module Api
module V1
# API para sitios
class SitesController < BaseController
SUBDOMAIN = ".#{Site.domain}"
TESTING_SUBDOMAIN = ".testing.#{Site.domain}"
PARTS = Site.domain.split('.').count
2020-02-06 16:11:17 +00:00
if Rails.env.production?
http_basic_authenticate_with name: ENV['HTTP_BASIC_USER'],
password: ENV['HTTP_BASIC_PASSWORD']
end
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
2024-12-28 19:37:42 +00:00
all_names = sites_names.concat(alternative_names).concat(api_names).concat(www_names).uniq.map do |name|
canonicalize name
end.reject do |name|
subdomain? name
end.reject do |name|
testing? name
2024-12-28 19:37:42 +00:00
end.uniq
render json: all_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
2024-12-23 19:36:27 +00:00
# @param query [ActiveRecord::Relation]
# @return [Array<String>]
def hostname_of(query)
query.pluck(Arel.sql("values->>'hostname'")).compact.uniq
end
2022-03-08 18:09:31 +00:00
def canonicalize(name)
name.end_with?('.') ? name[0..-2] : "#{name}.#{Site.domain}"
end
# Es un subdominio directo del dominio principal
#
# @param name [String]
# @return [Bool]
def subdomain?(name)
name.end_with?(SUBDOMAIN) && name.split('.').count == (PARTS + 1)
end
# Es un dominio de prueba
#
# @param name [String]
# @return [Bool]
def testing?(name)
name.end_with?(TESTING_SUBDOMAIN) && name.split('.').count == (PARTS + 2)
end
# Nombres de los sitios
2024-12-28 19:37:42 +00:00
#
# @param name [String]
# @return [Array<String>]
def sites_names
2024-12-28 19:37:42 +00:00
Site.all.order(:name).pluck(:name)
end
# Dominios alternativos, incluyendo todas las clases derivadas de
# esta.
#
# @return [Array<String>]
2020-05-29 15:42:39 +00:00
def alternative_names
hostname_of(DeployAlternativeDomain.all)
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.
#
# @return [Array<String>]
2020-05-29 15:42:39 +00:00
def api_names
Site.where(contact: true)
.or(Site.where(colaboracion_anonima: true))
2024-12-23 19:36:27 +00:00
.pluck(:name).map do |name|
"api.#{name}"
2022-03-08 18:09:31 +00:00
end
end
# Todos los dominios con WWW habilitado
2022-03-08 18:09:31 +00:00
def www_names
2024-12-23 19:36:27 +00:00
Site.where(id: DeployWww.all.pluck(:site_id)).pluck(:name).map do |name|
2024-12-28 19:37:42 +00:00
"www.#{name}"
end
2019-09-05 18:56:24 +00:00
end
end
end
end