From 274f555e3837522f163b6db1c7eab49a716f78ed Mon Sep 17 00:00:00 2001 From: Jacob Jonsson Date: Wed, 27 Sep 2023 21:36:20 +0200 Subject: [PATCH] FORAMT! --- app/Main.hs | 75 ++++++++++++++++++++++++++------------------------- src/Config.hs | 8 +++--- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 3fe398c..0b2ccb7 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,15 +2,14 @@ module Main ( main - ) -where + ) where import Control.Concurrent ( MVar , newMVar , threadDelay , tryPutMVar ) -import qualified Control.Concurrent.Async as Async +import qualified Control.Concurrent.Async as Async import Control.Monad ( forever ) import Control.Monad.Log ( defaultBatchingOptions , renderWithTimestamp @@ -56,8 +55,8 @@ import View ( render ) opts :: [OptDescr (Config -> Config)] opts = - [ Option [] ["help"] (NoArg (set cHelp True)) "Show usage info" - , Option [] ["port"] (ReqArg (set cPort . read) "PORT") "Port to run on" + [ Option [] ["help"] (NoArg (set cHelp True)) "Show usage info" + , Option [] ["port"] (ReqArg (set cPort . read) "PORT") "Port to run on" , Option [] ["interval"] (ReqArg (set cInterval . (1000000 *) . read) "INTERVAL (s)") @@ -68,41 +67,43 @@ usage :: IO () usage = putStrLn $ usageInfo "mat-chalmers [OPTION...]" opts main :: IO () -main = - (reifyConfig . getOpt Permute opts <$> getArgs) - >>= \case - (_ , _ , _ : _) -> usage - (_ , _ : _, _ ) -> usage - (Config{_cHelp=True}, _ , _ ) -> usage - (config , _ , _ ) -> do - upd <- newMVar () -- putMVar when to update - mgr <- newTlsManager - (viewRef, refreshAction) <- runLoggingT - (runReaderT refresh (ClientContext config mgr)) - print +main = (reifyConfig . getOpt Permute opts <$> getArgs) >>= \case + (_ , _ , _ : _) -> usage + (_ , _ : _, _ ) -> usage + (Config { _cHelp = True }, _ , _ ) -> usage + (config , _ , _ ) -> do + upd <- newMVar () -- putMVar when to update + mgr <- newTlsManager + (viewRef, refreshAction) <- runLoggingT + (runReaderT refresh (ClientContext config mgr)) + print - -- In the list there are three items running concurrently: - -- 1. Timer that sends a signal to the updater when it's time to update - -- 2. Webserver that serves the menus to the user - -- 3. Updater that fetches new data from the restaurants - Async.runConcurrently $ traverse_ Async.Concurrently - [ timer upd config - , webserver config viewRef upd - , updater mgr upd refreshAction config - ] + -- In the list there are three items running concurrently: + -- 1. Timer that sends a signal to the updater when it's time to update + -- 2. Webserver that serves the menus to the user + -- 3. Updater that fetches new data from the restaurants + Async.runConcurrently $ traverse_ + Async.Concurrently + [ timer upd config + , webserver config viewRef upd + , updater mgr upd refreshAction config + ] where - timer upd cfg = forever $ tryPutMVar upd () >> threadDelay (view cInterval cfg) + timer upd cfg = + forever $ tryPutMVar upd () >> threadDelay (view cInterval cfg) - updater mgr upd refreshAction cfg = - forever - $ withFDHandler defaultBatchingOptions stdout 1.0 80 - $ \logCallback -> - runLoggingT - (runReaderT (refreshAction upd) (ClientContext cfg mgr)) - (logCallback . renderWithTimestamp - (formatTime defaultTimeLocale (iso8601DateFormat (Just "%H:%M:%S"))) - id - ) + updater mgr upd refreshAction cfg = + forever + $ withFDHandler defaultBatchingOptions stdout 1.0 80 + $ \logCallback -> runLoggingT + (runReaderT (refreshAction upd) (ClientContext cfg mgr)) + ( logCallback + . renderWithTimestamp + (formatTime defaultTimeLocale + (iso8601DateFormat (Just "%H:%M:%S")) + ) + id + ) webserver :: Config diff --git a/src/Config.hs b/src/Config.hs index 316e862..4822482 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -13,7 +13,8 @@ data Config = Config , _cNextDayHour :: !Int , _cInterval :: !Int , _cPort :: !Int - } deriving (Eq, Show) + } + deriving (Eq, Show) makeLenses ''Config @@ -29,6 +30,5 @@ defaultConfig = Config False 14 (1000000 * 60 * 30) 5007 -- -- TODO: Feel free to bikeshed the function name. reifyConfig - :: ([Config -> Config], [String], [String]) - -> (Config, [String], [String]) -reifyConfig = (& _1 %~ foldl' (flip id) defaultConfig) + :: ([Config -> Config], [String], [String]) -> (Config, [String], [String]) +reifyConfig = (& _1 %~ foldl' (flip id) defaultConfig)