From 3a11ba2918795821600b7113d0758415718d263a Mon Sep 17 00:00:00 2001 From: gebele Date: Mon, 11 Jun 2018 12:46:06 +0200 Subject: combine gui with rest --- lib/api.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 lib/api.rb (limited to 'lib/api.rb') diff --git a/lib/api.rb b/lib/api.rb new file mode 100644 index 0000000..28e33df --- /dev/null +++ b/lib/api.rb @@ -0,0 +1,9 @@ +# route to swagger API file +get "/api/api.json" do + response['Content-Type'] = "application/json" + api_file = File.join("api", "api.json") + bad_request_error "API Documentation in Swagger JSON is not implemented." unless File.exists?(api_file) + api_hash = JSON.parse(File.read(api_file)) + api_hash["host"] = request.env['HTTP_HOST'] + return api_hash.to_json +end -- cgit v1.2.3 From 741701df8ff0861b3607a30e9aaf8b8a0c303cdf Mon Sep 17 00:00:00 2001 From: gebele Date: Thu, 13 Jun 2019 15:28:59 +0000 Subject: update with API --- lib/api.rb | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'lib/api.rb') diff --git a/lib/api.rb b/lib/api.rb index 28e33df..c3b27ce 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,9 +1,23 @@ -# route to swagger API file -get "/api/api.json" do - response['Content-Type'] = "application/json" +get "/api" do api_file = File.join("api", "api.json") - bad_request_error "API Documentation in Swagger JSON is not implemented." unless File.exists?(api_file) - api_hash = JSON.parse(File.read(api_file)) - api_hash["host"] = request.env['HTTP_HOST'] - return api_hash.to_json + halt 400, "API Documentation in Swagger JSON is not implemented." unless File.exists?(api_file) + case @accept + when "text/html" + response['Content-Type'] = "text/html" + index_file = File.join(ENV['HOME'],"swagger-ui/dist/index.html") + File.read(index_file) + when "application/json" + response['Content-Type'] = "application/json" + api_hash = JSON.parse(File.read(api_file)) + api_hash["host"] = request.env['HTTP_HOST'] + return api_hash.to_json + else + halt 400, "unknown MIME type '#{@accept}'" + end +end + +get "/api/api.json" do + response['Content-Type'] = "text/html" + index_file = File.join(ENV['HOME'],"swagger-ui/dist/index.html") + File.read(index_file) end -- cgit v1.2.3 From 3869670b3acfb4de982f45ea24af940eccac5474 Mon Sep 17 00:00:00 2001 From: gebele Date: Tue, 18 Jun 2019 12:49:35 +0000 Subject: update routes and mime type and generate server uri for API file --- lib/api.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'lib/api.rb') diff --git a/lib/api.rb b/lib/api.rb index c3b27ce..dffa9d7 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,23 +1,29 @@ get "/api" do api_file = File.join("api", "api.json") + `sed -i 's/SERVER_URI/#{request.env['HTTP_HOST']}/' #{api_file}` halt 400, "API Documentation in Swagger JSON is not implemented." unless File.exists?(api_file) case @accept when "text/html" response['Content-Type'] = "text/html" index_file = File.join(ENV['HOME'],"swagger-ui/dist/index.html") - File.read(index_file) + return File.read(index_file) when "application/json" - response['Content-Type'] = "application/json" - api_hash = JSON.parse(File.read(api_file)) - api_hash["host"] = request.env['HTTP_HOST'] - return api_hash.to_json + redirect("/api/api.json") else halt 400, "unknown MIME type '#{@accept}'" end end get "/api/api.json" do - response['Content-Type'] = "text/html" - index_file = File.join(ENV['HOME'],"swagger-ui/dist/index.html") - File.read(index_file) + api_file = File.join("api", "api.json") + `sed -i 's/SERVER_URI/#{request.env['HTTP_HOST']}/' #{api_file}` + case @accept + when "text/html" + response['Content-Type'] = "application/json" + return File.read(api_file) + when "application/json" + return File.read(api_file) + else + halt 400, "unknown MIME type '#{@accept}'" + end end -- cgit v1.2.3