1 {-# LANGUAGE UndecidableInstances #-}
2 {-# LANGUAGE NoMonomorphismRestriction #-}
3 {-# OPTIONS_GHC -Wno-missing-signatures #-}
5 module Examples.Ex04 where
7 import Control.Monad.Classes qualified as MC
9 -- import Control.Reactive
10 import Data.Map.Strict as Map
16 </> capturePathSegment @PostName "post"
17 <.> response @Post @'[PlainText]
19 </> capturePathSegment @PageName "page"
20 <.> response @Page @'[PlainText]
21 <+> pathSegment "lorem"
24 -- ( PostName -> (a :~: a, Post)
25 -- , PageName -> (a :~: a, Page) )
27 -- MC.MonadReader Model m =>
28 -- endpoint ~ m Text =>
30 -- (Either (PostName, Sym.Endpoint endpoint (m Post))
31 -- (PageName, Sym.Endpoint endpoint (m Page)))
33 content = contentPost :!: contentPage :!: contentOther
35 contentPost n = contentEndpoint do
37 return $ modelPosts Map.! n
39 contentEndpoint . \case
40 CapturedExtra (Left (_n, p)) -> do
42 CapturedExtra (Right n) -> do
44 return $ modelPages Map.! n
45 contentOther = return "ipsum"
47 -- c0 = compile CompilerEnv{} router content
49 instance MimeEncodable Post PlainText where
50 mimeEncode (Post t) = mimeEncode @_ @PlainText t
51 instance MimeEncodable Page PlainText where
52 mimeEncode (Page t) = mimeEncode @_ @PlainText t
54 instance (MC.MonadReader Model m) => Capturable PostName (Compiler m) where
55 capturePathSegment _n =
59 [ Output{outputPath = [unPostName name], outputExts = [], outputData = ($ name)}
60 | name <- Map.keys (modelPosts model)
62 instance (MC.MonadReader Model m) => Capturable PageName (Compiler m) where
63 -- Keep the 'Page' to avoid looking it up in 'contentPage'.
64 type Captured PageName (Compiler m) = CapturedExtra PageName Page
65 capturePathSegment _n =
70 { outputPath = [unPageName name]
72 , outputData = ($ CapturedExtra (Left (name, page)))
74 | (name, page) <- Map.toList (modelPages model)
79 { rodelPosts :: RW m (Map PostName (RW m Post))
80 , rodelPages :: RW m (Map PageName (RW m Page))
86 { modelPosts :: Map PostName Post
87 , modelPages :: Map PageName Page
93 [ (PostName "post1", Post "post-model-1")
94 , (PostName "post2", Post "post-model-2")
98 [ (PageName "page1", Page "page-model-1")
99 , (PageName "page2", Page "page-model-2")
103 -- ** Type 'PostName'
104 data PostName = PostName {unPostName :: PathSegment}
105 deriving (Show, Eq, Ord)
107 -- ** Type 'PageName'
108 data PageName = PageName {unPageName :: PathSegment}
109 deriving (Show, Eq, Ord)
112 data Post = Post {unPost :: Text}
116 data Page = Page {unPage :: Text}