summaryrefslogtreecommitdiff
path: root/lib/api/internals/pages.rb
blob: 1f0d5a99d834d549313e01ebbfb35f9dd23cd3d1 (plain)
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
# frozen_string_literal: true

module API
  module Internals
    class Pages < Grape::API
      before { authenticate_by_gitlab_pages_token! }

      namespace 'internals' do
        params do
          requires :host, type: String, desc: 'The Pages host'
        end
        get 'pages/query' do
          if namespace = find_namespace(params[:host])
            render namespace,
              using: API::Entities::Pages::NamespaceDomain,
              prefix: namespace.full_path
          elsif domain = find_pages_domain(params[:host])
            render domain,
              using: API::Entities::Pages::PagesDomain,
              prefix: domain.project.full_path
          else
            status :not_found
          end
        end
      end

      helpers do
        def find_namespace_name(host)
          host = host.downcase
          gitlab_host = "." + ::Settings.pages.host.downcase
          host.delete_suffix(gitlab_host) if host.ends_with?(gitlab_host)
        end

        def find_namespace(host)
          namespace_name = find_namespace_name(host)
          Namespace.find_by_full_path(namespace_name) if namespace_name
        end

        def find_pages_domain(host)
          PagesDomain.find_by(domain: host.downcase)
        end
      end
    end
  end
end