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.hs27
1 files changed, 20 insertions, 7 deletions
diff --git a/server/src/Thermoprint/Server/API.hs b/server/src/Thermoprint/Server/API.hs
index 6a92caf..a1efb8f 100644
--- a/server/src/Thermoprint/Server/API.hs
+++ b/server/src/Thermoprint/Server/API.hs
@@ -1,6 +1,7 @@
1{-# LANGUAGE TypeOperators #-} 1{-# LANGUAGE TypeOperators #-}
2{-# LANGUAGE FlexibleContexts #-} 2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE TemplateHaskell #-} 3{-# LANGUAGE TemplateHaskell #-}
4{-# LANGUAGE OverloadedStrings #-}
4 5
5module Thermoprint.Server.API 6module Thermoprint.Server.API
6 ( ProtoHandler, Handler 7 ( ProtoHandler, Handler
@@ -11,7 +12,8 @@ module Thermoprint.Server.API
11import Thermoprint.API hiding (JobId(..), DraftId(..)) 12import Thermoprint.API hiding (JobId(..), DraftId(..))
12import qualified Thermoprint.API as API (JobId(..), DraftId(..)) 13import qualified Thermoprint.API as API (JobId(..), DraftId(..))
13 14
14import Thermoprint.Server.Printer (Printer(..), Queue(..)) 15import Thermoprint.Server.Printer
16import Thermoprint.Server.Database
15 17
16import Data.Set (Set) 18import Data.Set (Set)
17import qualified Data.Set as Set 19import qualified Data.Set as Set
@@ -26,6 +28,7 @@ import Servant.Server.Internal.Enter
26 28
27import Control.Monad.Logger 29import Control.Monad.Logger
28import Control.Monad.Reader 30import Control.Monad.Reader
31import Control.Monad.Trans.Resource
29import Control.Monad.Trans.Either 32import Control.Monad.Trans.Either
30import Control.Monad.IO.Class 33import Control.Monad.IO.Class
31 34
@@ -40,7 +43,7 @@ import Data.Traversable (mapM)
40import Database.Persist 43import Database.Persist
41import Database.Persist.Sql 44import Database.Persist.Sql
42 45
43type ProtoHandler = ReaderT HandlerInput (LoggingT IO) 46type ProtoHandler = ReaderT HandlerInput (LoggingT (ResourceT IO))
44type Handler = EitherT ServantErr ProtoHandler 47type Handler = EitherT ServantErr ProtoHandler
45 48
46-- ^ Runtime configuration of our handlers 49-- ^ Runtime configuration of our handlers
@@ -63,7 +66,7 @@ handlerNat printerMap = do
63 , printers = printerMap 66 , printers = printerMap
64 } 67 }
65 protoNat :: ProtoHandler :~> IO 68 protoNat :: ProtoHandler :~> IO
66 protoNat = Nat (($ logFunc) . runLoggingT) . runReaderTNat handlerInput 69 protoNat = Nat runResourceT . Nat (($ logFunc) . runLoggingT) . runReaderTNat handlerInput
67 return $ hoistNat protoNat 70 return $ hoistNat protoNat
68 71
69thermoprintServer :: ServerT ThermoprintAPI Handler 72thermoprintServer :: ServerT ThermoprintAPI Handler
@@ -79,6 +82,16 @@ thermoprintServer = listPrinters
79 (<||>) = liftM2 (:<|>) 82 (<||>) = liftM2 (:<|>)
80 infixr 9 <||> 83 infixr 9 <||>
81 84
85lookupPrinter :: Maybe PrinterId -> Handler Printer
86lookupPrinter pId = asks printers >>= maybePrinter' pId
87 where
88 maybePrinter' Nothing printerMap
89 | Map.null printerMap = left $ err501 { errBody = "No printers available" }
90 | otherwise = return . snd $ Map.findMin printerMap
91 maybePrinter (Just pId) printerMap
92 | Just printer <- Map.lookup pId printerMap = return printer
93 | otherwise = left $ err404 { errBody = "No such printer" }
94
82listPrinters :: Handler (Map PrinterId PrinterStatus) 95listPrinters :: Handler (Map PrinterId PrinterStatus)
83listPrinters = fmap toStatus <$> (mapM (liftIO . readTVarIO . queue) . printers =<< ask) 96listPrinters = fmap toStatus <$> (mapM (liftIO . readTVarIO . queue) . printers =<< ask)
84 where 97 where
@@ -86,7 +99,7 @@ listPrinters = fmap toStatus <$> (mapM (liftIO . readTVarIO . queue) . printers
86 toStatus (Queue _ (Just id) _) = Busy . castId $ fromSqlKey id 99 toStatus (Queue _ (Just id) _) = Busy . castId $ fromSqlKey id
87 100
88queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId 101queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId
89queueJob = return undefined 102queueJob pId printout = lift . fmap (castId' . unJobKey) . withReaderT sqlPool . addToQueue printout =<< lookupPrinter pId
90 103
91printerStatus :: PrinterId -> Handler PrinterStatus 104printerStatus :: PrinterId -> Handler PrinterStatus
92printerStatus = return undefined 105printerStatus = return undefined