Вопрос

I want to create a part of my Yesod website that should be enabled only if certain configuration settings are present. So I have something like this (omitting details):

data MasterData = MasterData
  { subData :: Maybe SubData
  }

data SubData = SubData { ... }

mkYesodData "SubData" [parseRoutes| ... |]

mkYesodData "MasterData" [parseRoutes|
  /sub SubsiteR SubData getSubData
|]

How could I make my site return HTTP 404 for all urls starting with /sub if subData is Nothing?

Это было полезно?

Решение

One approach would be to create a newtype wrapper around Maybe that provides an instance of YesodSubDispatch, and have it return 404 whenever its argument is Nothing.

This turned out to be a fun exercise in subsites; you can view the code as a Gist. The one trick is that, when you link to that subsite, you'll need to wrap your constructors with OptionalRoute.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top