diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/src/Thermoprint/Server.hs | 21 | ||||
-rw-r--r-- | server/src/Thermoprint/Server/Printer.hs | 39 | ||||
-rw-r--r-- | server/src/Thermoprint/Server/Printer/Debug.hs | 32 | ||||
-rw-r--r-- | server/thermoprint-server.cabal | 1 |
4 files changed, 83 insertions, 10 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 | ||
7 | module Thermoprint.Server | 8 | module 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) | |||
19 | import qualified Data.Map as Map | 20 | import qualified Data.Map as Map |
20 | 21 | ||
21 | import Data.Maybe (maybe) | 22 | import Data.Maybe (maybe) |
22 | import Data.Foldable (mapM_, forM_) | 23 | import Data.Foldable (mapM_, forM_, foldlM) |
24 | import Data.Monoid | ||
23 | 25 | ||
24 | import Control.Monad.Trans.Resource | 26 | import Control.Monad.Trans.Resource |
25 | import Control.Monad.Trans.Control | 27 | import Control.Monad.Trans.Control |
@@ -27,6 +29,8 @@ import Control.Monad.Logger | |||
27 | import Control.Monad.Reader | 29 | import Control.Monad.Reader |
28 | import Control.Monad.IO.Class | 30 | import Control.Monad.IO.Class |
29 | 31 | ||
32 | import Control.Monad.Writer | ||
33 | |||
30 | import Control.Concurrent | 34 | import Control.Concurrent |
31 | 35 | ||
32 | import Data.Text (Text) | 36 | import Data.Text (Text) |
@@ -62,9 +66,9 @@ instance Default Config where | |||
62 | 66 | ||
63 | 67 | ||
64 | thermoprintServer :: ( MonadLoggerIO m | 68 | thermoprintServer :: ( 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 | |||
87 | withPrinters :: MonadResource m => Config -> [PrinterSpec] -> m Config | ||
88 | -- ^ Helper for comfortably specifying a set of 'Printer's | ||
89 | withPrinters 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 | ||
diff --git a/server/src/Thermoprint/Server/Printer.hs b/server/src/Thermoprint/Server/Printer.hs index f34b2fa..cd12297 100644 --- a/server/src/Thermoprint/Server/Printer.hs +++ b/server/src/Thermoprint/Server/Printer.hs | |||
@@ -1,14 +1,18 @@ | |||
1 | {-# LANGUAGE RankNTypes #-} | 1 | {-# LANGUAGE RankNTypes #-} |
2 | {-# LANGUAGE ImpredicativeTypes #-} | ||
2 | {-# LANGUAGE MultiParamTypeClasses #-} | 3 | {-# LANGUAGE MultiParamTypeClasses #-} |
3 | {-# LANGUAGE FlexibleContexts #-} | 4 | {-# LANGUAGE FlexibleContexts #-} |
4 | {-# LANGUAGE RecordWildCards #-} | 5 | {-# LANGUAGE RecordWildCards #-} |
5 | {-# LANGUAGE OverloadedStrings #-} | 6 | {-# LANGUAGE OverloadedStrings #-} |
6 | {-# LANGUAGE TemplateHaskell #-} | 7 | {-# LANGUAGE TemplateHaskell #-} |
7 | {-# LANGUAGE StandaloneDeriving #-} | 8 | {-# LANGUAGE StandaloneDeriving #-} |
9 | {-# LANGUAGE ExistentialQuantification #-} | ||
10 | {-# LANGUAGE RecordWildCards #-} | ||
8 | {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} | 11 | {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} |
9 | 12 | ||
10 | module Thermoprint.Server.Printer | 13 | module Thermoprint.Server.Printer |
11 | ( Printer(..), printer | 14 | ( Printer(..), printer |
15 | , IsPrinter(..), PrinterSpec(..) | ||
12 | , Queue(..) | 16 | , Queue(..) |
13 | , runPrinter | 17 | , runPrinter |
14 | ) where | 18 | ) where |
@@ -41,8 +45,23 @@ import Control.Monad (forever) | |||
41 | 45 | ||
42 | import Control.Concurrent.STM | 46 | import Control.Concurrent.STM |
43 | 47 | ||
44 | data Printer = Printer | 48 | import Data.Default.Class |
45 | { print :: forall m. (MonadIO m) => Printout -> m (Maybe PrintingError) | 49 | |
50 | import Prelude hiding (print) | ||
51 | |||
52 | class IsPrinter a where | ||
53 | toMethod :: forall m. (MonadResource m) => a -> (forall m1. (MonadResource m1) => m (Printout -> m1 (Maybe PrintingError))) | ||
54 | |||
55 | instance (MonadResource m) => IsPrinter (Printer m) where | ||
56 | toMethod Printer{..} = return print | ||
57 | |||
58 | instance (MonadResource m) => IsPrinter (PrinterSpec m) where | ||
59 | toMethod (PS p) = toMethod p | ||
60 | |||
61 | data PrinterSpec m = forall p. IsPrinter p => PS p | ||
62 | |||
63 | data Printer m = Printer | ||
64 | { print :: Printout -> m (Maybe PrintingError) | ||
46 | , queue :: TVar Queue | 65 | , queue :: TVar Queue |
47 | } | 66 | } |
48 | 67 | ||
@@ -54,17 +73,25 @@ data Queue = Queue | |||
54 | } | 73 | } |
55 | deriving (Typeable, Generic, NFData) | 74 | deriving (Typeable, Generic, NFData) |
56 | 75 | ||
57 | printer :: (MonadIO m) => (forall m. (MonadIO m) => Printout -> m (Maybe PrintingError)) -> m Printer | 76 | instance Default Queue where |
58 | printer f = Printer f <$> liftIO (newTVarIO $ Queue Seq.empty Nothing Seq.empty) | 77 | def = Queue |
78 | { pending = Seq.empty | ||
79 | , current = Nothing | ||
80 | , history = Seq.empty | ||
81 | } | ||
82 | |||
83 | printer :: (MonadResource m, MonadResource m1, IsPrinter p) => p -> m (Printer m1) | ||
84 | -- ^ Version of 'Printer' handling the initialisation of the 'TVar' | ||
85 | printer p = Printer <$> toMethod p <*> liftIO (newTVarIO def) | ||
59 | 86 | ||
60 | atomically' :: MonadIO m => STM a -> m a | 87 | atomically' :: MonadIO m => STM a -> m a |
61 | atomically' = liftIO . atomically | 88 | atomically' = liftIO . atomically |
62 | 89 | ||
63 | runPrinter :: ( MonadReader ConnectionPool m | 90 | runPrinter :: ( MonadReader ConnectionPool m |
64 | , MonadIO m | ||
65 | , MonadLogger m | 91 | , MonadLogger m |
92 | , MonadResource m | ||
66 | , MonadBaseControl IO m | 93 | , MonadBaseControl IO m |
67 | ) => Printer -> m () | 94 | ) => Printer m -> m () |
68 | -- ^ Loop 'forever' pushing entries from the 'Queue' associated to a 'Printer' into its 'print'-method | 95 | -- ^ Loop 'forever' pushing entries from the 'Queue' associated to a 'Printer' into its 'print'-method |
69 | runPrinter Printer{..} = forever $ do | 96 | runPrinter Printer{..} = forever $ do |
70 | jobId <- atomically' $ do | 97 | jobId <- atomically' $ do |
diff --git a/server/src/Thermoprint/Server/Printer/Debug.hs b/server/src/Thermoprint/Server/Printer/Debug.hs new file mode 100644 index 0000000..81e43a3 --- /dev/null +++ b/server/src/Thermoprint/Server/Printer/Debug.hs | |||
@@ -0,0 +1,32 @@ | |||
1 | {-# LANGUAGE EmptyDataDecls #-} | ||
2 | {-# LANGUAGE OverloadedStrings #-} | ||
3 | {-# LANGUAGE RankNTypes #-} | ||
4 | |||
5 | module Thermoprint.Server.Printer.Debug | ||
6 | ( Debug | ||
7 | ) where | ||
8 | |||
9 | import Control.Monad.IO.Class | ||
10 | import Control.Monad.Trans.Resource | ||
11 | |||
12 | import Data.Text.Lazy (Text) | ||
13 | import qualified Data.Text.Lazy as TL | ||
14 | import qualified Data.Text.Lazy.IO as TL | ||
15 | |||
16 | import Thermoprint.Printout | ||
17 | import Thermoprint.Server.Printer | ||
18 | |||
19 | import Data.List (intersperse) | ||
20 | import Data.Foldable (toList) | ||
21 | import Data.Monoid | ||
22 | |||
23 | data Debug | ||
24 | |||
25 | -- instance IsPrinter Debug where | ||
26 | -- toMethod _ = (>> return Nothing) . liftIO . TL.putStrLn . cotext' | ||
27 | |||
28 | -- cotext' :: Printout -> Text | ||
29 | -- cotext' = mconcat . intersperse "\n\n" . map (mconcat . map cotext'' . toList) . toList | ||
30 | -- where | ||
31 | -- cotext'' (Cooked b) = cotext b | ||
32 | -- cotext'' (Raw _) = "[Raw]" | ||
diff --git a/server/thermoprint-server.cabal b/server/thermoprint-server.cabal index 185a0f3..dd495c0 100644 --- a/server/thermoprint-server.cabal +++ b/server/thermoprint-server.cabal | |||
@@ -21,6 +21,7 @@ library | |||
21 | , Thermoprint.Server.Database | 21 | , Thermoprint.Server.Database |
22 | , Thermoprint.Server.API | 22 | , Thermoprint.Server.API |
23 | , Thermoprint.Server.Printer | 23 | , Thermoprint.Server.Printer |
24 | , Thermoprint.Server.Printer.Debug | ||
24 | other-modules: Thermoprint.Server.Database.Instances | 25 | other-modules: Thermoprint.Server.Database.Instances |
25 | -- other-extensions: | 26 | -- other-extensions: |
26 | build-depends: base >=4.8 && <5 | 27 | build-depends: base >=4.8 && <5 |