aboutsummaryrefslogtreecommitdiff
path: root/server/src/Thermoprint/Server/API.hs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/Thermoprint/Server/API.hs')
-rw-r--r--server/src/Thermoprint/Server/API.hs24
1 files changed, 18 insertions, 6 deletions
diff --git a/server/src/Thermoprint/Server/API.hs b/server/src/Thermoprint/Server/API.hs
index 6411a70..6a92caf 100644
--- a/server/src/Thermoprint/Server/API.hs
+++ b/server/src/Thermoprint/Server/API.hs
@@ -10,6 +10,9 @@ module Thermoprint.Server.API
10 10
11import Thermoprint.API hiding (JobId(..), DraftId(..)) 11import Thermoprint.API hiding (JobId(..), DraftId(..))
12import qualified Thermoprint.API as API (JobId(..), DraftId(..)) 12import qualified Thermoprint.API as API (JobId(..), DraftId(..))
13
14import Thermoprint.Server.Printer (Printer(..), Queue(..))
15
13import Data.Set (Set) 16import Data.Set (Set)
14import qualified Data.Set as Set 17import qualified Data.Set as Set
15import Data.Sequence (Seq) 18import Data.Sequence (Seq)
@@ -26,10 +29,14 @@ import Control.Monad.Reader
26import Control.Monad.Trans.Either 29import Control.Monad.Trans.Either
27import Control.Monad.IO.Class 30import Control.Monad.IO.Class
28 31
32import Control.Concurrent.STM
33
29import Control.Monad ((<=<), liftM2) 34import Control.Monad ((<=<), liftM2)
30import Prelude hiding ((.), id) 35import Prelude hiding ((.), id, mapM)
31import Control.Category 36import Control.Category
32 37
38import Data.Traversable (mapM)
39
33import Database.Persist 40import Database.Persist
34import Database.Persist.Sql 41import Database.Persist.Sql
35 42
@@ -37,21 +44,23 @@ type ProtoHandler = ReaderT HandlerInput (LoggingT IO)
37type Handler = EitherT ServantErr ProtoHandler 44type Handler = EitherT ServantErr ProtoHandler
38 45
39-- ^ Runtime configuration of our handlers 46-- ^ Runtime configuration of our handlers
40data HandlerInput = HandlerInput { sqlPool :: ConnectionPool -- ^ How to interact with 'persistent' storage 47data HandlerInput = HandlerInput { sqlPool :: ConnectionPool -- ^ How to interact with 'persistent' storage
48 , printers :: Map PrinterId Printer
41 } 49 }
42 50
43handlerNat :: ( MonadReader ConnectionPool m 51handlerNat :: ( MonadReader ConnectionPool m
44 , MonadLoggerIO m 52 , MonadLoggerIO m
45 ) => m (Handler :~> EitherT ServantErr IO) 53 ) => Map PrinterId Printer -> m (Handler :~> EitherT ServantErr IO)
46-- ^ Servant requires its handlers to be 'EitherT ServantErr IO' 54-- ^ Servant requires its handlers to be 'EitherT ServantErr IO'
47-- 55--
48-- This generates a 'Nat'ural transformation for squashing the monad-transformer-stack we use in our handlers to the monad 'servant-server' wants 56-- This generates a 'Nat'ural transformation for squashing the monad-transformer-stack we use in our handlers to the monad 'servant-server' wants
49handlerNat = do 57handlerNat printerMap = do
50 sqlPool <- ask 58 sqlPool <- ask
51 logFunc <- askLoggerIO 59 logFunc <- askLoggerIO
52 let 60 let
53 handlerInput = HandlerInput 61 handlerInput = HandlerInput
54 { sqlPool = sqlPool 62 { sqlPool = sqlPool
63 , printers = printerMap
55 } 64 }
56 protoNat :: ProtoHandler :~> IO 65 protoNat :: ProtoHandler :~> IO
57 protoNat = Nat (($ logFunc) . runLoggingT) . runReaderTNat handlerInput 66 protoNat = Nat (($ logFunc) . runLoggingT) . runReaderTNat handlerInput
@@ -71,7 +80,10 @@ thermoprintServer = listPrinters
71 infixr 9 <||> 80 infixr 9 <||>
72 81
73listPrinters :: Handler (Map PrinterId PrinterStatus) 82listPrinters :: Handler (Map PrinterId PrinterStatus)
74listPrinters = return $ Map.fromList [(1, Available), (7, Available), (3, Available)] 83listPrinters = fmap toStatus <$> (mapM (liftIO . readTVarIO . queue) . printers =<< ask)
84 where
85 toStatus (Queue _ Nothing _) = Available
86 toStatus (Queue _ (Just id) _) = Busy . castId $ fromSqlKey id
75 87
76queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId 88queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId
77queueJob = return undefined 89queueJob = return undefined