hello_world.rb 2.6 KB

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