diff options
Diffstat (limited to 'server/src/Thermoprint/Server/API.hs')
| -rw-r--r-- | server/src/Thermoprint/Server/API.hs | 27 |
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 | ||
| 5 | module Thermoprint.Server.API | 6 | module Thermoprint.Server.API |
| 6 | ( ProtoHandler, Handler | 7 | ( ProtoHandler, Handler |
| @@ -11,7 +12,8 @@ module Thermoprint.Server.API | |||
| 11 | import Thermoprint.API hiding (JobId(..), DraftId(..)) | 12 | import Thermoprint.API hiding (JobId(..), DraftId(..)) |
| 12 | import qualified Thermoprint.API as API (JobId(..), DraftId(..)) | 13 | import qualified Thermoprint.API as API (JobId(..), DraftId(..)) |
| 13 | 14 | ||
| 14 | import Thermoprint.Server.Printer (Printer(..), Queue(..)) | 15 | import Thermoprint.Server.Printer |
| 16 | import Thermoprint.Server.Database | ||
| 15 | 17 | ||
| 16 | import Data.Set (Set) | 18 | import Data.Set (Set) |
| 17 | import qualified Data.Set as Set | 19 | import qualified Data.Set as Set |
| @@ -26,6 +28,7 @@ import Servant.Server.Internal.Enter | |||
| 26 | 28 | ||
| 27 | import Control.Monad.Logger | 29 | import Control.Monad.Logger |
| 28 | import Control.Monad.Reader | 30 | import Control.Monad.Reader |
| 31 | import Control.Monad.Trans.Resource | ||
| 29 | import Control.Monad.Trans.Either | 32 | import Control.Monad.Trans.Either |
| 30 | import Control.Monad.IO.Class | 33 | import Control.Monad.IO.Class |
| 31 | 34 | ||
| @@ -40,7 +43,7 @@ import Data.Traversable (mapM) | |||
| 40 | import Database.Persist | 43 | import Database.Persist |
| 41 | import Database.Persist.Sql | 44 | import Database.Persist.Sql |
| 42 | 45 | ||
| 43 | type ProtoHandler = ReaderT HandlerInput (LoggingT IO) | 46 | type ProtoHandler = ReaderT HandlerInput (LoggingT (ResourceT IO)) |
| 44 | type Handler = EitherT ServantErr ProtoHandler | 47 | type 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 | ||
| 69 | thermoprintServer :: ServerT ThermoprintAPI Handler | 72 | thermoprintServer :: ServerT ThermoprintAPI Handler |
| @@ -79,6 +82,16 @@ thermoprintServer = listPrinters | |||
| 79 | (<||>) = liftM2 (:<|>) | 82 | (<||>) = liftM2 (:<|>) |
| 80 | infixr 9 <||> | 83 | infixr 9 <||> |
| 81 | 84 | ||
| 85 | lookupPrinter :: Maybe PrinterId -> Handler Printer | ||
| 86 | lookupPrinter 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 | |||
| 82 | listPrinters :: Handler (Map PrinterId PrinterStatus) | 95 | listPrinters :: Handler (Map PrinterId PrinterStatus) |
| 83 | listPrinters = fmap toStatus <$> (mapM (liftIO . readTVarIO . queue) . printers =<< ask) | 96 | listPrinters = 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 | ||
| 88 | queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId | 101 | queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId |
| 89 | queueJob = return undefined | 102 | queueJob pId printout = lift . fmap (castId' . unJobKey) . withReaderT sqlPool . addToQueue printout =<< lookupPrinter pId |
| 90 | 103 | ||
| 91 | printerStatus :: PrinterId -> Handler PrinterStatus | 104 | printerStatus :: PrinterId -> Handler PrinterStatus |
| 92 | printerStatus = return undefined | 105 | printerStatus = return undefined |
