diff options
Diffstat (limited to 'server/src/Thermoprint/Server.hs')
-rw-r--r-- | server/src/Thermoprint/Server.hs | 16 |
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 | ||
14 | import Data.Default.Class | 15 | import Data.Default.Class |
15 | import qualified Config.Dyre as Dyre | 16 | import qualified Config.Dyre as Dyre |
16 | 17 | ||
17 | import Control.Monad (mapM_) | 18 | import Data.Map (Map) |
19 | import qualified Data.Map as Map | ||
18 | 20 | ||
19 | import Data.Maybe (maybe) | 21 | import Data.Maybe (maybe) |
22 | import Data.Foldable (mapM_, forM_) | ||
20 | 23 | ||
21 | import Control.Monad.Trans.Resource | 24 | import Control.Monad.Trans.Resource |
25 | import Control.Monad.Trans.Control | ||
22 | import Control.Monad.Logger | 26 | import Control.Monad.Logger |
23 | import Control.Monad.Reader | 27 | import Control.Monad.Reader |
24 | import Control.Monad.IO.Class | 28 | import Control.Monad.IO.Class |
29 | |||
30 | import Control.Concurrent | ||
25 | 31 | ||
26 | import Data.Text (Text) | 32 | import Data.Text (Text) |
27 | import qualified Data.Text as T (pack) | 33 | import qualified Data.Text as T (pack) |
@@ -35,20 +41,23 @@ import Servant.Server.Internal.Enter (enter, (:~>)(..)) | |||
35 | import Database.Persist.Sql (runMigrationSilent, ConnectionPool, runSqlPool) | 41 | import Database.Persist.Sql (runMigrationSilent, ConnectionPool, runSqlPool) |
36 | 42 | ||
37 | 43 | ||
38 | import Thermoprint.API (thermoprintAPI) | 44 | import Thermoprint.API (thermoprintAPI, PrinterId) |
39 | 45 | ||
40 | import Thermoprint.Server.Database | 46 | import Thermoprint.Server.Database |
47 | import Thermoprint.Server.Printer | ||
41 | import qualified Thermoprint.Server.API as API (thermoprintServer) | 48 | import qualified Thermoprint.Server.API as API (thermoprintServer) |
42 | import Thermoprint.Server.API hiding (thermoprintServer) | 49 | import Thermoprint.Server.API hiding (thermoprintServer) |
43 | 50 | ||
44 | -- | Compile-time configuration for 'thermoprintServer' | 51 | -- | Compile-time configuration for 'thermoprintServer' |
45 | data Config = Config { dyreError :: Maybe String -- ^ Set by 'Dyre' -- sent to log as an error | 52 | data 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 | ||
49 | instance Default Config where | 57 | instance 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 | ||