Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(files) move serving files into a plugin #130

Merged
merged 7 commits into from
Apr 7, 2024

Conversation

Tieske
Copy link
Contributor

@Tieske Tieske commented Feb 28, 2023

This builds on top of #127

  • moves file handling into a plugin
  • enforces HEAD requests to have no body

this reduces the 'handler' to primary task of running plugins

@Tieske Tieske force-pushed the files branch 2 times, most recently from 3a3d433 to 7c6391c Compare March 4, 2023 12:55
@Tieske Tieske force-pushed the files branch 2 times, most recently from 853998e to b758301 Compare May 5, 2023 12:17
server:start(function(req)
local data = req:post()
server:start(function(req, resp)
local stop = false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this variable, you can simply return the values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I originally had. I introduced the stop variable for readability. It makes it clear what the returned result is supposed to be. If results are directly returned one has to mentally parse every return statement, whereas this reads easy as the intent is clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know if you want me to change this anyway. No problem.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree that it's making it more readable, but I'm fine to keep it there as it's just a code example :)


local path = req:path()
if req:method() ~= "POST" or path ~= "/index.html" then
return stop
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return stop
return false

end
stop = not not resp:writeFile("./example/root" .. path)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stop = not not resp:writeFile("./example/root" .. path)
return not not resp:writeFile("./example/root" .. path)

end
stop = not not resp:writeFile("./example/root" .. path)
return stop
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return stop

@EvandroLG
Copy link
Owner

I think we should add more unit tests here. We have entire new modules without any tests.

@Tieske Tieske force-pushed the files branch 4 times, most recently from ee4642c to 1ac5fdb Compare December 27, 2023 23:41
@Tieske
Copy link
Contributor Author

Tieske commented Dec 27, 2023

Finally had some time to write the tests for the 2 new files; test for the json lib, and the files-plugin.

@Tieske
Copy link
Contributor Author

Tieske commented Feb 29, 2024

@EvandroLG I rebased the PR, afetr some of the commits were merged via other PR's. What's left is hard to simplify due to conflicts that would arise.

Please have another look.

@Tieske
Copy link
Contributor Author

Tieske commented Mar 27, 2024

@EvandroLG anything I can do to get this moving?

@EvandroLG
Copy link
Owner

Super nice stuff, @Tieske! Thanks! :)
Can you update the Native Plugins section with an example of how to use the File plugin?
We also need to update the rockspec and add the new plugin. Can you please do that?

@Tieske
Copy link
Contributor Author

Tieske commented Apr 2, 2024

Added the requested docs. Accidentally also pushed the router plugin, but removed that commit again. The files plugin was already in the rockspec.

So all changed as requested.

handler.plugins = plugins or {}

if location then
handler.plugins[#handler.plugins+1] = Files:new {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if I agree that a plugin should be called into the handler logic. In my view, the handler logic should work just with stuff that is part of the Pegasus core. I'm curious to see how you envision it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,62 @@
-- Wrapper library that supports multiple JSON libraries
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more like a plugin, right? Why do you think it should be served as part of the Pegasus root?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false
end
print"running TLS"
--print"running TLS"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
--print"running TLS"

server:start(function(req)
local data = req:post()
server:start(function(req, resp)
local stop = false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree that it's making it more readable, but I'm fine to keep it there as it's just a code example :)

@EvandroLG
Copy link
Owner

Looks great, @Tieske! Thanks!

@EvandroLG EvandroLG merged commit 37cb2db into EvandroLG:master Apr 7, 2024
7 checks passed
@Tieske Tieske deleted the files branch April 8, 2024 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants