|
| 1 | +require 'launchdarkly-server-sdk' |
| 2 | +require 'json' |
| 3 | +require 'logger' |
| 4 | +require 'net/http' |
| 5 | +require 'sinatra' |
| 6 | + |
| 7 | +require './client_entity.rb' |
| 8 | + |
| 9 | +configure :development do |
| 10 | + disable :show_exceptions |
| 11 | +end |
| 12 | + |
| 13 | +$log = Logger.new(STDOUT) |
| 14 | +$log.formatter = proc {|severity, datetime, progname, msg| |
| 15 | + "[GLOBAL] #{datetime.strftime('%Y-%m-%d %H:%M:%S.%3N')} #{severity} #{progname} #{msg}\n" |
| 16 | +} |
| 17 | + |
| 18 | +set :port, 9000 |
| 19 | +set :logging, false |
| 20 | + |
| 21 | +clients = {} |
| 22 | +clientCounter = 0 |
| 23 | + |
| 24 | +get '/' do |
| 25 | + { |
| 26 | + capabilities: [ |
| 27 | + 'server-side', |
| 28 | + 'all-flags-with-reasons', |
| 29 | + 'all-flags-client-side-only', |
| 30 | + 'all-flags-details-only-for-tracked-flags', |
| 31 | + ] |
| 32 | + }.to_json |
| 33 | +end |
| 34 | + |
| 35 | +delete '/' do |
| 36 | + $log.info("Test service has told us to exit") |
| 37 | + Thread.new { sleep 1; exit } |
| 38 | + return 204 |
| 39 | +end |
| 40 | + |
| 41 | +post '/' do |
| 42 | + opts = JSON.parse(request.body.read, :symbolize_names => true) |
| 43 | + tag = "[#{opts[:tag]}]" |
| 44 | + |
| 45 | + clientCounter += 1 |
| 46 | + clientId = clientCounter.to_s |
| 47 | + |
| 48 | + log = Logger.new(STDOUT) |
| 49 | + log.formatter = proc {|severity, datetime, progname, msg| |
| 50 | + "#{tag} #{datetime.strftime('%Y-%m-%d %H:%M:%S.%3N')} #{severity} #{progname} #{msg}\n" |
| 51 | + } |
| 52 | + |
| 53 | + log.info("Starting client") |
| 54 | + log.debug("Parameters: #{opts}") |
| 55 | + |
| 56 | + client = ClientEntity.new(log, opts[:configuration]) |
| 57 | + |
| 58 | + if !client.initialized? && opts[:configuration][:initCanFail] == false |
| 59 | + client.close() |
| 60 | + return [500, nil, "Failed to initialize"] |
| 61 | + end |
| 62 | + |
| 63 | + clientResourceUrl = "/clients/#{clientId}" |
| 64 | + clients[clientId] = client |
| 65 | + return [201, {'Location' => clientResourceUrl}, nil] |
| 66 | +end |
| 67 | + |
| 68 | +post '/clients/:id' do |clientId| |
| 69 | + client = clients[clientId] |
| 70 | + return 404 if client.nil? |
| 71 | + |
| 72 | + params = JSON.parse(request.body.read, :symbolize_names => true) |
| 73 | + |
| 74 | + client.log.info("Processing request for client #{clientId}") |
| 75 | + client.log.debug("Parameters: #{params}") |
| 76 | + |
| 77 | + case params[:command] |
| 78 | + when "evaluate" |
| 79 | + response = client.evaluate(params[:evaluate]) |
| 80 | + return [200, nil, response.to_json] |
| 81 | + when "evaluateAll" |
| 82 | + response = {:state => client.evaluate_all(params[:evaluateAll])} |
| 83 | + return [200, nil, response.to_json] |
| 84 | + when "customEvent" |
| 85 | + client.track(params[:customEvent]) |
| 86 | + return 201 |
| 87 | + when "identifyEvent" |
| 88 | + client.identify(params[:identifyEvent]) |
| 89 | + return 201 |
| 90 | + when "aliasEvent" |
| 91 | + client.alias(params[:aliasEvent]) |
| 92 | + return 201 |
| 93 | + when "flushEvents" |
| 94 | + client.flush_events |
| 95 | + return 201 |
| 96 | + end |
| 97 | + |
| 98 | + return [400, nil, {:error => "Unknown command requested"}.to_json] |
| 99 | +end |
| 100 | + |
| 101 | +delete '/clients/:id' do |clientId| |
| 102 | + client = clients[clientId] |
| 103 | + return 404 if client.nil? |
| 104 | + clients.delete(clientId) |
| 105 | + client.close |
| 106 | + |
| 107 | + return 204 |
| 108 | +end |
| 109 | + |
| 110 | +error do |
| 111 | + env['sinatra.error'].message |
| 112 | +end |
0 commit comments