-
Notifications
You must be signed in to change notification settings - Fork 31
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
Missing Servant.Utils.StaticFiles module in servant-server-0.11 #49
Comments
I am afraid that module was commented in the eta patch for the wai-server package |
Actually, the issue is not even So the best solution is to use the JVM's concept of resources. Patch |
Thanks for the info. I use it to serve static content like CSS and JS files. |
Thanks for the clarification @rahulmutt, It seems servant uses warp only for an example in servant-server and for testing in servant-client. It uses wai-app-static too, and it uses warp to get a static file server executable. We could use only the lib part of it, but we would have to implement ResponseFile in wai-servlet (tracked in this issue) |
@jneira @rahulmutt there is some utility in providing functionality of serving static files just for developer convenience even if it is not very fast and/or is part of a separate patch library. I will need to implement some simple workaround for now to convert my pet project. If there is no better solution by the time I finish this, it could serve as a temporary hack-patch? I just have seen the commented out stuff in servant-server patch. Very slowly learning hoops. |
@rpeszek Temporary patches that just implement the functionality are more than welcome. |
@jneira could you clarify your comment on the ResponseFile handling issue you have linked (jneira/wai-servlet#1). Is it about etagging only? Native type-checking tells me that the core of what is missing is (and that matches what you have indicated): Network.Wai.Application.Static (staticAppPieces, defaultFileServerSettings) I do not understand staticAppPieces etag logic well, it has some, but it seems to me that this function could fully encapsulate etagging when serving static file content. There is lots of OS specific stuff here so I do see reasoning to not convert it. |
After a more carefully reading of
Imo we could uncomment all the code but CommandLine and the Template Haskell part and i think we would try to make the package usable (taking in account the os/posix stuff) . If you want to serve files from the file system we'll have to implement |
Trying to install a minimal functional version of
Link to the cabal file: https://gist.github.com/jneira/99cef71f79ec61a1134872476eb8d237 Another options would be replace the calls for those packages (that are deeply linked with the os ans c libs) with direct java calls in wai-app-static, as a temporary workaround (?) |
Make usable the FileSystem way to handle files will need some work, replacing s.o. calls for java ones and implementing ResponseFile handling in wai-servlet |
@jneira thank you for your explanation. I decided to try to use plain responseLBS helper. A very poor man's version of static file serving that plugs nicely into servant Raw tape: -- LBS - Data.ByteString.Lazy
-- T - Text
data StaticSettings = StaticSettings FilePath
staticAppPieces :: StaticSettings -> [Text] -> WAI.Application
staticAppPieces (StaticSettings root) pieces req respond =
let filePath = joinPath $ map T.unpack (T.pack root : pieces)
contentType = M.mimeByExt M.defaultMimeMap "application/text" (SF.lastDef "" pieces)
in do
fileExists <- jIsReadableAndExists filePath -- needed to work-around in Java
res <- if fileExists
then do
contentBody <- LBS.readFile filePath
contentLength <- jFileSize filePath -- needed to work-around in Java
respond $ W.responseLBS H.status200
[("Content-Type", contentType),
("Content-Length", BS.pack $ show $ contentLength)]
contentBody
else
respond $ WAI.responseLBS H.status404
[("Content-Type", "text/plain")]
"File not found"
return res Full code: https://github.com/rpeszek/crud-ex-backend-servant/blob/eta-jetty/src-server/Util/WaiStatic.hs I had to work-around some issues using Java (I will create tickets) and I get exceptions printed when I run it (I will try to narrow down what is causing these) but this does serve files. Should be not hard to make it a bit better adding some etag and maybe even chunk support. Would it make sense to create servant-static-tempfix project for developer testing of servant using this approach? |
Good enough for now!
I think the code could be used to patch wai-servlet-static for eta. This is the current standard way in eta for adapt haskell packages. Etlas applies patches from that repo automatically when it installs the package. |
@jneira I was away and then absorbed by project work. Sorry for not replying. EDITED: With latest version of master this code is working fine. I found one more issue which is not relevant to static file serving and I will report on separately (things can hang at startup if several requests to server are issued at once). |
@jneira I am finding eta-hackage/servant-server which could be patched by uncommenting parts EDITED: Since this code is a temporary hack and since it is only a small part of wai-app-static I will try to patch servant-server only. That seems like the most prudent approach. I am no finding wai-servlet-static in either eta-hackage or etlas-index repos. So I assume this is a typo. |
@rpeszek Things hanging up seems like a bug to report. I've been investigating a lot of these hanging bugs recently so please do report them.
|
Ooops sorry, i wanted to mean wai-app-static. Patch servant-server is a good option too. |
@rahulmutt I created typelead/eta#520 listing various findings. I will wait with adding this to servant-server patch just in case. |
@rpeszek Yes, it does look like it's a result of my recent runtime changes. I'll take a look asap. |
@jneira @rahulmutt Note that we are missing servant-server-0.10 patches. |
This is a great start thanks! The next step is to add the ETag stuff so that the browser doesn't have to download unchanged static content over and over again and should return 304 if the context is unchanged. |
It was fun working on this. Supporting large files using chunks would be another important step. Or maybe instead of continuing piecemeal approach we just implement ResponseFile (as pointed out by @jneira). |
Hi, i've been workin in a version of wai-servlet that supports ResponseFile. It is not fully tested, but it seems that it works for the simple cases, caching files which have not been changed. |
Sounds good! Great to see progress on this. |
adding import like this
causes etlas to error out
EDITED: removed useless example project reference
The text was updated successfully, but these errors were encountered: