hello_world.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # frozen_string_literal: true
  2. # Our Rack application to be executed by rackup
  3. class HelloWorld
  4. DEFAULT_HEADERS = {}.tap do |h|
  5. h['Server'] = SERVER_STRING if SERVER_STRING
  6. end.freeze
  7. def bounded_queries(env)
  8. params = Rack::Utils.parse_query(env['QUERY_STRING'])
  9. queries = params['queries'].to_i
  10. return QUERIES_MIN if queries < QUERIES_MIN
  11. return QUERIES_MAX if queries > QUERIES_MAX
  12. queries
  13. end
  14. # Return a random number between 1 and MAX_PK
  15. def rand1
  16. rand(MAX_PK).succ
  17. end
  18. WORLD_BY_ID = World.naked.where(:id=>:$id).prepare(:first, :world_by_id)
  19. WORLD_UPDATE = World.where(:id=>:$id).prepare(:update, :world_update, :randomnumber=>:$randomnumber)
  20. def db
  21. WORLD_BY_ID.(:id=>rand1)
  22. end
  23. def queries(env)
  24. DB.synchronize do
  25. Array.new(bounded_queries(env)) do
  26. WORLD_BY_ID.(:id=>rand1)
  27. end
  28. end
  29. end
  30. def fortunes
  31. fortunes = Fortune.all
  32. fortunes << Fortune.new(
  33. :id=>0,
  34. :message=>'Additional fortune added at request time.'
  35. )
  36. fortunes.sort_by!(&:message)
  37. html = <<~'HTML'
  38. <!DOCTYPE html>
  39. <html>
  40. <head>
  41. <title>Fortunes</title>
  42. </head>
  43. <body>
  44. <table>
  45. <tr>
  46. <th>id</th>
  47. <th>message</th>
  48. </tr>
  49. HTML
  50. fortunes.each do |fortune|
  51. html += <<~"HTML"
  52. <tr>
  53. <td>#{fortune.id}</td>
  54. <td>#{Rack::Utils.escape_html(fortune.message)}</td>
  55. </tr>
  56. HTML
  57. end
  58. html += <<~'HTML'
  59. </table>
  60. </body>
  61. </html>
  62. HTML
  63. end
  64. def updates(env)
  65. DB.synchronize do
  66. Array.new(bounded_queries(env)) do
  67. world = WORLD_BY_ID.(:id=>rand1)
  68. WORLD_UPDATE.(:id=>world[:id], :randomnumber=>(world[:randomnumber] = rand1))
  69. world
  70. end
  71. end
  72. end
  73. def call(env)
  74. content_type, *body =
  75. case env['PATH_INFO']
  76. when '/json'
  77. # Test type 1: JSON serialization
  78. ['application/json', JSON.fast_generate(:message=>'Hello, World!')]
  79. when '/db'
  80. # Test type 2: Single database query
  81. ['application/json', JSON.fast_generate(db)]
  82. when '/queries'
  83. # Test type 3: Multiple database queries
  84. ['application/json', JSON.fast_generate(queries(env))]
  85. when '/fortunes'
  86. # Test type 4: Fortunes
  87. ['text/html; charset=utf-8', fortunes]
  88. when '/updates'
  89. # Test type 5: Database updates
  90. ['application/json', JSON.fast_generate(updates(env))]
  91. when '/plaintext'
  92. # Test type 6: Plaintext
  93. ['text/plain', 'Hello, World!']
  94. end
  95. [
  96. 200,
  97. DEFAULT_HEADERS.merge(
  98. 'Content-Type'=>content_type,
  99. 'Date'=>Time.now.httpdate
  100. ),
  101. body
  102. ]
  103. end
  104. end