hello_world.rb 2.7 KB

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