diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-10-17 21:23:45 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-10-17 21:23:45 +0200 |
commit | e65e1eaac335a4738abb9e8ee8da7a229f96c2c0 (patch) | |
tree | 8711caffd49f24ee8136523e0aefc76e37d8666a /servant/src | |
parent | 005dc408dc09c3b479398ebe3e92efa2cd54846e (diff) | |
download | thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar.gz thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar.bz2 thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar.xz thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.zip |
Drafts
Diffstat (limited to 'servant/src')
-rw-r--r-- | servant/src/Main.hs | 105 | ||||
-rw-r--r-- | servant/src/PrintOut.hs | 14 |
2 files changed, 110 insertions, 9 deletions
diff --git a/servant/src/Main.hs b/servant/src/Main.hs index 9d88559..0aa9eeb 100644 --- a/servant/src/Main.hs +++ b/servant/src/Main.hs | |||
@@ -1,27 +1,59 @@ | |||
1 | {-# LANGUAGE RecordWildCards, OverloadedStrings #-} | 1 | {-# LANGUAGE RecordWildCards, OverloadedStrings #-} |
2 | {-# LANGUAGE EmptyDataDecls #-} | ||
3 | {-# LANGUAGE FlexibleContexts #-} | ||
4 | {-# LANGUAGE GADTs #-} | ||
5 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
6 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
7 | {-# LANGUAGE QuasiQuotes #-} | ||
8 | {-# LANGUAGE TemplateHaskell #-} | ||
9 | {-# LANGUAGE TypeFamilies #-} | ||
2 | 10 | ||
3 | import Thermoprint | 11 | import Thermoprint |
4 | import Thermoprint.Api | 12 | import Thermoprint.Api |
13 | import PrintOut | ||
5 | 14 | ||
15 | import qualified Data.Text.Lazy as TL | ||
16 | import qualified Data.ByteString.Lazy.Char8 as LBS | ||
17 | import qualified Data.ByteString.Lazy.Char8 as Lazy (ByteString) | ||
18 | import qualified Data.Text as T (pack) | ||
19 | import Data.ByteString (ByteString) | ||
20 | import qualified Data.ByteString as BS | ||
21 | |||
6 | import Data.Aeson | 22 | import Data.Aeson |
7 | import Network.Wai | 23 | import Network.Wai |
8 | import Network.Wai.Handler.Warp | 24 | import Network.Wai.Handler.Warp |
9 | import Servant | 25 | import Servant |
10 | import qualified Data.Text.Lazy as Text | ||
11 | import qualified Data.ByteString.Lazy.Char8 as ByteString | ||
12 | import Data.ByteString.Lazy.Char8 (ByteString) | ||
13 | import GHC.Generics | 26 | import GHC.Generics |
14 | 27 | ||
15 | import Control.Monad | 28 | import Control.Monad |
29 | import Control.Monad.Trans.Class | ||
16 | import Control.Monad.IO.Class | 30 | import Control.Monad.IO.Class |
17 | import Control.Monad.Trans.Either | 31 | import Control.Monad.Trans.Either |
18 | 32 | ||
33 | import Control.Monad.Logger | ||
34 | |||
19 | import Options.Applicative | 35 | import Options.Applicative |
20 | 36 | ||
21 | import System.IO | 37 | import System.IO hiding (print) |
38 | |||
39 | import Database.Persist | ||
40 | import Database.Persist.Sqlite | ||
41 | import Database.Persist.TH | ||
42 | |||
43 | import Data.Int (Int64) | ||
44 | |||
45 | import Prelude hiding (print) | ||
46 | |||
47 | share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| | ||
48 | Draft | ||
49 | title String | ||
50 | content PrintOut | ||
51 | deriving Show | ||
52 | |] | ||
53 | |||
22 | 54 | ||
23 | server :: Options -> Integer -> Block String -> EitherT ServantErr IO () | 55 | print :: Options -> Integer -> Block String -> EitherT ServantErr IO () |
24 | server Options{..} printerNo printOut = do | 56 | print Options{..} printerNo printOut = do |
25 | printerPath <- case genericIndex printers printerNo of | 57 | printerPath <- case genericIndex printers printerNo of |
26 | Just path -> return path | 58 | Just path -> return path |
27 | Nothing -> left $ err404 { errBody = "printerId out of bounds" } | 59 | Nothing -> left $ err404 { errBody = "printerId out of bounds" } |
@@ -29,7 +61,7 @@ server Options{..} printerNo printOut = do | |||
29 | where | 61 | where |
30 | doPrint handle = do | 62 | doPrint handle = do |
31 | hSetBuffering handle NoBuffering | 63 | hSetBuffering handle NoBuffering |
32 | ByteString.hPut handle $ render' printOut | 64 | LBS.hPut handle $ render' printOut |
33 | genericIndex :: Integral i => [a] -> i -> Maybe a | 65 | genericIndex :: Integral i => [a] -> i -> Maybe a |
34 | genericIndex (x:_) 0 = Just x | 66 | genericIndex (x:_) 0 = Just x |
35 | genericIndex (_:xs) n | 67 | genericIndex (_:xs) n |
@@ -37,11 +69,47 @@ server Options{..} printerNo printOut = do | |||
37 | | otherwise = Nothing | 69 | | otherwise = Nothing |
38 | genericIndex _ _ = Nothing | 70 | genericIndex _ _ = Nothing |
39 | 71 | ||
72 | withPool = flip runSqlPool | ||
73 | |||
74 | queryDrafts :: Options -> ConnectionPool -> EitherT ServantErr IO [(Int64, String)] | ||
75 | queryDrafts Options{..} cPool = withPool cPool $ do | ||
76 | drafts <- selectList [] [] | ||
77 | return $ map deSQLify drafts | ||
78 | where | ||
79 | deSQLify :: Entity Draft -> (Int64, String) | ||
80 | deSQLify (Entity k (Draft title _)) = (fromSqlKey k, title) | ||
81 | |||
82 | getDraft :: Options -> ConnectionPool -> Int64 -> EitherT ServantErr IO (String, Block String) | ||
83 | getDraft Options{..} cPool draftId = withPool cPool $ do | ||
84 | draft <- get $ toSqlKey draftId | ||
85 | case draft of | ||
86 | Nothing -> lift $ left $ err404 { errBody = "no such draftId" } | ||
87 | Just (Draft title content) -> return (title, content) | ||
88 | |||
89 | writeDraft :: Options -> ConnectionPool -> Int64 -> (String, Block String) -> EitherT ServantErr IO () | ||
90 | writeDraft Options{..} cPool draftId (draftName, draft) = withPool cPool $ repsert (toSqlKey draftId) (Draft draftName draft) | ||
91 | |||
92 | addDraft :: Options -> ConnectionPool -> (String, Block String) -> EitherT ServantErr IO Int64 | ||
93 | addDraft Options{..} cPool (draftName, draft) = withPool cPool $ (fromSqlKey <$> insert (Draft draftName draft)) | ||
94 | |||
95 | delDraft :: Options -> ConnectionPool -> Int64 -> EitherT ServantErr IO () | ||
96 | delDraft Options{..} cPool draftId = withPool cPool $ delete (toSqlKey draftId :: Key Draft) | ||
97 | |||
40 | data Options = Options | 98 | data Options = Options |
41 | { port :: Int | 99 | { port :: Int |
100 | , connStr :: String | ||
101 | , connNmbr :: Int | ||
42 | , printers :: [FilePath] | 102 | , printers :: [FilePath] |
43 | } | 103 | } |
44 | 104 | ||
105 | server :: Options -> ConnectionPool -> Server ThermoprintApi | ||
106 | server opts cPool = print opts | ||
107 | :<|> queryDrafts opts cPool | ||
108 | :<|> addDraft opts cPool | ||
109 | :<|> getDraft opts cPool | ||
110 | :<|> writeDraft opts cPool | ||
111 | :<|> delDraft opts cPool | ||
112 | |||
45 | options :: Parser Options | 113 | options :: Parser Options |
46 | options = Options | 114 | options = Options |
47 | <$> option auto ( | 115 | <$> option auto ( |
@@ -52,6 +120,21 @@ options = Options | |||
52 | <> value 8080 | 120 | <> value 8080 |
53 | <> showDefault | 121 | <> showDefault |
54 | ) | 122 | ) |
123 | <*> strOption ( | ||
124 | long "database" | ||
125 | <> short 'd' | ||
126 | <> metavar "STRING" | ||
127 | <> help "The sqlite connection string to use (can inlude some options)" | ||
128 | <> value "./storage.sqlite" | ||
129 | <> showDefault | ||
130 | ) | ||
131 | <*> option auto ( | ||
132 | long "database-connections" | ||
133 | <> metavar "INT" | ||
134 | <> help "The number of parallel sqlite connections to maintain" | ||
135 | <> value 2 | ||
136 | <> showDefault | ||
137 | ) | ||
55 | <*> some (strArgument ( | 138 | <*> some (strArgument ( |
56 | metavar "PATH [...]" | 139 | metavar "PATH [...]" |
57 | <> help "Path to one of the printers to use" | 140 | <> help "Path to one of the printers to use" |
@@ -62,10 +145,14 @@ thermoprintApi = Proxy | |||
62 | 145 | ||
63 | main :: IO () | 146 | main :: IO () |
64 | main = do | 147 | main = do |
65 | execParser opts >>= main' | 148 | execParser opts >>= runNoLoggingT . main' |
66 | where | 149 | where |
67 | opts = info (helper <*> options) ( | 150 | opts = info (helper <*> options) ( |
68 | fullDesc | 151 | fullDesc |
69 | <> header "thermoprint-servant - A REST server for interacting with a cheap thermoprinter" | 152 | <> header "thermoprint-servant - A REST server for interacting with a cheap thermoprinter" |
70 | ) | 153 | ) |
71 | main' args@(Options{..}) = run port $ serve thermoprintApi (server args) | 154 | main' args@(Options{..}) = withSqlitePool (T.pack connStr) connNmbr $ main'' |
155 | where | ||
156 | main'' cPool = do | ||
157 | runSqlPool (runMigration migrateAll) cPool | ||
158 | liftIO $ run port $ serve thermoprintApi (server args cPool) | ||
diff --git a/servant/src/PrintOut.hs b/servant/src/PrintOut.hs new file mode 100644 index 0000000..5f95a22 --- /dev/null +++ b/servant/src/PrintOut.hs | |||
@@ -0,0 +1,14 @@ | |||
1 | {-# LANGUAGE TemplateHaskell #-} | ||
2 | {-# LANGUAGE TypeSynonymInstances #-} | ||
3 | {-# LANGUAGE FlexibleInstances #-} | ||
4 | module PrintOut | ||
5 | ( PrintOut | ||
6 | ) where | ||
7 | |||
8 | import Thermoprint | ||
9 | import Thermoprint.Api | ||
10 | import Database.Persist.TH | ||
11 | |||
12 | type PrintOut = Block String | ||
13 | |||
14 | derivePersistFieldJSON "PrintOut" | ||