summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-02-06 01:27:22 +0000
committerNick Thomas <nick@gitlab.com>2019-02-11 22:28:49 +0000
commitd4a07e66e30df6890c84feee2461ddd16345d1c2 (patch)
treee3ac84a41713ef02c3d7e75c500bc9f73ea97423
parenta1215556ec6c5ab84862519b82cee99645dad357 (diff)
downloadgitlab-ce-57322-eat-pages.tar.gz
Om nom nom57322-eat-pages
-rw-r--r--app/controllers/gitlab_pages_controller.rb51
-rw-r--r--config/routes.rb4
-rw-r--r--config/routes/pages.rb9
3 files changed, 64 insertions, 0 deletions
diff --git a/app/controllers/gitlab_pages_controller.rb b/app/controllers/gitlab_pages_controller.rb
new file mode 100644
index 00000000000..83e0e73b161
--- /dev/null
+++ b/app/controllers/gitlab_pages_controller.rb
@@ -0,0 +1,51 @@
+class GitlabPagesController < ActionController::Base
+ include RoutableActions
+
+ before_action :exact_pages_domain!
+ before_action :find_project!
+
+ attr_reader :project
+
+ SUFFIX_REGEXP = /\.#{Regexp.escape(Settings.pages.host)}\z/i.freeze
+
+ def show
+ # TODO: Now take the rest of the path and ask workhorse to extract and show
+ # the file in the artifact that it corresponds to, just like the "get a file
+ # from inside the artifact" API does
+ render plain: project.full_path
+ end
+
+ private
+
+ # TODO: implement access control for pages \o/
+ # This involves being an OAuth client against ourselves, kind of
+ def can?(*args)
+ true
+ end
+
+ def route_not_found
+ head :not_found
+ end
+
+ # We have no content to show for this case. Perhaps in future, we can allow
+ # admins to configure a pages site to use when this domain is reached.
+ def exact_pages_domain!
+ return unless request.host.downcase == Settings.pages.host.downcase
+
+ route_not_found
+ end
+
+ # This must be determined from the subdomain (if any), plus path
+ def find_project!
+ @project =
+ if SUFFIX_REGEXP.match?(request.host)
+ toplevel_group = request.host.sub(SUFFIX_REGEXP, '')
+
+ find_routable!(Project, toplevel_group + request.path)
+ else
+ PagesDomain.find_by(domain: request.host)&.project
+ end
+
+ route_not_found unless @project
+ end
+end
diff --git a/config/routes.rb b/config/routes.rb
index 484e05114be..6d365674920 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2,6 +2,10 @@ require 'sidekiq/web'
require 'sidekiq/cron/web'
Rails.application.routes.draw do
+
+ # Serve GitLab Pages from within Rails
+ draw :pages
+
concern :access_requestable do
post :request_access, on: :collection
post :approve_access_request, on: :member
diff --git a/config/routes/pages.rb b/config/routes/pages.rb
new file mode 100644
index 00000000000..0acc4de74f1
--- /dev/null
+++ b/config/routes/pages.rb
@@ -0,0 +1,9 @@
+get '*path',
+ to: 'gitlab_pages#show',
+ format: false,
+ constraints: ->(request) do
+ # Ensure we match *exactly* the Pages domain, or any subdomain of it.
+ request.host.downcase == Settings.pages.host.downcase ||
+ GitlabPagesController::SUFFIX_REGEXP.match?(request.host)
+ end
+