diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-21 08:44:04 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-21 08:44:04 +0000 |
commit | c51e334bc5a537300d9421f43bd355850e2013b4 (patch) | |
tree | a0385ef4a26b4a8be7d31efb4b7d2056e9d247df /server | |
parent | 79a01c5fe636dad60338e4847be8b3cbf3716192 (diff) | |
download | thermoprint-c51e334bc5a537300d9421f43bd355850e2013b4.tar thermoprint-c51e334bc5a537300d9421f43bd355850e2013b4.tar.gz thermoprint-c51e334bc5a537300d9421f43bd355850e2013b4.tar.bz2 thermoprint-c51e334bc5a537300d9421f43bd355850e2013b4.tar.xz thermoprint-c51e334bc5a537300d9421f43bd355850e2013b4.zip |
Fixed Nats & new API
Diffstat (limited to 'server')
-rw-r--r-- | server/src/Thermoprint/Server.hs | 95 |
1 files changed, 37 insertions, 58 deletions
diff --git a/server/src/Thermoprint/Server.hs b/server/src/Thermoprint/Server.hs index 4018d17..eff6c3a 100644 --- a/server/src/Thermoprint/Server.hs +++ b/server/src/Thermoprint/Server.hs | |||
@@ -41,6 +41,8 @@ import Data.Set (Set) | |||
41 | import qualified Data.Set as Set | 41 | import qualified Data.Set as Set |
42 | import Data.Sequence (Seq) | 42 | import Data.Sequence (Seq) |
43 | import qualified Data.Sequence as Seq | 43 | import qualified Data.Sequence as Seq |
44 | import Data.Sequence (Map) | ||
45 | import qualified Data.Sequence as Map | ||
44 | 46 | ||
45 | import qualified Network.Wai.Handler.Warp as Warp | 47 | import qualified Network.Wai.Handler.Warp as Warp |
46 | import Network.Wai (Application) | 48 | import Network.Wai (Application) |
@@ -62,11 +64,16 @@ instance Default Config where | |||
62 | , warpSettings = Warp.defaultSettings | 64 | , warpSettings = Warp.defaultSettings |
63 | } | 65 | } |
64 | 66 | ||
67 | data HandlerInput = HandlerInput { sqlPool :: ConnectionPool | ||
68 | } | ||
69 | |||
65 | 70 | ||
66 | share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| | 71 | share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| |
67 | Job | 72 | Job |
73 | printer PrinterId | ||
68 | content Printout | 74 | content Printout |
69 | Draft | 75 | Draft |
76 | title DraftTitle Maybe | ||
70 | content Printout | 77 | content Printout |
71 | |] | 78 | |] |
72 | 79 | ||
@@ -90,93 +97,65 @@ thermoprintServer io = Dyre.wrapMain $ Dyre.defaultParams | |||
90 | | otherwise = return cfg | 97 | | otherwise = return cfg |
91 | 98 | ||
92 | realMain (Config{..}) = enter io $ do | 99 | realMain (Config{..}) = enter io $ do |
93 | runSqlPool' (runMigrationSilent migrateAll) >>= mapM_ $(logWarn) | 100 | sqlPool <- ask |
94 | liftIO . Warp.runSettings warpSettings . serve thermoprintAPI $ enter (hoistNat io) thermoprintServer' | 101 | runSqlPool (runMigrationSilent migrateAll) sqlPool >>= mapM_ $(logWarn) |
95 | 102 | let | |
96 | 103 | handlerInput = HandlerInput | |
97 | runSqlPool' :: ( MonadBaseControl IO m | 104 | { sqlPool = sqlPool |
98 | , MonadReader ConnectionPool m | 105 | } |
99 | ) => SqlPersistT m a -> m a | 106 | io' :: ReaderT HandlerInput IO :~> IO |
100 | runSqlPool' a = runSqlPool a =<< ask | 107 | io' = runReaderTNat handlerInput |
108 | liftIO . Warp.runSettings warpSettings . serve thermoprintAPI $ enter (hoistNat io') thermoprintServer' | ||
109 | |||
110 | type Handler = EitherT ServantErr (ReaderT HandlerInput IO) | ||
101 | 111 | ||
102 | (<||>) :: Monad m => m a -> m b -> m (a :<|> b) | 112 | (<||>) :: Monad m => m a -> m b -> m (a :<|> b) |
103 | (<||>) = liftM2 (:<|>) | 113 | (<||>) = liftM2 (:<|>) |
104 | infixr 9 <||> | 114 | infixr 9 <||> |
105 | 115 | ||
106 | 116 | thermoprintServer' :: ServerT ThermoprintAPI Handler | |
107 | thermoprintServer' :: ( Monad m | ||
108 | ) => ServerT ThermoprintAPI (EitherT ServantErr m) | ||
109 | thermoprintServer' = listPrinters | 117 | thermoprintServer' = listPrinters |
110 | :<|> queueJob <||> printerStatus | 118 | :<|> listJobs <||> queueJob |
111 | :<|> listJobs | 119 | :<|> getJob <||> jobStatus <||> deleteJob |
112 | :<|> getJob <||> jobStatus <||> getJobPrinter <||> deleteJob | ||
113 | :<|> (listDrafts :<|> addDraft) | 120 | :<|> (listDrafts :<|> addDraft) |
114 | :<|> updateDraft <||> getDraft <||> deleteDraft | 121 | :<|> updateDraft <||> getDraft <||> deleteDraft <||> printDraft |
115 | 122 | ||
116 | 123 | ||
117 | listPrinters :: ( Monad m | 124 | listPrinters :: Handler (Set PrinterId) |
118 | ) => EitherT ServantErr m (Set PrinterId) | ||
119 | listPrinters = return Set.empty | 125 | listPrinters = return Set.empty |
120 | 126 | ||
121 | queueJob :: ( Monad m | 127 | queueJob :: Maybe PrinterId -> Printout -> Handler API.JobId |
122 | ) => PrinterId | ||
123 | -> Printout | ||
124 | -> EitherT ServantErr m API.JobId | ||
125 | queueJob = return undefined | 128 | queueJob = return undefined |
126 | 129 | ||
127 | printerStatus :: ( Monad m | 130 | printerStatus :: PrinterId -> Handler PrinterStatus |
128 | ) => PrinterId | ||
129 | -> EitherT ServantErr m PrinterStatus | ||
130 | printerStatus = return undefined | 131 | printerStatus = return undefined |
131 | 132 | ||
132 | listJobs :: ( Monad m | 133 | listJobs :: Maybe PrinterId -> Maybe API.JobId -> Maybe API.JobId -> Handler (Seq (API.JobId, JobStatus)) |
133 | ) => Maybe PrinterId | ||
134 | -> Maybe API.JobId | ||
135 | -> Maybe API.JobId | ||
136 | -> EitherT ServantErr m (Seq API.JobId) | ||
137 | listJobs = return undefined | 134 | listJobs = return undefined |
138 | 135 | ||
139 | getJob :: ( Monad m | 136 | getJob :: API.JobId -> Handler Printout |
140 | ) => API.JobId | ||
141 | -> EitherT ServantErr m Printout | ||
142 | getJob = return undefined | 137 | getJob = return undefined |
143 | 138 | ||
144 | jobStatus :: ( Monad m | 139 | jobStatus :: API.JobId -> Handler JobStatus |
145 | ) => API.JobId | ||
146 | -> EitherT ServantErr m JobStatus | ||
147 | jobStatus = return undefined | 140 | jobStatus = return undefined |
148 | 141 | ||
149 | getJobPrinter :: ( Monad m | 142 | deleteJob :: API.JobId -> Handler () |
150 | ) => API.JobId | ||
151 | -> EitherT ServantErr m PrinterId | ||
152 | getJobPrinter = return undefined | ||
153 | |||
154 | deleteJob :: ( Monad m | ||
155 | ) => API.JobId | ||
156 | -> EitherT ServantErr m () | ||
157 | deleteJob = return undefined | 143 | deleteJob = return undefined |
158 | 144 | ||
159 | listDrafts :: ( Monad m | 145 | listDrafts :: Handler (Map API.DraftId (Maybe DraftTitle)) |
160 | ) => EitherT ServantErr m (Set API.DraftId) | ||
161 | listDrafts = return undefined | 146 | listDrafts = return undefined |
162 | 147 | ||
163 | addDraft :: ( Monad m | 148 | addDraft :: Maybe DraftTitle -> Printout -> Handler API.DraftId |
164 | ) => Printout | ||
165 | -> EitherT ServantErr m API.DraftId | ||
166 | addDraft = return undefined | 149 | addDraft = return undefined |
167 | 150 | ||
168 | updateDraft :: ( Monad m | 151 | updateDraft :: API.DraftId -> Maybe DraftTitle -> Printout -> Handler () |
169 | ) => API.DraftId | ||
170 | -> Printout | ||
171 | -> EitherT ServantErr m () | ||
172 | updateDraft = return undefined | 152 | updateDraft = return undefined |
173 | 153 | ||
174 | getDraft :: ( Monad m | 154 | getDraft :: API.DraftId -> Handler (Maybe DraftTitle, Printout) |
175 | ) => API.DraftId | ||
176 | -> EitherT ServantErr m Printout | ||
177 | getDraft = return undefined | 155 | getDraft = return undefined |
178 | 156 | ||
179 | deleteDraft :: ( Monad m | 157 | deleteDraft :: API.DraftId -> Handler () |
180 | ) => API.DraftId | ||
181 | -> EitherT ServantErr m () | ||
182 | deleteDraft = return undefined | 158 | deleteDraft = return undefined |
159 | |||
160 | printDraft :: API.DraftId -> Maybe PrinterId -> Handler API.JobId | ||
161 | printDraft = return undefined | ||