on master help sign in
Merge pull request #209 from obsidiansystems/aa-routes-skeleton

Update skeleton with routes

commitb258b1827b34753ec0a764a945df8098b0b8c691 authorRyan Trinkle <ryan.trinkle@gmail.com> dateTue 4 Sep 2018, 09:41 (8 years ago) parent57a101fdf1aa9d
8 files changed+63−36
skeleton/backend/backend.cabal+1−5 skeleton/backend/src-bin/main.hs+3−17 skeleton/backend/src/Backend.hs+12−6 skeleton/common/common.cabal+4 skeleton/common/src/Common/Route.hs+24 skeleton/frontend/frontend.cabal+4−1 skeleton/frontend/src-bin/main.hs+7−2 skeleton/frontend/src/Frontend.hs+8−5
skeleton/backend/backend.cabal+1−5
@@ -11,6 +11,7 @@ library
1111 , common
1212 , frontend
1313 , obelisk-backend
14+ , obelisk-route
1415 exposed-modules:
1516 Backend
1617 ghc-options: -Wall
@@ -24,9 +25,4 @@ executable backend
2425 , backend
2526 , common
2627 , frontend
27− , lens
28− , modern-uri
2928 , obelisk-backend
30− , obelisk-executable-config
31− , obelisk-executable-config-inject
32− , text
skeleton/backend/src-bin/main.hs+3−17
@@ -1,20 +1,6 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−import Control.Exception
3−import Control.Lens
4−import Data.Maybe
5−import Data.Text.Encoding
6−import Obelisk.ExecutableConfig (get)
7−import System.Environment
8−import qualified Text.URI as URI
9−import Text.URI.Lens
10−
111import Backend
2+import Frontend
3+import Obelisk.Backend
124
135main :: IO ()
14−main = do
15− Just route <- get "common/route"
16− port <- case URI.mkURI route of
17− Left err -> fail $ show err
18− Right uri -> return $ fromMaybe 8000 $ uri ^? uriAuthority . _Right . authPort . _Just
19− withArgs ["--port", show port] backend
20−
6+main = runBackend backend frontend
skeleton/backend/src/Backend.hs+12−6
@@ -1,10 +1,16 @@
1+{-# LANGUAGE EmptyCase #-}
2+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE LambdaCase #-}
4+{-# LANGUAGE RankNTypes #-}
5+{-# LANGUAGE TypeFamilies #-}
16module Backend where
27
3−import Common.Api
4−import Frontend
5−import qualified Obelisk.Backend as Ob
8+import Common.Route
9+import Obelisk.Backend
10+import Obelisk.Route
611
7−backend :: IO ()
8−backend = Ob.backend Ob.def
9− { Ob._backendConfig_head = fst frontend
12+backend :: Backend Void1 IndexOnlyRoute
13+backend = Backend
14+ { _backend_run = \serve -> serve $ const $ return ()
15+ , _backend_routeEncoder = backendRouteEncoder
1016 }
skeleton/common/common.cabal+4
@@ -6,6 +6,10 @@ build-type: Simple
66library
77 hs-source-dirs: src
88 build-depends: base
9+ , obelisk-route
10+ , mtl
11+ , text
912
1013 exposed-modules:
1114 Common.Api
15+ Common.Route
skeleton/common/src/Common/Route.hsnew file+24
@@ -0,0 +1,24 @@
1+{-# LANGUAGE EmptyCase #-}
2+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE GADTs #-}
4+{-# LANGUAGE LambdaCase #-}
5+{-# LANGUAGE RankNTypes #-}
6+module Common.Route where
7+
8+import Prelude hiding ((.))
9+
10+import Control.Category
11+import Control.Monad.Except
12+import Data.Text
13+import Data.Functor.Sum
14+import Obelisk.Route
15+
16+backendRouteEncoder
17+ :: forall check parse. (check ~ parse, MonadError Text parse)
18+ => Encoder check parse (R (Sum Void1 (ObeliskRoute IndexOnlyRoute))) PageName
19+backendRouteEncoder = Encoder $ do
20+ let componentEncoder = shadowEncoder void1Encoder $ obeliskRouteComponentEncoder indexOnlyRouteComponentEncoder
21+ restEncoder <- checkObeliskRouteRestEncoder indexOnlyRouteRestEncoder
22+ checkEncoder $ pathComponentEncoder (componentEncoder . someSumEncoder) $ \case
23+ InL backendRoute -> case backendRoute of {}
24+ InR obeliskRoute -> runValidEncoderFunc restEncoder obeliskRoute
skeleton/frontend/frontend.cabal+4−1
@@ -7,6 +7,8 @@ library
77 hs-source-dirs: src
88 build-depends: base
99 , common
10+ , obelisk-frontend
11+ , obelisk-route
1012 , reflex-dom
1113 , static
1214 , text
@@ -19,9 +21,10 @@ executable frontend
1921 hs-source-dirs: src-bin
2022 build-depends: base
2123 , common
24+ , obelisk-frontend
25+ , obelisk-route
2226 , reflex-dom
2327 , static
24− , text
2528 , frontend
2629 --TODO: Make these ghc-options optional
2730 ghc-options: -threaded
skeleton/frontend/src-bin/main.hs+7−2
@@ -1,5 +1,10 @@
1−import Reflex.Dom
21import Frontend
2+import Common.Route
3+import Obelisk.Frontend
4+import Obelisk.Route.Frontend
5+import Reflex.Dom
36
47main :: IO ()
5−main = mainWidget $ snd frontend
8+main = do
9+ let Right validFullEncoder = checkEncoder backendRouteEncoder
10+ run $ runFrontend validFullEncoder frontend
skeleton/frontend/src/Frontend.hs+8−5
@@ -4,16 +4,19 @@
44module Frontend where
55
66import qualified Data.Text as T
7+import Obelisk.Frontend
8+import Obelisk.Route
79import Reflex.Dom.Core
810
911import Common.Api
1012import Static
1113
12−frontend :: (StaticWidget x (), Widget x ())
13−frontend = (head', body)
14− where
15− head' = el "title" $ text "Obelisk Minimal Example"
16− body = do
14+frontend :: Frontend (R IndexOnlyRoute)
15+frontend = Frontend
16+ { _frontend_head = el "title" $ text "Obelisk Minimal Example"
17+ , _frontend_body = do
1718 text "Welcome to Obelisk!"
1819 el "p" $ text $ T.pack commonStuff
1920 elAttr "img" ("src" =: static @"obelisk.jpg") blank
21+ , _frontend_notFoundRoute = const $ IndexOnlyRoute :/ ()
22+ }