thailandwhe.blogg.se

Ruby rack app
Ruby rack app













ruby rack app

Ruby frameworks like Rails and Sinatra are built on top of the Rack interface. If you want to follow along, here's the code we ended up with. We'll be continuing with the code we wrote last time. Using those, we were able to build an HTTP response to send back to the browser, before closing the connection to wait for a new request to come in.ġ # http_server.rb 2 require 'socket' 3 require 'rack' 4 require 'rack/lobster' 5 6app = Rack::Lobster.newħ server = TCPServer.new 5678 8 9 #1 10 while session = server.acceptġ3 14 #2 15 method, full_path = request.split( ' ' )ġ7 18 #3 19 status, headers, body = app.call( \r\n" 29 end 30 session.print "\r\n" 31 body.each do |part| 32 session.print part.The request method, the path and the query string were passed to the Rack app, which returned a triplet with a status, some response headers and the response body.When that happened, the request-line ( GET /?flip=left HTTP/1.1\r\n) was parsed to get the request method ( GET), the path ( /), and the query parameters ( flip=left).

ruby rack app

  • Our implementation opened a TCP server and waited for a request to come in.
  • Last time, we implemented just enough of a server to have it serve Rack::Lobster as an example application. When we're done, we'll have a web server that can serve Rails' famous fifteen minute blog that allows you to create, update and delete posts. This time, we'll take our home made server a bit further. Without having to write a lot of code, we were able to handle HTTP GET requests and serve a simple Rack application. In an earlier edition of Ruby Magic we implemented a 30-line HTTP server in Ruby.

    ruby rack app

    We publish a new article about once a month, so be sure to subscribe to our newsletter if you're into this sort of thing too. It's all about the process the end result isn't something you'd use in production, we learn about the internal workings of the Ruby language and its popular libraries. In the Ruby Magic series we love to take software apart to learn how it functions under the hood.















    Ruby rack app