SaturnFramework / Saturn

Opinionated, web development framework for F# which implements the server-side, functional MVC pattern
https://saturnframework.org
MIT License
708 stars 108 forks source link

What is the recommended way to serve static html page #207

Closed mastoj closed 4 years ago

mastoj commented 4 years ago

I can’t find what the best way to serve a static abc.html file is in Saturn? Is it to use Controller.text and feed that the content of the file and then also set the content type header? Or is it something built in that I haven’t found where you just can point to the file?

isaacabraham commented 4 years ago

You can use the use_static command in the application and provide that with a physical path where static files live?

mastoj commented 4 years ago

@isaacabraham, sounds simple enough. But how do you go about serving an index.html on /?

Krzysztof-Cieslak commented 4 years ago

There is Controller.file helper function - https://github.com/SaturnFramework/Saturn/blob/master/src/Saturn/ControllerHelpers.fs#L42

baronfel commented 4 years ago

I'd prefer doing a permanent redirect from / to /index.html, and if you were already serving static files at your root then that would do just fine. This is nice because the underlying static files middleware handles etag calculation, HEAD requests, everything for you: https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/StaticFiles/src/StaticFileContext.cs#L298-L341

baronfel commented 4 years ago

Ooh, I found a better, more supported way: add app.UseDefaultFiles() before app.UseStaticFiles(): https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.0#serve-a-default-document

But the use_static handler already sets app.UseDefaultFiles() (see https://github.com/SaturnFramework/Saturn/blob/master/src/Saturn/Application.fs#L209), so it looks like it should work already?

mastoj commented 4 years ago

I had some problems getting it to work when I cloned the repo and tried to run the ChannelSample project. The app starts, but I can't reach any of the routes specified, this is even before me trying to modify them. It works perfectly fine when I create the project from the Saturn template.

My goal was to create a minimalistic client sample for the ChannelSample to showcase how to connect to channels.

mastoj commented 4 years ago

I'll open a separate issue on ☝️

isaacabraham commented 4 years ago

@baronfel that's exactly why I put that into Saturn a year or so ago re: usedefaultfiles :-)