From db86b3ca991a8f0fb0ddda56e6962f70d8d2a46d Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Tue, 1 Aug 2023 17:45:45 +0100 Subject: [PATCH] Add endpoint for solution image generator (#5747) * Add endpoint for solution image generator * Allow access from docker --------- Co-authored-by: Erik Schierboom --- .../spi/solution_image_data_controller.rb | 26 +++++++++++++++++++ config/environments/development.rb | 1 + config/routes/spi.rb | 1 + 3 files changed, 28 insertions(+) create mode 100644 app/controllers/spi/solution_image_data_controller.rb diff --git a/app/controllers/spi/solution_image_data_controller.rb b/app/controllers/spi/solution_image_data_controller.rb new file mode 100644 index 0000000000..104a9da893 --- /dev/null +++ b/app/controllers/spi/solution_image_data_controller.rb @@ -0,0 +1,26 @@ +module SPI + class SolutionImageDataController < BaseController + def show + solution = Solution.for!( + params[:user_handle], + params[:track_slug], + params[:exercise_slug] + ) + + exercise = solution.exercise + track = exercise.track + file = solution.latest_published_iteration_submission.files.first + snippet = solution.snippet.presence || file.content[0, 10] + render json: { + solution: { + snippet:, + extension: File.extname(file.filename), + language: track.highlightjs_language, + track_icon_url: track.icon_url, + exercise_icon_url: exercise.icon_url, + user_avatar_url: solution.user.avatar_url + } + } + end + end +end diff --git a/config/environments/development.rb b/config/environments/development.rb index 3df28792f0..a1d979ad71 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -87,6 +87,7 @@ config.hosts << "website" if ENV['EXERCISM_DOCKER'] config.hosts << /.*.ngrok.io/ config.hosts << /.*.ngrok-free.app/ + config.hosts << "host.docker.internal" end Rails.application.routes.default_url_options = { diff --git a/config/routes/spi.rb b/config/routes/spi.rb index 4f52684cef..34d8b9a2b2 100644 --- a/config/routes/spi.rb +++ b/config/routes/spi.rb @@ -4,4 +4,5 @@ namespace :spi do resources :tooling_jobs, only: :update resources :chatgpt_responses, only: :create + get "solution_image_data/:track_slug/:exercise_slug/:user_handle" => "solution_image_data#show" end