leafo / lapis

A web framework for Lua and OpenResty written in MoonScript
http://leafo.net/lapis/
MIT License
3.12k stars 247 forks source link

Loading static files like css with cqueues server #780

Closed kmafeni04 closed 7 months ago

kmafeni04 commented 8 months ago

How would I go about doing this as I cannot find any information

JohnEmhoff commented 8 months ago

Based on my limited lapis experience, I'd avoid the cqueues backend. I similarly didn't see any information about handling static files so I implemented something myself. Then I noticed that making http requests would always fail with what looked like an internal event loop error. And on top of all that, after serving a few hundred requests I noticed response times would climb up to 400-500ms. I never figured out what it was; at that point I just switched to nginx and all these problems went away.

leafo commented 7 months ago

The cqueues server only supports handling requests through the application module you provide. There are no built-in webserver "fundamentals" for things like efficient static asset hosting. In those cases, I would still recommend using something like Nginx with a reverse proxy to handle static assets, as it will help prevent blocking the app server for basic I/O that can be offloaded to a proxying server.

If performance is no issue and load is minimal (say an internal tool only you use), then you can load the file, set the content type, and return it as a string within your lapis application handler. (Just want to iterate again, there is no async I/O support here so it will hurt throughput)

(As an aside, I only personally use the cqueues server for internal tools that aren't publicly facing. I would say it's not as well tested and robust as the openresty mode. If you have a publicly facing server I would recommend setting up openresty)