| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | module Dev |
| 4 | class PanelController < ApplicationController |
| 5 | CACHE_KEY = "dev:smbcloud_environment" |
| 6 | VALID_ENVIRONMENTS = %w[dev production].freeze |
| 7 | |
| 8 | skip_before_action :require_sign_in!, raise: false |
| 9 | skip_before_action :verify_authenticity_token |
| 10 | |
| 11 | # GET /dev/environment |
| 12 | def show |
| 13 | render json: { environment: current_environment } |
| 14 | end |
| 15 | |
| 16 | # PUT /dev/environment |
| 17 | def update |
| 18 | env = params[:environment].to_s.strip.downcase |
| 19 | |
| 20 | unless VALID_ENVIRONMENTS.include?(env) |
| 21 | return render json: { error: "Invalid environment. Must be one of: #{VALID_ENVIRONMENTS.join(', ')}" }, |
| 22 | status: :unprocessable_entity |
| 23 | end |
| 24 | |
| 25 | Rails.cache.write(CACHE_KEY, env, expires_in: 24.hours) |
| 26 | render json: { environment: env } |
| 27 | end |
| 28 | |
| 29 | private |
| 30 | |
| 31 | def current_environment |
| 32 | Rails.cache.read(CACHE_KEY) || ENV.fetch("SIGITSI_SMBCLOUD_ENVIRONMENT", "dev") |
| 33 | end |
| 34 | end |
| 35 | end |