aboutsummaryrefslogtreecommitdiff
path: root/server/src/Thermoprint/Server.hs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Thermoprint/Server.hs')
-rw-r--r--server/src/Thermoprint/Server.hs16
1 files changed, 13 insertions, 3 deletions
diff --git a/server/src/Thermoprint/Server.hs b/server/src/Thermoprint/Server.hs
index 0d96de0..39bf0a1 100644
--- a/server/src/Thermoprint/Server.hs
+++ b/server/src/Thermoprint/Server.hs
@@ -9,19 +9,25 @@ module Thermoprint.Server
9 , Config(..) 9 , Config(..)
10 , module Data.Default.Class 10 , module Data.Default.Class
11 , module Servant.Server.Internal.Enter 11 , module Servant.Server.Internal.Enter
12 , module Thermoprint.Server.Printer
12 ) where 13 ) where
13 14
14import Data.Default.Class 15import Data.Default.Class
15import qualified Config.Dyre as Dyre 16import qualified Config.Dyre as Dyre
16 17
17import Control.Monad (mapM_) 18import Data.Map (Map)
19import qualified Data.Map as Map
18 20
19import Data.Maybe (maybe) 21import Data.Maybe (maybe)
22import Data.Foldable (mapM_, forM_)
20 23
21import Control.Monad.Trans.Resource 24import Control.Monad.Trans.Resource
25import Control.Monad.Trans.Control
22import Control.Monad.Logger 26import Control.Monad.Logger
23import Control.Monad.Reader 27import Control.Monad.Reader
24import Control.Monad.IO.Class 28import Control.Monad.IO.Class
29
30import Control.Concurrent
25 31
26import Data.Text (Text) 32import Data.Text (Text)
27import qualified Data.Text as T (pack) 33import qualified Data.Text as T (pack)
@@ -35,20 +41,23 @@ import Servant.Server.Internal.Enter (enter, (:~>)(..))
35import Database.Persist.Sql (runMigrationSilent, ConnectionPool, runSqlPool) 41import Database.Persist.Sql (runMigrationSilent, ConnectionPool, runSqlPool)
36 42
37 43
38import Thermoprint.API (thermoprintAPI) 44import Thermoprint.API (thermoprintAPI, PrinterId)
39 45
40import Thermoprint.Server.Database 46import Thermoprint.Server.Database
47import Thermoprint.Server.Printer
41import qualified Thermoprint.Server.API as API (thermoprintServer) 48import qualified Thermoprint.Server.API as API (thermoprintServer)
42import Thermoprint.Server.API hiding (thermoprintServer) 49import Thermoprint.Server.API hiding (thermoprintServer)
43 50
44-- | Compile-time configuration for 'thermoprintServer' 51-- | Compile-time configuration for 'thermoprintServer'
45data Config = Config { dyreError :: Maybe String -- ^ Set by 'Dyre' -- sent to log as an error 52data Config = Config { dyreError :: Maybe String -- ^ Set by 'Dyre' -- sent to log as an error
46 , warpSettings :: Warp.Settings -- ^ Configure 'Warp's behaviour 53 , warpSettings :: Warp.Settings -- ^ Configure 'Warp's behaviour
54 , printers :: Map PrinterId Printer
47 } 55 }
48 56
49instance Default Config where 57instance Default Config where
50 def = Config { dyreError = Nothing 58 def = Config { dyreError = Nothing
51 , warpSettings = Warp.defaultSettings 59 , warpSettings = Warp.defaultSettings
60 , printers = Map.empty
52 } 61 }
53 62
54 63
@@ -68,4 +77,5 @@ thermoprintServer io = Dyre.wrapMain $ Dyre.defaultParams
68 realMain Config{..} = unNat io $ do 77 realMain Config{..} = unNat io $ do
69 maybe (return ()) ($(logErrorS) "Dyre" . T.pack) dyreError 78 maybe (return ()) ($(logErrorS) "Dyre" . T.pack) dyreError
70 mapM_ ($(logWarnS) "DB") =<< runSqlPool (runMigrationSilent migrateAll) =<< ask 79 mapM_ ($(logWarnS) "DB") =<< runSqlPool (runMigrationSilent migrateAll) =<< ask
71 liftIO . Warp.runSettings warpSettings . serve thermoprintAPI . flip enter API.thermoprintServer =<< handlerNat 80 forM_ printers $ liftBaseDiscard forkIO . runPrinter
81 liftIO . Warp.runSettings warpSettings . serve thermoprintAPI . flip enter API.thermoprintServer =<< handlerNat printers