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

Rewrite index.html to / #16

Open
Profpatsch opened this issue Oct 11, 2016 · 5 comments
Open

Rewrite index.html to / #16

Profpatsch opened this issue Oct 11, 2016 · 5 comments

Comments

@Profpatsch
Copy link

Is it possible to rewrite index.html so that it is served when the user navigates to /?

I tried something like

WaiS.policy (\case
  "/" -> Just "index.html"
  _   -> Nothing))

But that only lead to my whole server not returning any static file anymore.

@habibalamin
Copy link

habibalamin commented Sep 7, 2017

I have the same issue. Anyone got a solution to this?

I'm using this:

cachedStatic :: CacheContainer -> Middleware
cachedStatic cacheContainer =
  staticPolicy' cacheContainer
    $ rewrite "/" "/index.html"
    >-> addBase "public"
      where
        rewrite from to = policy $ pure . change from to
        change from to = (== from) >>= bool id (const to)

I've also tried rewrite "" "/index.html".

@Profpatsch you have to return the original argument wrapped in a Just if you still want to respond to other static files. A Nothing basically means 'reject this URL'.

@habibalamin
Copy link

Ah, shit, never mind! I just got it to work with rewrite "" "index.html".

@Profpatsch I guess I've answered your question if you still care; you can use something similar to the code above with that minor modification. Here is the relevant policy extracted out (without import statements) for anyone interested:

rewrite :: Policy
rewrite from to = policy $ pure . change from to
  where
    change from to = (== from) >>= bool id (const to)

@phlummox
Copy link

phlummox commented Dec 20, 2019

@habibalamin That's a handy function to have (both as an example, and for practical use) - might be worth suggesting it be added to the package?

btw, I think the signature on that should be String -> String -> Policy.

@habibalamin
Copy link

@phlummox that's a good idea. I wonder if it'd be a good idea to extend it to work for any path ending in /. It'd be nice to have a policy that tries a (</> "index.html") on any request path which doesn't return anything but leaves alone paths that would return something.

Hmm…

@sol
Copy link
Contributor

sol commented Sep 11, 2020

For what it's worth, I think rewrite can be trivially implemented as:

rewrite :: [(String, FilePath)] -> Policy
rewrite mappings = only mappings <|> mempty

(read: serve only the given mappings or alternatively anything else)

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

No branches or pull requests

4 participants