ruby_impl.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. module App
  2. Ruby = lambda do |env|
  3. content_type, body = case env['PATH_INFO']
  4. when '/plaintext'
  5. ['text/plain', "Hello, World!"]
  6. when '/json'
  7. ['application/json', {:message => "Hello, World!"}.to_json]
  8. when '/db'
  9. id = Random.rand(10000) + 1
  10. query = "SELECT * FROM World WHERE id = " + id.to_s
  11. client = Mysql2::Client.new(DB_CONFIG)
  12. results = begin
  13. client.query(query)
  14. ensure
  15. client.close
  16. end
  17. ['application/json', results.first.to_json]
  18. when '/queries'
  19. query_string = Rack::Utils.parse_query(env['QUERY_STRING'])
  20. queries = query_string['queries'].to_i
  21. queries = 1 if queries < 1
  22. queries = 500 if queries > 500
  23. client = Mysql2::Client.new(DB_CONFIG)
  24. results = begin
  25. (1..queries).map do
  26. id = Random.rand(10000) + 1
  27. client.query("SELECT * FROM World WHERE id = " + id.to_s).first
  28. end
  29. ensure
  30. client.close
  31. end
  32. ['application/json', results.to_json]
  33. when '/updates'
  34. query_string = Rack::Utils.parse_query(env['QUERY_STRING'])
  35. queries = query_string['queries'].to_i
  36. queries = 1 if queries < 1
  37. queries = 500 if queries > 500
  38. client = Mysql2::Client.new(DB_CONFIG)
  39. results = begin
  40. results = (1..queries).map do
  41. id = Random.rand(10000) + 1
  42. num = Random.rand(10000) + 1
  43. client.query("UPDATE World SET randomNumber = " + num.to_s + " WHERE id = " + id.to_s)
  44. client.query("SELECT * FROM World WHERE id = " + id.to_s).first
  45. end
  46. results
  47. ensure
  48. client.close
  49. end
  50. ['application/json', results.to_json]
  51. end
  52. [200, { 'Content-Type' => content_type, 'Date' => Time.now.httpdate, 'Server' => 'WebServer' }, [body]]
  53. end
  54. end