on master help sign in
Add a backend check step prior to running the backend
commit5dfd178cc8cd00887185ec0ab8f367aa79c82b98 authorRyan Trinkle <ryan.trinkle@gmail.com> dateTue 10 Jul 2018, 18:03 (8 years ago) parent76b4a5c
2 files changed+44−35
lib/backend/src/Obelisk/Backend.hs+28−26 lib/run/src/Obelisk/Run.hs+16−9
lib/backend/src/Obelisk/Backend.hs+28−26
@@ -66,38 +66,40 @@ backend
6666 :: ( Universe (R route) --TODO: This seems wrong - should be Universe (Some route)
6767 , OrdTag route Identity
6868 , ShowTag route Identity
69+ , check ~ Either Text --TODO: Replace with MonadError Text check
6970 )
7071 => BackendConfig route
71− -> IO ()
72+ -> check (IO ())
7273backend cfg = do
73− -- Make output more legible by decreasing the likelihood of output from
74− -- multiple threads being interleaved
75− hSetBuffering stdout LineBuffering
76− hSetBuffering stderr LineBuffering
74+ getRequestRoute <- checkGetRequestRoute $ _backendConfig_routeEncoder cfg --TODO: Report error better
75+ return $ do
76+ -- Make output more legible by decreasing the likelihood of output from
77+ -- multiple threads being interleaved
78+ hSetBuffering stdout LineBuffering
79+ hSetBuffering stderr LineBuffering
7780
78− -- Get the web server configuration from the command line
79− cmdLineConf <- commandLineConfig defaultConfig
80− let httpConf = cmdLineConf
81− { accessLog = Just $ ConfigIoLog BSC8.putStrLn
82− , errorLog = Just $ ConfigIoLog BSC8.putStrLn
83− }
84− let Right getRequestRoute = checkGetRequestRoute $ _backendConfig_routeEncoder cfg --TODO: Report error better
85− -- Start the web server
86− httpServe httpConf $ do
87− let staticAssets = StaticAssets
88− { _staticAssets_processed = "static.jsexe.assets"
89− , _staticAssets_unprocessed = "static.jsexe"
81+ -- Get the web server configuration from the command line
82+ cmdLineConf <- commandLineConfig defaultConfig
83+ let httpConf = cmdLineConf
84+ { accessLog = Just $ ConfigIoLog BSC8.putStrLn
85+ , errorLog = Just $ ConfigIoLog BSC8.putStrLn
9086 }
91− frontendApp = GhcjsApp
92− { _ghcjsApp_compiled = StaticAssets
93− { _staticAssets_processed = "frontend.jsexe.assets"
94− , _staticAssets_unprocessed = "frontend.jsexe"
87+ -- Start the web server
88+ httpServe httpConf $ do
89+ let staticAssets = StaticAssets
90+ { _staticAssets_processed = "static.jsexe.assets"
91+ , _staticAssets_unprocessed = "static.jsexe"
9592 }
96− , _ghcjsApp_value = _backendConfig_frontend cfg
97− }
98− getRequestRoute >>= \case
99− Left e -> writeText e
100− Right r -> serveObeliskRoute staticAssets frontendApp r
93+ frontendApp = GhcjsApp
94+ { _ghcjsApp_compiled = StaticAssets
95+ { _staticAssets_processed = "frontend.jsexe.assets"
96+ , _staticAssets_unprocessed = "frontend.jsexe"
97+ }
98+ , _ghcjsApp_value = _backendConfig_frontend cfg
99+ }
100+ getRequestRoute >>= \case
101+ Left e -> writeText e
102+ Right r -> serveObeliskRoute staticAssets frontendApp r
101103
102104checkGetRequestRoute :: (MonadSnap m, Monad check, MonadError Text parse) => Encoder check parse route PageName -> check (m (parse route))
103105checkGetRequestRoute routeEncoder = do
lib/run/src/Obelisk/Run.hs+16−9
@@ -68,16 +68,23 @@ import Text.URI (URI)
6868import qualified Text.URI as URI
6969import Text.URI.Lens
7070
71−run :: (Universe route, Ord route, Show route)
72− => Int -- ^ Port to run the backend
73− -> IO () -- ^ Backend
74− -> Frontend route -- ^ Frontend
75− -> IO ()
76−run port backend frontend = do
71+run
72+ :: ( Universe route
73+ , Ord route
74+ , Show route
75+ )
76+ => Int -- ^ Port to run the backend
77+ -> Either Text (IO ()) -- ^ Backend
78+ -> Frontend route -- ^ Frontend
79+ -> IO ()
80+run port checkBackend frontend = do
7781 let handleBackendErr (e :: IOException) = hPutStrLn stderr $ "backend stopped; make a change to your code to reload - error " <> show e
78− backendTid <- forkIO $ handle handleBackendErr $ withArgs ["--quiet", "--port", show port] backend
79− let conf = defRunConfig { _runConfig_redirectPort = port }
80− runWidget conf frontend `finally` killThread backendTid
82+ case checkBackend of
83+ Left e -> hPutStrLn stderr $ "backend error:\n" <> T.unpack e
84+ Right backend -> do
85+ backendTid <- forkIO $ handle handleBackendErr $ withArgs ["--quiet", "--port", show port] backend
86+ let conf = defRunConfig { _runConfig_redirectPort = port }
87+ runWidget conf frontend `finally` killThread backendTid
8188
8289getConfigRoute :: IO (Maybe URI)
8390getConfigRoute = get "common/route" >>= \case