Skip to content

Commit

Permalink
Create ping endpoint in cmd-tlm and scripts api
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Dec 30, 2023
1 parent dd13990 commit e1f2069
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
23 changes: 14 additions & 9 deletions openc3-cosmos-cmd-tlm-api/app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
require 'openc3/utilities/open_telemetry'

class ApiController < ApplicationController
def ping
render plain: 'OK'
end

def api
OpenC3.in_span('jsonrpc_api') do |span|
req = Rack::Request.new(request.env)

if request.post?
request_headers = Hash[*request.env.select {|k,v| k.start_with? 'HTTP_'}.sort.flatten]
request_headers = Hash[*request.env.select {|k,_v| k.start_with? 'HTTP_'}.sort.flatten]
request_data = req.body.read
span.add_attributes({ "request_data" => request_data }) if span and request_data.length <= 1000
status = nil
Expand All @@ -40,7 +43,7 @@ def api
OpenC3::Logger.info("API data: #{request_data}", scope: nil, user: username())
OpenC3::Logger.debug("API headers: #{request_headers}", scope: nil, user: username())
status, content_type, body = handle_post(request_data, request_headers)
rescue OpenC3::AuthError => error
rescue OpenC3::AuthError => e
id = 1
begin
parsed = JSON.parse(request_data, :allow_nan => true)
Expand All @@ -50,7 +53,7 @@ def api
end
error_code = OpenC3::JsonRpcError::ErrorCode::AUTH_ERROR
response = OpenC3::JsonRpcErrorResponse.new(
OpenC3::JsonRpcError.new(error_code, error.message, error), id
OpenC3::JsonRpcError.new(error_code, e.message, e), id
)
status = 401
content_type = "application/json-rpc"
Expand Down Expand Up @@ -94,15 +97,17 @@ def handle_post(request_data, request_headers)
# see http://www.jsonrpc.org/historical/json-rpc-over-http.html#errors
if error_code
case error_code
when OpenC3::JsonRpcError::ErrorCode::PARSE_ERROR then status = 500 # Internal server error
when OpenC3::JsonRpcError::ErrorCode::INVALID_REQUEST then status = 400 # Bad request
when OpenC3::JsonRpcError::ErrorCode::AUTH_ERROR then status = 401 # Auth
when OpenC3::JsonRpcError::ErrorCode::FORBIDDEN_ERROR then status = 403 # Forbidden
when OpenC3::JsonRpcError::ErrorCode::METHOD_NOT_FOUND then status = 404 # Not found
when OpenC3::JsonRpcError::ErrorCode::INVALID_PARAMS then status = 500 # Internal server error
when OpenC3::JsonRpcError::ErrorCode::INTERNAL_ERROR then status = 500 # Internal server error
when OpenC3::JsonRpcError::ErrorCode::AUTH_ERROR then status = 401
when OpenC3::JsonRpcError::ErrorCode::FORBIDDEN_ERROR then status = 403
when OpenC3::JsonRpcError::ErrorCode::HAZARDOUS_ERROR then status = 409 # Server conflict
else status = 500 # Internal server error
else
# Also includes the following errors:
# OpenC3::JsonRpcError::ErrorCode::PARSE_ERROR
# OpenC3::JsonRpcError::ErrorCode::INVALID_PARAMS
# OpenC3::JsonRpcError::ErrorCode::INTERNAL_ERROR
status = 500 # Internal server error
end
# Note we don't log an error here because it's logged in JsonDRb::process_request
else
Expand Down
1 change: 1 addition & 0 deletions openc3-cosmos-cmd-tlm-api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
delete '/secrets/:key', to: 'secrets#destroy', key: /[^\/]+/

post "/api" => "api#api"
get "/ping" => "api#ping"

get "/auth/token-exists" => "auth#token_exists"
post "/auth/verify" => "auth#verify"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class ScriptsController < ApplicationController
SUITE_REGEX = /^\s*class\s+\w+\s+<\s+(Cosmos::|OpenC3::)?(Suite|TestSuite)/
PYTHON_SUITE_REGEX = /^\s*class\s+\w+\s*\(\s*(Suite|TestSuite)\s*\)/

def ping
render plain: 'OK'
end

def index
return unless authorization('script_view')
render :json => Script.all(params[:scope])
Expand All @@ -48,7 +52,6 @@ def body

file = Script.body(params[:scope], params[:name])
if file
success = true
locked = Script.locked?(params[:scope], params[:name])
unless locked
Script.lock(params[:scope], params[:name], username())
Expand Down
1 change: 1 addition & 0 deletions openc3-cosmos-script-runner-api/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
scope "script-api" do
get "/ping" => "scripts#ping"
get "/scripts" => "scripts#index"
delete "/scripts/temp_files" => "scripts#delete_temp"
get "/scripts/*name" => "scripts#body", format: false, defaults: { format: 'html' }
Expand Down

0 comments on commit e1f2069

Please sign in to comment.