diff options
Diffstat (limited to 'server/src')
| -rw-r--r-- | server/src/Thermoprint/Server.hs | 57 | 
1 files changed, 32 insertions, 25 deletions
| diff --git a/server/src/Thermoprint/Server.hs b/server/src/Thermoprint/Server.hs index f73a418..07462da 100644 --- a/server/src/Thermoprint/Server.hs +++ b/server/src/Thermoprint/Server.hs | |||
| @@ -67,6 +67,8 @@ import Servant.API | |||
| 67 | import Servant.Utils.Links | 67 | import Servant.Utils.Links | 
| 68 | import Network.URI | 68 | import Network.URI | 
| 69 | 69 | ||
| 70 | import System.Environment (lookupEnv) | ||
| 71 | |||
| 70 | import Database.Persist.Sql (runMigrationSilent, ConnectionPool, runSqlPool) | 72 | import Database.Persist.Sql (runMigrationSilent, ConnectionPool, runSqlPool) | 
| 71 | 73 | ||
| 72 | 74 | ||
| @@ -135,28 +137,33 @@ thermoprintServer :: ( MonadLoggerIO m | |||
| 135 | -> (m :~> IO) -- ^ 'dyre' controls the base of the monad-transformer-stack ('IO') but we let the user specify much of the rest of it (we handle 'ResourceT' ourselves, since we need it to fork properly). Therefore we require a specification of how to collapse the stack. | 137 | -> (m :~> IO) -- ^ 'dyre' controls the base of the monad-transformer-stack ('IO') but we let the user specify much of the rest of it (we handle 'ResourceT' ourselves, since we need it to fork properly). Therefore we require a specification of how to collapse the stack. | 
| 136 | -> ResourceT m (Config (ResourceT m)) -> IO () | 138 | -> ResourceT m (Config (ResourceT m)) -> IO () | 
| 137 | -- ^ Run the server | 139 | -- ^ Run the server | 
| 138 | thermoprintServer dyre io = Dyre.wrapMain $ Dyre.defaultParams | 140 | thermoprintServer dyre io = do | 
| 139 | { Dyre.projectName = "thermoprint-server" | 141 | cfgDir <- lookupEnv "THERMOPRINT_CONFIG" | 
| 140 | , Dyre.realMain = realMain | 142 | cacheDir <- lookupEnv "THERMOPRINT_CACHE" | 
| 141 | , Dyre.showError = flip (\msg -> fmap (\cfg -> cfg { dyreError = Just msg })) | 143 | Dyre.wrapMain $ Dyre.defaultParams | 
| 142 | , Dyre.configCheck = dyre | 144 | { Dyre.projectName = "thermoprint-server" | 
| 143 | } | 145 | , Dyre.realMain = realMain | 
| 144 | where | 146 | , Dyre.showError = flip (\msg -> fmap (\cfg -> cfg { dyreError = Just msg })) | 
| 145 | realMain cfg = unNat (io . Nat runResourceT) $ do | 147 | , Dyre.configCheck = dyre | 
| 146 | tMgr <- threadManager resourceForkIO | 148 | , Dyre.configDir = cfgDir | 
| 147 | flip finally (cleanup tMgr) $ do | 149 | , Dyre.cacheDir = cacheDir | 
| 148 | Config{..} <- cfg | 150 | } | 
| 149 | maybe (return ()) ($(logErrorS) "Dyre" . T.pack) dyreError | 151 | where | 
| 150 | mapM_ ($(logWarnS) "DB") =<< runSqlPool (runMigrationSilent migrateAll) =<< ask | 152 | realMain cfg = unNat (io . Nat runResourceT) $ do | 
| 151 | forM_ printers $ fork tMgr . runPrinter | 153 | tMgr <- threadManager resourceForkIO | 
| 152 | gcChan <- liftIO newTChanIO | 154 | flip finally (cleanup tMgr) $ do | 
| 153 | fork tMgr $ jobGC gcChan | 155 | Config{..} <- cfg | 
| 154 | let | 156 | maybe (return ()) ($(logErrorS) "Dyre" . T.pack) dyreError | 
| 155 | runQM' (queueManagers -> QMConfig qm nat) printer = unNat nat $ runQM gcChan qm printer | 157 | mapM_ ($(logWarnS) "DB") =<< runSqlPool (runMigrationSilent migrateAll) =<< ask | 
| 156 | mapM_ (fork tMgr . uncurry runQM') $ Map.toList printers | 158 | forM_ printers $ fork tMgr . runPrinter | 
| 157 | nChan <- liftIO $ newBroadcastTChanIO | 159 | gcChan <- liftIO newTChanIO | 
| 158 | let | 160 | fork tMgr $ jobGC gcChan | 
| 159 | printerUrl :: API.PrinterId -> URI | 161 | let | 
| 160 | printerUrl = safeLink thermoprintAPI (Proxy :: Proxy ("jobs" :> QueryParam "printer" API.PrinterId :> Get '[JSON] (Seq (API.JobId, UTCTime, JobStatus)))) | 162 | runQM' (queueManagers -> QMConfig qm nat) printer = unNat nat $ runQM gcChan qm printer | 
| 161 | mapM_ (fork tMgr . uncurry (notifyOnChange nChan ((==) `on` fromZipper)) . bimap printerUrl queue) $ Map.toList printers | 163 | mapM_ (fork tMgr . uncurry runQM') $ Map.toList printers | 
| 162 | liftIO . Warp.runSettings warpSettings . withPush nChan . serve thermoprintAPI . flip enter API.thermoprintServer =<< handlerNat printers nChan | 164 | nChan <- liftIO $ newBroadcastTChanIO | 
| 165 | let | ||
| 166 | printerUrl :: API.PrinterId -> URI | ||
| 167 | printerUrl = safeLink thermoprintAPI (Proxy :: Proxy ("jobs" :> QueryParam "printer" API.PrinterId :> Get '[JSON] (Seq (API.JobId, UTCTime, JobStatus)))) | ||
| 168 | mapM_ (fork tMgr . uncurry (notifyOnChange nChan ((==) `on` fromZipper)) . bimap printerUrl queue) $ Map.toList printers | ||
| 169 | liftIO . Warp.runSettings warpSettings . withPush nChan . serve thermoprintAPI . flip enter API.thermoprintServer =<< handlerNat printers nChan | ||
