Sha256: d1bb6c811e4c6b4a14d7af1a9882ee9700d3bcedb9c6af25595669736f713409

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

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
    if params[:id]
      redirect "/services/#{params[:id]}"
    elsif params[:name]
      srv = find_by_id_or_name
      format_response(srv, verbose)
    else
      services = Service.all
      format_response(services, verbose)
    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
  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)
      srv.update!(yaml.to_o)
    end
    format_response(srv)
  end

  # curl -X DELETE localhost:4567/services/1
  delete '/services/:id' do
    srv = find_by_id_or_name
    MkitJob.publish(topic: :delete_service, service_id: srv.id)
    format_response(srv)
  end

  # curl -X POST localhost:4567/services  -F "file=@mkit/samples/mkit.yml"
  post '/services' do
    srv = "no file"
    if params[:file]
      tempfile = params[:file][:tempfile]
      yaml = YAML.load(tempfile.read)
      srv = Service.create(yaml.to_o)
    end
    format_response(srv)
  end

  #
  # operations
  #
  # curl -X PUT localhost:4567/services/1/start
  put '/services/:id/start' do
    srv = find_by_id_or_name
    MkitJob.publish(topic: :start_service, service_id: srv.id)
    format_response(srv)
  end

  # 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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mkit-0.4.1 lib/mkit/app/controllers/services_controller.rb
mkit-0.4.0 lib/mkit/app/controllers/services_controller.rb