lib/mkit/app/controllers/services_controller.rb in mkit-0.4.1 vs lib/mkit/app/controllers/services_controller.rb in mkit-0.4.2
- old
+ new
@@ -1,18 +1,17 @@
+# frozen_string_literal: true
+
require 'mkit/app/model/service'
require 'mkit/app/helpers/services_helper'
class ServicesController < MKIt::Server
helpers MKIt::ServicesHelper
# curl localhost:4567/services
get '/services' do
- if params[:verbose]
- verbose = params[:verbose] == 'true'
- else
- verbose = false
- end
+ verbose = params[:verbose] == 'true'
+
if params[:id]
redirect "/services/#{params[:id]}"
elsif params[:name]
srv = find_by_id_or_name
format_response(srv, verbose)
@@ -22,24 +21,24 @@
end
end
get '/services/:id' do
srv = find_by_id_or_name
- if request.env['CONTENT_TYPE'] == 'application/json'
- resp = srv.to_json
- else
- resp = format_response(srv)
- end
+ resp = if request.env['CONTENT_TYPE'] == 'application/json'
+ srv.to_json
+ else
+ format_response(srv)
+ end
resp
end
# curl -X PUT localhost:4567/services/1 -F "file=@mkit/samples/mkit.yml"
put '/services/:id' do
srv = find_by_id_or_name
if params[:file]
tempfile = params[:file][:tempfile]
- yaml = YAML.load(tempfile.read)
+ yaml = YAML.safe_load(tempfile.read)
srv.update!(yaml.to_o)
end
format_response(srv)
end
@@ -50,14 +49,14 @@
format_response(srv)
end
# curl -X POST localhost:4567/services -F "file=@mkit/samples/mkit.yml"
post '/services' do
- srv = "no file"
+ srv = 'no file'
if params[:file]
tempfile = params[:file][:tempfile]
- yaml = YAML.load(tempfile.read)
+ yaml = YAML.safe_load(tempfile.read)
srv = Service.create(yaml.to_o)
end
format_response(srv)
end
@@ -73,8 +72,15 @@
# curl -X PUT localhost:4567/services/1/stop
put '/services/:id/stop' do
srv = find_by_id_or_name
MkitJob.publish(topic: :stop_service, service_id: srv.id)
+ format_response(srv)
+ end
+
+ put '/services/:id/restart' do
+ srv = find_by_id_or_name
+ MkitJob.publish(topic: :stop_service, service_id: srv.id)
+ MkitJob.publish(topic: :start_service, service_id: srv.id)
format_response(srv)
end
end