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

Add helper to make it easier for people to make custom route combinators #22

Open
dbp opened this issue Dec 16, 2018 · 1 comment
Open

Comments

@dbp
Copy link
Contributor

dbp commented Dec 16, 2018

I was adding an authentication check, and thought that it would be nice if it were part of the routing (at least in the application I was dealing with, I didn't want to even reveal the existence of the paths to non authenticated users). The combinators like segment, end, etc can't access the application context (which I wonder if they should be able to!), but I was able to make something that I could use like:

segment // path "blah" // myauth ctxt // ... ==> ...

And it was pretty easy to do, but mainly because I already had an idea how the rest of the combinators worked, as I was able to just modify anything to get something like:

myauth :: Ctxt -> Req -> IO (Maybe (Req, a -> a))
myauth ctxt req = return $ if authcheck ctxt then Just (req, id) else Nothing

As I wasn't actually trying to do anything interesting with the actual request -- just stop this route if the user wasn't authenticated.

Which made me wonder -- would it be helpful to have a helper to make this type of function (and maybe some others that are similar)? It's not that it's much code, it's just that having a function that says this lets you create a new, application specific routing combinator, might be helpful. For it to be able to modify the path gets tricky, but if it's just reading the request (or as in this case, not even doing that), the interface might not actually be that complicated.

@emhoracek
Copy link
Member

@dbp Sorry for the delay, I only just saw this!!

I often write these sorts of things after the ==>, like:

path "admin" ==> adminCheck adminRoutes
adminCheck :: [Route Ctxt] -> Ctxt -> IO (Maybe Response)
adminCheck routes ctxt = do
  isAdmin <- checkIfAdmin ctxt 
  if isAdmin
    then route ctxt routes
    else notFoundText "Not found"

I'm not sure what a good helper would look like, especially in the part before the ==>. That's partly because I can't sell my pair partners on even Fn's current features like matching on POST params in routes, so I've never considered trying to make anything fancy myself.

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

2 participants