on master help sign in
Remove ComponentConfigs and don't run backend in ConfigsT
commitb58ab89601ed65f5f0eb34d5c8b6814f4e9095b5 authorAli Abrar <aliabrar@gmail.com> dateFri 14 Jun 2019, 20:03 (7 years ago) parenteada55e
2 files changed+26−45
lib/backend/src/Obelisk/Backend.hs+17−32 lib/run/src/Obelisk/Run.hs+9−13
lib/backend/src/Obelisk/Backend.hs+17−32
@@ -11,7 +11,6 @@ module Obelisk.Backend
1111 ( Backend (..)
1212 -- * Re-exports
1313 , Default (def)
14− , ComponentConfigs(..)
1514 , getPageName
1615 , getRouteWith
1716 , runSnapWithCommandLineArgs
@@ -20,7 +19,7 @@ module Obelisk.Backend
2019 , runBackend
2120 , staticRenderContentType
2221 , mkRouteToUrl
23− , getComponentConfigs
22+ , getPublicConfigs
2423 ) where
2524
2625import Prelude hiding (id, (.))
@@ -41,7 +40,6 @@ import Data.Text (Text)
4140import qualified Data.Text as T
4241import Data.Text.Encoding
4342import Obelisk.Asset.Serve.Snap (serveAsset)
44−import Obelisk.Configs
4543import qualified Obelisk.ExecutableConfig.Lookup as Lookup
4644import Obelisk.Frontend
4745import Obelisk.Route
@@ -55,14 +53,7 @@ import System.IO (BufferMode (..), hSetBuffering, stderr, stdout)
5553
5654data Backend backendRoute frontendRoute = Backend
5755 { _backend_routeEncoder :: Encoder (Either Text) Identity (R (Sum backendRoute (ObeliskRoute frontendRoute))) PageName
58− , _backend_run :: ((R backendRoute -> ConfigsT Snap ()) -> ConfigsT IO ()) -> ConfigsT IO ()
59− }
60−
61−-- | All the configs separated by which context they are accessible in. Common
62−-- and frontend configs are present in both.
63−data ComponentConfigs = ComponentConfigs
64− { _componentConfigs_backend :: Map Text ByteString
65− , _componentConfigs_frontend :: Map Text ByteString
56+ , _backend_run :: ((R backendRoute -> Snap ()) -> IO ()) -> IO ()
6657 }
6758
6859-- | The static assets provided must contain a compiled GHCJS app that corresponds exactly to the Frontend provided
@@ -187,17 +178,15 @@ runBackend :: Backend fullRoute frontendRoute -> Frontend (R frontendRoute) -> I
187178runBackend backend frontend = case checkEncoder $ _backend_routeEncoder backend of
188179 Left e -> fail $ "backend error:\n" <> T.unpack e
189180 Right validFullEncoder -> do
190− configs <- getComponentConfigs
191− runConfigsT (_componentConfigs_backend configs) $
192− _backend_run backend $ \serveRoute -> do
193− runSnapWithCommandLineArgs $ do
194− getRouteWith validFullEncoder >>= \case
195− Identity r -> case r of
196− InL backendRoute :=> Identity a ->
197− runConfigsT (_componentConfigs_backend configs) $
198− serveRoute $ backendRoute :/ a
199− InR obeliskRoute :=> Identity a ->
200− serveDefaultObeliskApp (mkRouteToUrl validFullEncoder) (serveStaticAssets defaultStaticAssets) frontend (_componentConfigs_frontend configs) $ obeliskRoute :/ a
181+ publicConfigs <- getPublicConfigs
182+ _backend_run backend $ \serveRoute -> do
183+ runSnapWithCommandLineArgs $ do
184+ getRouteWith validFullEncoder >>= \case
185+ Identity r -> case r of
186+ InL backendRoute :=> Identity a -> serveRoute $ backendRoute :/ a
187+ InR obeliskRoute :=> Identity a ->
188+ serveDefaultObeliskApp (mkRouteToUrl validFullEncoder) (serveStaticAssets defaultStaticAssets) frontend publicConfigs $
189+ obeliskRoute :/ a
201190
202191mkRouteToUrl :: Encoder Identity parse (R (Sum f (ObeliskRoute r))) PageName -> R r -> Text
203192mkRouteToUrl validFullEncoder =
@@ -220,13 +209,9 @@ renderGhcjsFrontend urlEnc route configs f = do
220209instance HasCookies Snap where
221210 askCookies = map (\c -> (cookieName c, cookieValue c)) <$> getsRequest rqCookies
222211
223−getComponentConfigs :: IO ComponentConfigs
224−getComponentConfigs = do
225− allConfigs <- Lookup.getConfigs
226− return $ ComponentConfigs
227− { _componentConfigs_backend =
228− allConfigs
229− , _componentConfigs_frontend =
230− Map.filterWithKey (\k _ -> isMemberOf k ["common", "frontend"]) allConfigs
231− }
232− where isMemberOf k dirs = or $ map (`T.isPrefixOf` k) dirs
212+-- | Get configs from the canonical "public" locations (i.e., locations that obelisk expects to make available
213+-- to frontend applications, and hence visible to end users).
214+getPublicConfigs :: IO (Map Text ByteString)
215+getPublicConfigs = Map.filterWithKey (\k _ -> isMemberOf k ["common", "frontend"]) <$> Lookup.getConfigs
216+ where
217+ isMemberOf k = any (`T.isPrefixOf` k)
lib/run/src/Obelisk/Run.hs+9−13
@@ -49,7 +49,6 @@ import Network.WebSockets (ConnectionOptions)
4949import Network.WebSockets.Connection (defaultConnectionOptions)
5050import qualified Obelisk.Asset.Serve.Snap as Snap
5151import Obelisk.Backend
52−import Obelisk.Configs
5352import Obelisk.Frontend
5453import Obelisk.Route.Frontend
5554import Reflex.Dom.Core
@@ -76,20 +75,17 @@ run port serveStaticAsset backend frontend = do
7675 case checkEncoder $ _backend_routeEncoder backend of
7776 Left e -> hPutStrLn stderr $ "backend error:\n" <> T.unpack e
7877 Right validFullEncoder -> do
79− configs <- getComponentConfigs
78+ publicConfigs <- getPublicConfigs
8079 backendTid <- forkIO $ handle handleBackendErr $ withArgs ["--quiet", "--port", show port] $ do
81− runConfigsT (_componentConfigs_backend configs) $
82− _backend_run backend $ \serveRoute -> do
83− runSnapWithCommandLineArgs $ do
84− getRouteWith validFullEncoder >>= \case
85− Identity r -> case r of
86− InL backendRoute :=> Identity a ->
87− runConfigsT (_componentConfigs_backend configs) $
88− serveRoute $ backendRoute :/ a
89− InR obeliskRoute :=> Identity a ->
90− serveDefaultObeliskApp (mkRouteToUrl validFullEncoder) serveStaticAsset frontend (_componentConfigs_frontend configs) $ obeliskRoute :/ a
80+ _backend_run backend $ \serveRoute -> do
81+ runSnapWithCommandLineArgs $ do
82+ getRouteWith validFullEncoder >>= \case
83+ Identity r -> case r of
84+ InL backendRoute :=> Identity a -> serveRoute $ backendRoute :/ a
85+ InR obeliskRoute :=> Identity a ->
86+ serveDefaultObeliskApp (mkRouteToUrl validFullEncoder) serveStaticAsset frontend publicConfigs $ obeliskRoute :/ a
9187 let conf = defRunConfig { _runConfig_redirectPort = port }
92− runWidget conf (_componentConfigs_frontend configs) frontend validFullEncoder `finally` killThread backendTid
88+ runWidget conf publicConfigs frontend validFullEncoder `finally` killThread backendTid
9389
9490-- Convenience wrapper to handle path segments for 'Snap.serveAsset'
9591runServeAsset :: FilePath -> [Text] -> Snap ()