aboutsummaryrefslogtreecommitdiff
path: root/server/src/Thermoprint/Server.hs
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-23 19:42:22 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-23 19:42:22 +0000
commit6434397a3d103547b563ada27fd64c38cb05e1f0 (patch)
tree29e67e7a0804ca46467565c35dc3c15447bf814e /server/src/Thermoprint/Server.hs
parent8553c33f72c41e553cbef4e7175cef8cec3cdbe2 (diff)
downloadthermoprint-6434397a3d103547b563ada27fd64c38cb05e1f0.tar
thermoprint-6434397a3d103547b563ada27fd64c38cb05e1f0.tar.gz
thermoprint-6434397a3d103547b563ada27fd64c38cb05e1f0.tar.bz2
thermoprint-6434397a3d103547b563ada27fd64c38cb05e1f0.tar.xz
thermoprint-6434397a3d103547b563ada27fd64c38cb05e1f0.zip
Broken existentially quantified printer config
Diffstat (limited to 'server/src/Thermoprint/Server.hs')
-rw-r--r--server/src/Thermoprint/Server.hs21
1 files changed, 17 insertions, 4 deletions
diff --git a/server/src/Thermoprint/Server.hs b/server/src/Thermoprint/Server.hs
index 39bf0a1..4e8d962 100644
--- a/server/src/Thermoprint/Server.hs
+++ b/server/src/Thermoprint/Server.hs
@@ -3,10 +3,11 @@
3{-# LANGUAGE OverloadedStrings #-} 3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE TypeOperators #-} 4{-# LANGUAGE TypeOperators #-}
5{-# LANGUAGE FlexibleContexts #-} 5{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE ViewPatterns #-}
6 7
7module Thermoprint.Server 8module Thermoprint.Server
8 ( thermoprintServer 9 ( thermoprintServer
9 , Config(..) 10 , Config(..), withPrinters
10 , module Data.Default.Class 11 , module Data.Default.Class
11 , module Servant.Server.Internal.Enter 12 , module Servant.Server.Internal.Enter
12 , module Thermoprint.Server.Printer 13 , module Thermoprint.Server.Printer
@@ -19,7 +20,8 @@ import Data.Map (Map)
19import qualified Data.Map as Map 20import qualified Data.Map as Map
20 21
21import Data.Maybe (maybe) 22import Data.Maybe (maybe)
22import Data.Foldable (mapM_, forM_) 23import Data.Foldable (mapM_, forM_, foldlM)
24import Data.Monoid
23 25
24import Control.Monad.Trans.Resource 26import Control.Monad.Trans.Resource
25import Control.Monad.Trans.Control 27import Control.Monad.Trans.Control
@@ -27,6 +29,8 @@ import Control.Monad.Logger
27import Control.Monad.Reader 29import Control.Monad.Reader
28import Control.Monad.IO.Class 30import Control.Monad.IO.Class
29 31
32import Control.Monad.Writer
33
30import Control.Concurrent 34import Control.Concurrent
31 35
32import Data.Text (Text) 36import Data.Text (Text)
@@ -62,9 +66,9 @@ instance Default Config where
62 66
63 67
64thermoprintServer :: ( MonadLoggerIO m 68thermoprintServer :: ( MonadLoggerIO m
65 , MonadIO m
66 , MonadBaseControl IO m
67 , MonadReader ConnectionPool m 69 , MonadReader ConnectionPool m
70 , MonadResource m
71 , MonadBaseControl IO m
68 ) => (m :~> IO) -- ^ 'dyre' controls the base of the monad-transformer-stack ('IO') but we let the user specify the rest of it. Therefore we require a specification of how to enter the stack. 72 ) => (m :~> IO) -- ^ 'dyre' controls the base of the monad-transformer-stack ('IO') but we let the user specify the rest of it. Therefore we require a specification of how to enter the stack.
69 -> Config -> IO () 73 -> Config -> IO ()
70-- ^ Run the server 74-- ^ Run the server
@@ -79,3 +83,12 @@ thermoprintServer io = Dyre.wrapMain $ Dyre.defaultParams
79 mapM_ ($(logWarnS) "DB") =<< runSqlPool (runMigrationSilent migrateAll) =<< ask 83 mapM_ ($(logWarnS) "DB") =<< runSqlPool (runMigrationSilent migrateAll) =<< ask
80 forM_ printers $ liftBaseDiscard forkIO . runPrinter 84 forM_ printers $ liftBaseDiscard forkIO . runPrinter
81 liftIO . Warp.runSettings warpSettings . serve thermoprintAPI . flip enter API.thermoprintServer =<< handlerNat printers 85 liftIO . Warp.runSettings warpSettings . serve thermoprintAPI . flip enter API.thermoprintServer =<< handlerNat printers
86
87withPrinters :: MonadResource m => Config -> [PrinterSpec] -> m Config
88-- ^ Helper for comfortably specifying a set of 'Printer's
89withPrinters cfg = fmap (\ps -> cfg { printers = printers cfg <> ps }) . foldlM (\ps p -> Map.insert (nextKey ps) <$> printer p <*> pure ps) Map.empty
90 where
91 nextKey :: Map PrinterId a -> PrinterId
92 nextKey (Map.keys -> keys)
93 | null keys = 0
94 | otherwise = succ $ maximum keys