Created
August 1, 2014 13:10
-
-
Save snoyberg/8d85d41c5b4d0da002ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE QuasiQuotes #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
{-# LANGUAGE TypeFamilies #-} | |
import Yesod | |
import Yesod.OptionalSubsite | |
import Yesod.Static | |
data HelloWorld = HelloWorld | |
getStatic _ = OptionalSubsite Nothing | |
mkYesod "HelloWorld" [parseRoutes| | |
/ HomeR GET | |
/static StaticR OptionalSubsite-Static getStatic | |
|] | |
instance Yesod HelloWorld | |
getHomeR :: Handler Html | |
getHomeR = defaultLayout [whamlet|Hello World!|] | |
main :: IO () | |
main = warp 3000 HelloWorld |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE StandaloneDeriving #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
module Yesod.OptionalSubsite where | |
import Yesod.Core | |
import Yesod.Core.Types | |
newtype OptionalSubsite a = OptionalSubsite (Maybe a) | |
instance RenderRoute a => RenderRoute (OptionalSubsite a) where | |
newtype Route (OptionalSubsite a) = OptionalRoute (Route a) | |
renderRoute (OptionalRoute x) = renderRoute x | |
deriving instance Eq (Route a) => Eq (Route (OptionalSubsite a)) | |
deriving instance Show (Route a) => Show (Route (OptionalSubsite a)) | |
deriving instance Read (Route a) => Read (Route (OptionalSubsite a)) | |
instance ParseRoute a => ParseRoute (OptionalSubsite a) where | |
parseRoute = fmap OptionalRoute . parseRoute | |
instance (MonadHandler m, YesodSubDispatch a m) => YesodSubDispatch (OptionalSubsite a) m where | |
yesodSubDispatch subEnv req = | |
case msub of | |
Nothing -> ysreParentRunner subEnv notFound (ysreParentEnv subEnv) Nothing req | |
Just sub -> flip yesodSubDispatch req subEnv | |
{ ysreGetSub = const sub | |
, ysreToParentRoute = ysreToParentRoute subEnv . OptionalRoute | |
} | |
where | |
OptionalSubsite msub = ysreGetSub subEnv $ yreSite $ ysreParentEnv subEnv |
You can use the StaticRoute
data constructor: https://www.stackage.org/haddock/lts-18.26/yesod-static-1.6.1.0/src/Yesod.Static.html#line-166
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have stumbled upon this gist after 8 years. How can I specify the route to my OptionalSubsite in hamlet? I need the route to some file but I can not depend on template Haskell functions such as
staticFiles
to generate the routes because the served folder is optional.