1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TemplateHaskell #-}
module Thermoprint.Server.API
( ProtoHandler, Handler
, thermoprintServer
, handlerNat
) where
import Thermoprint.API hiding (JobId(..), DraftId(..))
import qualified Thermoprint.API as API (JobId(..), DraftId(..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Map (Map)
import qualified Data.Map as Map
import Servant
import Servant.Server
import Servant.Server.Internal.Enter
import Control.Monad.Logger
import Control.Monad.Reader
import Control.Monad.Trans.Either
import Control.Monad.IO.Class
import Control.Monad ((<=<), liftM2)
import Prelude hiding ((.), id)
import Control.Category
import Database.Persist
import Database.Persist.Sql
type ProtoHandler = ReaderT HandlerInput (LoggingT IO)
type Handler = EitherT ServantErr ProtoHandler
-- ^ Runtime configuration of our handlers
data HandlerInput = HandlerInput { sqlPool :: ConnectionPool -- ^ How to interact with 'persistent' storage
}
handlerNat :: ( MonadReader ConnectionPool m
, MonadLoggerIO m
) => m (Handler :~> EitherT ServantErr IO)
-- ^ Servant requires its handlers to be 'EitherT ServantErr IO'
--
-- This generates a 'Nat'ural transformation for squashing the monad-transformer-stack we use in our handlers to the monad 'servant-server' wants
handlerNat = do
sqlPool <- ask
logFunc <- askLoggerIO
let
handlerInput = HandlerInput
{ sqlPool = sqlPool
}
protoNat :: ProtoHandler :~> IO
protoNat = Nat (($ logFunc) . runLoggingT) . runReaderTNat handlerInput
return $ hoistNat protoNat
thermoprintServer :: ServerT ThermoprintAPI Handler
-- ^ A 'servant-server' for 'ThermoprintAPI'
thermoprintServer = listPrinters
:<|> (listJobs :<|> queueJob)
:<|> getJob <||> jobStatus <||> deleteJob
:<|> (listDrafts :<|> addDraft)
:<|> updateDraft <||> getDraft <||> deleteDraft <||> printDraft
where
-- :: (a -> b) -> (a -> c) -> (a -> b :<|> c)
(<||>) :: Monad m => m a -> m b -> m (a :<|> b)
(<||>) = liftM2 (:<|>)
infixr 9 <||>
listPrinters :: Handler (Map PrinterId PrinterStatus)
listPrinters = return $ Map.fromList [(1, Available), (7, Available), (3, Available)]
queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId
queueJob = return undefined
printerStatus :: PrinterId -> Handler PrinterStatus
printerStatus = return undefined
listJobs :: Maybe PrinterId -> Maybe API.JobId -> Maybe API.JobId -> Handler (Seq (API.JobId, JobStatus))
listJobs = return undefined
getJob :: API.JobId -> Handler Printout
getJob = return undefined
jobStatus :: API.JobId -> Handler JobStatus
jobStatus = return undefined
deleteJob :: API.JobId -> Handler ()
deleteJob = return undefined
listDrafts :: Handler (Map API.DraftId (Maybe DraftTitle))
listDrafts = return undefined
addDraft :: Maybe DraftTitle -> Printout -> Handler API.DraftId
addDraft = return undefined
updateDraft :: API.DraftId -> Maybe DraftTitle -> Printout -> Handler ()
updateDraft = return undefined
getDraft :: API.DraftId -> Handler (Maybe DraftTitle, Printout)
getDraft = return undefined
deleteDraft :: API.DraftId -> Handler ()
deleteDraft = return undefined
printDraft :: API.DraftId -> Maybe PrinterId -> Handler API.JobId
printDraft = return undefined
|