22
33module Init where
44
5+ import Control.Concurrent (killThread )
56import qualified Control.Monad.Metrics as M
67import Database.Persist.Postgresql (runSqlPool )
8+ import Lens.Micro ((^.) )
79import Network.Wai (Application )
810import Network.Wai.Metrics (metrics , registerWaiMetrics )
911import System.Environment (lookupEnv )
10- import System.Remote.Monitoring (forkServer , serverMetricStore )
12+ import System.Remote.Monitoring (forkServer , serverMetricStore ,
13+ serverThreadId )
1114
1215import Api (app )
1316import Api.User (generateJavaScript )
1417import Config (Config (.. ), Environment (.. ),
1518 makePool , setLogger )
19+ import Control.Exception (bracket )
20+ import qualified Data.Pool as Pool
21+ import qualified Katip
1622import Logger (defaultLogEnv )
1723import Models (doMigrations )
18- import Safe (readMay )
19- import Control.Exception (bracket )
2024import Network.Wai.Handler.Warp (run )
21- import qualified Data.Pool as Pool
22- import qualified Katip
25+ import Safe (readMay )
2326
2427-- | An action that creates a WAI 'Application' together with its resources,
2528-- runs it, and tears it down on exit
@@ -31,14 +34,12 @@ runApp = bracket acquireConfig shutdownApp runApp
3134-- | The 'initialize' function accepts the required environment information,
3235-- initializes the WAI 'Application' and returns it
3336initialize :: Config -> IO Application
34- initialize cfg@ (Config pool env _ _ _) = do
35- waiMetrics <-
36- registerWaiMetrics =<< serverMetricStore
37- <$> forkServer " localhost" 8000
38- let logger = setLogger env
39- runSqlPool doMigrations pool
37+ initialize cfg = do
38+ waiMetrics <- registerWaiMetrics (configMetrics cfg ^. M. metricsStore)
39+ let logger = setLogger (configEnv cfg)
40+ runSqlPool doMigrations (configPool cfg)
4041 generateJavaScript
41- pure ( logger $ metrics waiMetrics $ app cfg)
42+ pure . logger . metrics waiMetrics . app $ cfg
4243
4344-- | Allocates resources for 'Config'
4445acquireConfig :: IO Config
@@ -47,23 +48,28 @@ acquireConfig = do
4748 env <- lookupSetting " ENV" Development
4849 logEnv <- defaultLogEnv
4950 pool <- makePool env logEnv
50- store <- serverMetricStore <$> forkServer " localhost" 8000
51- waiMetrics <- registerWaiMetrics store
51+ ekgServer <- forkServer " localhost" 8000
52+ let store = serverMetricStore ekgServer
53+ waiMetrics <- registerWaiMetrics store
5254 metr <- M. initializeWith store
53- pure Config { configPool = pool
54- , configEnv = env
55- , configMetrics = metr
56- , configLogEnv = logEnv
57- , configPort = port }
55+ pure Config
56+ { configPool = pool
57+ , configEnv = env
58+ , configMetrics = metr
59+ , configLogEnv = logEnv
60+ , configPort = port
61+ , configEkgServer = serverThreadId ekgServer
62+ }
5863
5964-- | Takes care of cleaning up 'Config' resources
6065shutdownApp :: Config -> IO ()
61- shutdownApp ( Config pool _env metr logEnv _port) = do
62- Katip. closeScribes logEnv
63- Pool. destroyAllResources pool
66+ shutdownApp cfg = do
67+ Katip. closeScribes (configLogEnv cfg)
68+ Pool. destroyAllResources (configPool cfg)
6469 -- Monad.Metrics does not provide a function to destroy metrics store
6570 -- so, it'll hopefully get torn down when async exception gets thrown
6671 -- at metrics server process
72+ killThread (configEkgServer cfg)
6773 pure ()
6874
6975-- | Looks up a setting in the environment, with a provided default, and
0 commit comments