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 = compilerEndpoint do
37 return $ modelPosts Map.! n
39 compilerEndpoint . \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 =
60 { outputPath = OutputPath{outputPathSegs = [unPostName name], outputPathExts = []}
61 , outputData = ($ name)
63 | name <- Map.keys (modelPosts model)
65 instance MC.MonadReader Model m => Capturable PageName (Compiler m) where
66 -- Keep the 'Page' to avoid looking it up in 'contentPage'.
67 type Captured PageName (Compiler m) = CapturedExtra PageName Page
68 capturePathSegment _n =
73 { outputPath = OutputPath{outputPathSegs = [unPageName name], outputPathExts = []}
74 , outputData = ($ CapturedExtra (Left (name, page)))
76 | (name, page) <- Map.toList (modelPages model)
81 { rodelPosts :: RW m (Map PostName (RW m Post))
82 , rodelPages :: RW m (Map PageName (RW m Page))
88 { modelPosts :: Map PostName Post
89 , modelPages :: Map PageName Page
95 [ (PostName "post1", Post "post-model-1")
96 , (PostName "post2", Post "post-model-2")
100 [ (PageName "page1", Page "page-model-1")
101 , (PageName "page2", Page "page-model-2")
105 -- ** Type 'PostName'
106 data PostName = PostName {unPostName :: PathSegment}
107 deriving (Show, Eq, Ord)
109 -- ** Type 'PageName'
110 data PageName = PageName {unPageName :: PathSegment}
111 deriving (Show, Eq, Ord)
114 data Post = Post {unPost :: Text}
118 data Page = Page {unPage :: Text}