Skip to content

Commit

Permalink
Add support for built in maintenance mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanzdosilovic committed Jan 14, 2020
1 parent 99939b8 commit 201221e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ def authorize_request
head :forbidden if safe_compare(Rails.application.secrets.authz_token, Rails.application.secrets.authz_header)
end

def check_maintenance
@@maintenance_message ||= ENV['MAINTENANCE_MESSAGE'].to_s.presence || "Judge0 API is currently in maintenance."
if Config::MAINTENANCE_MODE
render json: {
error: @@maintenance_message
}, status: :service_unavailable
end
end

def safe_compare(token, header)
token = token.to_s
header = header.to_s
Expand Down
1 change: 1 addition & 0 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SubmissionsController < ApplicationController
before_action :authorize_request, only: [:index, :destroy]
before_action :check_maintenance, only: [:create, :destroy]

def index
render_invalid_field_error and return if has_invalid_field
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Config
# For more info read:
# https://github.com/judge0/api/blob/master/judge0-api.conf.default

MAINTENANCE_MODE = ENV['MAINTENANCE_MODE'] == "true"
ENABLE_WAIT_RESULT = ENV['ENABLE_WAIT_RESULT'] != "false"
ENABLE_COMPILER_OPTIONS = ENV["ENABLE_COMPILER_OPTIONS"] != "false"
ALLOWED_LANGUAGES_FOR_COMPILER_OPTIONS = ENV["ALLOWED_LANGUAGES_FOR_COMPILER_OPTIONS"].to_s.strip.split
Expand Down Expand Up @@ -32,6 +33,7 @@ module Config

def self.config_info
@@default_confg ||= {
"maintenance_mode": MAINTENANCE_MODE,
"enable_wait_result": ENABLE_WAIT_RESULT,
"enable_compiler_options": ENABLE_COMPILER_OPTIONS,
"allowed_languages_for_compile_options": ALLOWED_LANGUAGES_FOR_COMPILER_OPTIONS,
Expand Down
10 changes: 10 additions & 0 deletions judge0-api.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ RAILS_ENV=
# Default: 5
RAILS_MAX_THREADS=

# Maintenance mode is a mode in which clients cannot
# create or delete submissions while maintenance is enabled.
# Default: false
MAINTENANCE_MODE=

# Set custom maintenance message that will be returned to clients
# who try to create or delete submisions.
# Default: Judge0 API is currently in maintenance.
MAINTENANCE_MESSAGE=

# Secret key base for production, if not set it will be randomly generated
# Default: randomly generated
SECRET_KEY_BASE=
Expand Down

0 comments on commit 201221e

Please sign in to comment.