kernelsauce / turbo

Turbo is a framework built for LuaJIT 2 to simplify the task of building fast and scalable network applications. It uses a event-driven, non-blocking, no thread design to deliver excellent performance and minimal footprint to high-load applications while also providing excellent support for embedded uses.
http://turbo.readthedocs.io/
Apache License 2.0
528 stars 84 forks source link

How to obtain absolute URI within RequestHandler? #195

Closed jufickel closed 9 years ago

jufickel commented 9 years ago

Hi,

I would like to set the Location header in the response of a POST request. According to the HTTP spec this header should contain an absolute URI to the newly created resource (or entity). Now I'm trying to obtain the absolute URI somehow within the RequestHandler of the POST request. Could you please inform me about how to achieve this?

Thank you!

kernelsauce commented 9 years ago

The RequestHandler will not know what the URL to the newly created resource is, you must program that yourself.

You can get the current request URI via self.request.uri though https://turbo.readthedocs.org/en/latest/httpserver.html#httprequest-class.

jufickel commented 9 years ago

I see. Thanks. What remains unclear to me is how to interleave turbo.web.Application and HTTPServer since I'm writing a REST application with the core web framework.

kernelsauce commented 9 years ago

Like this you mean? There are a lot of examples in the /examples directory of the project.

local turbo = require "turbo"

local ExampleHandler = class("ExampleHandler", turbo.web.RequestHandler)

function ExampleHandler:get()
    self:write("Hello World!")
end

local app = turbo.web.Application({
    {"^/$", ExampleHandler},
    -- Serve contents of directory.
    {"^/(.*)$", turbo.web.StaticFileHandler, "/var/www/"}
})

local srv = turbo.httpserver.HTTPServer(app)
srv:bind(8888)
srv:start(1) -- Adjust amount of processes to fork.

turbo.ioloop.instance():start()
jufickel commented 9 years ago

Yes that was what I was looking for. Now everything works as expected. Thank you very much!