From 237c2257328ff997a8d41cc9180262cdc1fecca5 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Tue, 6 Jul 2021 17:02:12 +0200 Subject: [PATCH] Add support for running CodeOcean under a subpath * Also refactor (JavaScript) routes --- README.md | 3 ++- app/assets/javascripts/dashboard.js | 2 +- app/assets/javascripts/editor/editor.js.erb | 2 +- .../javascripts/editor/participantsupport.js.erb | 2 +- app/assets/javascripts/exercises.js.erb | 9 ++++----- ...wn_ace_editor.js => markdown_ace_editor.js.erb} | 2 +- ..._statistics.js => submission_statistics.js.erb} | 2 +- app/helpers/statistics_helper.rb | 2 +- app/views/request_for_comments/show.html.slim | 14 +++++++------- config.ru | 6 ++++-- config/application.rb | 4 +++- config/environments/staging.rb | 3 --- config/initializers/session_store.rb | 5 ++++- config/webpack/environment.js | 12 ++++++++++-- 14 files changed, 40 insertions(+), 28 deletions(-) rename app/assets/javascripts/{markdown_ace_editor.js => markdown_ace_editor.js.erb} (82%) rename app/assets/javascripts/{submission_statistics.js => submission_statistics.js.erb} (97%) diff --git a/README.md b/README.md index cf63dc3b..1c02bc0c 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ In order to execute code submissions using Docker, source code files are written ## Production Setup -- We recommend using [Capistrano](http://capistranorb.com/) for deployment +- We recommend using [Capistrano](http://capistranorb.com/) for deployment. +- Once deployed, CodeOcean assumes to run exclusively under a (sub)domain. If you want to use it under a custom subpath, you can specify the desired path using an environment variable: `RAILS_RELATIVE_URL_ROOT=/codeocean`. Please ensure to rebuild all assets and restart the server to apply the new path. ## Monitoring - We use a [Prometheus Exporter](https://github.com/discourse/prometheus_exporter) and a [Telegraf Client](https://github.com/jgraichen/telegraf-ruby) diff --git a/app/assets/javascripts/dashboard.js b/app/assets/javascripts/dashboard.js index c8b1ae51..6ae38079 100644 --- a/app/assets/javascripts/dashboard.js +++ b/app/assets/javascripts/dashboard.js @@ -51,7 +51,7 @@ $(document).on('turbolinks:load', function() { } else { var jqxhr = $.ajax({ dataType: 'json', - url: '/admin/dashboard', + url: Routes.admin_dashboard_path(), method: 'GET' }); jqxhr.done(function(response) { diff --git a/app/assets/javascripts/editor/editor.js.erb b/app/assets/javascripts/editor/editor.js.erb index f94f9e16..98e095f3 100644 --- a/app/assets/javascripts/editor/editor.js.erb +++ b/app/assets/javascripts/editor/editor.js.erb @@ -1,7 +1,7 @@ var CodeOceanEditor = { //ACE-Editor-Path // ruby part adds the relative_url_root, if it is set. - ACE_FILES_PATH: '<%= (defined? Rails.application.config.relative_url_root) && Rails.application.config.relative_url_root != nil && Rails.application.config.relative_url_root != "" ? Rails.application.config.relative_url_root : "" %>' + '/assets/ace/', + ACE_FILES_PATH: '<%= "#{Rails.application.config.relative_url_root.chomp('/')}/assets/ace/" %>', THEME: 'ace/theme/textmate', //Color-Encoding for Percentages in Progress Bars (For submissions) diff --git a/app/assets/javascripts/editor/participantsupport.js.erb b/app/assets/javascripts/editor/participantsupport.js.erb index ba9e03ec..af4cb501 100644 --- a/app/assets/javascripts/editor/participantsupport.js.erb +++ b/app/assets/javascripts/editor/participantsupport.js.erb @@ -142,7 +142,7 @@ CodeOceanEditorRequestForComments = { var createRequestForComments = function (submission) { $.ajax({ method: 'POST', - url: '/request_for_comments', + url: Routes.request_for_comments_path(), data: { request_for_comment: { exercise_id: exercise_id, diff --git a/app/assets/javascripts/exercises.js.erb b/app/assets/javascripts/exercises.js.erb index f669951e..5ffea543 100644 --- a/app/assets/javascripts/exercises.js.erb +++ b/app/assets/javascripts/exercises.js.erb @@ -1,6 +1,6 @@ $(document).on('turbolinks:load', function () { // ruby part adds the relative_url_root, if it is set. - var ACE_FILES_PATH = '<%= (defined? Rails.application.config.relative_url_root) && Rails.application.config.relative_url_root != nil && Rails.application.config.relative_url_root != "" ? Rails.application.config.relative_url_root : "" %>' + '/assets/ace/'; + var ACE_FILES_PATH = '<%= "#{Rails.application.config.relative_url_root.chomp('/')}/assets/ace/" %>'; var THEME = 'ace/theme/textmate'; var TAB_KEY_CODE = 9; @@ -351,7 +351,7 @@ $(document).on('turbolinks:load', function () { return $.ajax({ type: 'POST', - url: '/exercises/' + exerciseID + '/export_external_check', + url: Routes.export_external_check_exercise_path(exerciseID), dataType: 'json', success: function (response) { $messageDiv.html(response.message); @@ -370,7 +370,7 @@ $(document).on('turbolinks:load', function () { return $.ajax({ type: 'POST', - url: '/exercises/' + exerciseID + '/export_external_confirm', + url: Routes.export_external_confirm_exercise_path(exerciseID), dataType: 'json', success: function (response) { $messageDiv.html(response.message) @@ -427,9 +427,8 @@ $(document).on('turbolinks:load', function () { }; var updateFileTemplates = function (fileType) { - var rel_url_root = '<%= (defined? Rails.application.config.relative_url_root) && Rails.application.config.relative_url_root != nil && Rails.application.config.relative_url_root != "" ? Rails.application.config.relative_url_root : "" %>'; var jqxhr = $.ajax({ - url: rel_url_root + '/file_templates/by_file_type/' + fileType + '.json', + url: Routes.by_file_type_file_templates_path(fileType), dataType: 'json' }); jqxhr.done(function (response) { diff --git a/app/assets/javascripts/markdown_ace_editor.js b/app/assets/javascripts/markdown_ace_editor.js.erb similarity index 82% rename from app/assets/javascripts/markdown_ace_editor.js rename to app/assets/javascripts/markdown_ace_editor.js.erb index bd9845f3..9ac28df1 100644 --- a/app/assets/javascripts/markdown_ace_editor.js +++ b/app/assets/javascripts/markdown_ace_editor.js.erb @@ -1,5 +1,5 @@ (function() { - var ACE_FILES_PATH = '/assets/ace/'; + var ACE_FILES_PATH = '<%= "#{Rails.application.config.relative_url_root.chomp('/')}/assets/ace/" %>'; window.MarkdownEditor = function(selector) { ace.config.set('modePath', ACE_FILES_PATH); diff --git a/app/assets/javascripts/submission_statistics.js b/app/assets/javascripts/submission_statistics.js.erb similarity index 97% rename from app/assets/javascripts/submission_statistics.js rename to app/assets/javascripts/submission_statistics.js.erb index 71b9427f..0fab3777 100644 --- a/app/assets/javascripts/submission_statistics.js +++ b/app/assets/javascripts/submission_statistics.js.erb @@ -1,6 +1,6 @@ $(document).on('turbolinks:load', function() { - var ACE_FILES_PATH = '/assets/ace/'; + var ACE_FILES_PATH = '<%= "#{Rails.application.config.relative_url_root.chomp('/')}/assets/ace/" %>'; var THEME = 'ace/theme/textmate'; var currentSubmission = 0; diff --git a/app/helpers/statistics_helper.rb b/app/helpers/statistics_helper.rb index a9e669b7..4472e538 100644 --- a/app/helpers/statistics_helper.rb +++ b/app/helpers/statistics_helper.rb @@ -44,7 +44,7 @@ module StatisticsHelper data: ExternalUser.joins(:submissions) .where(['submissions.created_at >= ?', DateTime.now - 5.minutes]) .distinct('external_users.id').count, - url: 'statistics/graphs', + url: statistics_graphs_path, }, ] end diff --git a/app/views/request_for_comments/show.html.slim b/app/views/request_for_comments/show.html.slim index 183ddeda..c25d8ab6 100644 --- a/app/views/request_for_comments/show.html.slim +++ b/app/views/request_for_comments/show.html.slim @@ -148,7 +148,7 @@ javascript: }); // set file paths for ace - var ACE_FILES_PATH = '/assets/ace/'; + var ACE_FILES_PATH = "#{Rails.application.config.relative_url_root.chomp('/')}/assets/ace/"; _.each(['modePath', 'themePath', 'workerPath'], function(attribute) { ace.config.set(attribute, ACE_FILES_PATH); }); @@ -231,7 +231,7 @@ javascript: var jqrequest = $.ajax({ dataType: 'json', method: 'GET', - url: '/comments', + url: Routes.comments_path(), data: { file_id: fileid } @@ -254,7 +254,7 @@ javascript: function deleteComment(commentId, editor, file_id, callback) { var jqxhr = $.ajax({ type: 'DELETE', - url: "/comments/" + commentId + url: Routes.comments_path(commentId) }); jqxhr.done(function () { setAnnotations(editor, file_id); @@ -266,7 +266,7 @@ javascript: function updateComment(commentId, text, editor, file_id, callback) { var jqxhr = $.ajax({ type: 'PATCH', - url: "/comments/" + commentId, + url: Routes.comments_path(commentId), data: { comment: { text: text @@ -293,7 +293,7 @@ javascript: }, dataType: 'json', method: 'POST', - url: "/comments" + url: Routes.comments_path() }); jqxhr.done(function(){ setAnnotations(editor, file_id); @@ -312,7 +312,7 @@ javascript: }, dataType: 'json', method: 'POST', - url: "/subscriptions.json" + url: Routes.subscriptions_path({format: 'json'}) }); jqxhr.done(function(subscription) { checkbox.data('subscription', subscription.id); @@ -329,7 +329,7 @@ javascript: checkbox.attr("disabled", true); var subscriptionId = checkbox.data('subscription'); var jqxhr = $.ajax({ - url: '/subscriptions/' + subscriptionId + '/unsubscribe.json' + url: Routes.unsubscribe_subscription_path(subscriptionId, {format: 'json'}) }); jqxhr.done(function(response) { checkbox.prop('checked', false); diff --git a/config.ru b/config.ru index 6dc83218..4a160382 100644 --- a/config.ru +++ b/config.ru @@ -4,5 +4,7 @@ require_relative 'config/environment' -run Rails.application -Rails.application.load_server +map Rails.application.config.relative_url_root do + run Rails.application + Rails.application.load_server +end diff --git a/config/application.rb b/config/application.rb index 78b2bee6..91c8782d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -40,7 +40,9 @@ module CodeOcean config.autoload_paths += extra_paths config.eager_load_paths += extra_paths - config.action_cable.mount_path = '/cable' + config.relative_url_root = ENV.fetch('RAILS_RELATIVE_URL_ROOT', '/').to_s + + config.action_cable.mount_path = "#{ENV.fetch('RAILS_RELATIVE_URL_ROOT', '')}/cable" config.telegraf.tags = {application: 'codeocean'} diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 41a7e30a..2ad25a24 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -100,7 +100,4 @@ Rails.application.configure do # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false - - # Run on subfolder in production environment. - # config.relative_url_root = '/co-staging' end diff --git a/config/initializers/session_store.rb b/config/initializers/session_store.rb index 43ed5fc8..234f4f22 100644 --- a/config/initializers/session_store.rb +++ b/config/initializers/session_store.rb @@ -2,4 +2,7 @@ # Be sure to restart your server when you modify this file. -Rails.application.config.session_store :cookie_store, key: '_code_ocean_session', expire_after: 1.month +Rails.application.config.session_store :cookie_store, + key: '_code_ocean_session', + expire_after: 1.month, + path: Rails.application.config.relative_url_root diff --git a/config/webpack/environment.js b/config/webpack/environment.js index c690ddab..94f7ba80 100644 --- a/config/webpack/environment.js +++ b/config/webpack/environment.js @@ -4,8 +4,8 @@ Info for this file can be found github.com/rails/webpacker/blob/master/docs/webpack.md */ -const { environment } = require('@rails/webpacker') -const { merge } = require('webpack-merge') +const {environment} = require('@rails/webpacker') +const {merge} = require('webpack-merge') const webpack = require('webpack') const erb = require('./loaders/erb') @@ -25,6 +25,14 @@ environment.plugins.prepend('Provide', new webpack.ProvidePlugin({ }) ) +// This setting will change the absolute path used to refer +// external files (images, fonts, ...) in the generated assets +const relative_url_root = process.env.RAILS_RELATIVE_URL_ROOT || ''; +const public_output_path = environment.config.output.publicPath; +environment.loaders.get('file') + .use.find(item => item.loader === 'file-loader') + .options.publicPath = relative_url_root + public_output_path; + environment.loaders.append('erb', erb) const envConfig = module.exports = environment