diff --git a/app/controllers/api/v0/health_checks_controller.rb b/app/controllers/api/v0/health_checks_controller.rb new file mode 100644 index 000000000..5b1cb4826 --- /dev/null +++ b/app/controllers/api/v0/health_checks_controller.rb @@ -0,0 +1,37 @@ +module Api + module V0 + class HealthChecksController < ApiController + before_action :authenticate_with_token + + def app + render json: { message: "App is up!" }, status: :ok + end + + def search + if Search::Client.ping + render json: { message: "Search ping succeeded!" }, status: :ok + else + render json: { message: "Search ping failed!" }, status: :internal_server_error + end + end + + def database + if ActiveRecord::Base.connected? + render json: { message: "Database connected" }, status: :ok + else + render json: { message: "Database NOT connected!" }, status: :internal_server_error + end + end + + private + + def authenticate_with_token + key = request.headers["health-check-token"] + + return if key == SiteConfig.health_check_token + + error_unauthorized + end + end + end +end diff --git a/app/models/site_config.rb b/app/models/site_config.rb index 95878c7c3..879da4719 100644 --- a/app/models/site_config.rb +++ b/app/models/site_config.rb @@ -116,4 +116,7 @@ class SiteConfig < RailsSettings::Base # Shop field :shop_url, type: :string, default: "https://shop.dev.to" + + # Special API tokens + field :health_check_token, type: :string, default: "secret" end diff --git a/app/views/internal/configs/show.html.erb b/app/views/internal/configs/show.html.erb index 227f5cff0..cd98a7761 100644 --- a/app/views/internal/configs/show.html.erb +++ b/app/views/internal/configs/show.html.erb @@ -293,7 +293,6 @@