on decrease-android-validity help sign in
52 lines 1.8 kB history raw
1{-# LANGUAGE EmptyCase #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE GADTs #-}
4{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE TemplateHaskell #-}
7{-# LANGUAGE KindSignatures #-}
8{-# LANGUAGE EmptyCase #-}
9{-# LANGUAGE MultiParamTypeClasses #-}
10{-# LANGUAGE FlexibleInstances #-}
11{-# LANGUAGE OverloadedStrings #-}
13module Common.Route where
15{- -- You will probably want these imports for composing Encoders.
16import Prelude hiding (id, (.))
17import Control.Category
18-}
20import Data.Text (Text)
21import Data.Functor.Identity
22import Data.Functor.Sum
24import Obelisk.Route
25import Obelisk.Route.TH
27data BackendRoute :: * -> * where
28 -- | Used to handle unparseable routes.
29 BackendRoute_Missing :: BackendRoute ()
30 -- You can define any routes that will be handled specially by the backend here.
31 -- i.e. These do not serve the frontend, but do something different, such as serving static files.
33data FrontendRoute :: * -> * where
34 FrontendRoute_Main :: FrontendRoute ()
35 -- This type is used to define frontend routes, i.e. ones for which the backend will serve the frontend.
37backendRouteEncoder
38 :: Encoder (Either Text) Identity (R (Sum BackendRoute (ObeliskRoute FrontendRoute))) PageName
39backendRouteEncoder = handleEncoder (const (InL BackendRoute_Missing :/ ())) $
40 pathComponentEncoder $ \case
41 InL backendRoute -> case backendRoute of
42 BackendRoute_Missing -> PathSegment "missing" $ unitEncoder mempty
43 InR obeliskRoute -> obeliskRouteSegment obeliskRoute $ \case
44 -- The encoder given to PathEnd determines how to parse query parameters,
45 -- in this example, we have none, so we insist on it.
46 FrontendRoute_Main -> PathEnd $ unitEncoder mempty
49concat <$> mapM deriveRouteComponent
50 [ ''BackendRoute
51 , ''FrontendRoute
52 ]