diff options
Diffstat (limited to 'servant')
-rw-r--r-- | servant/api/Thermoprint/Api.hs | 9 | ||||
-rw-r--r-- | servant/servant.cabal | 6 | ||||
-rw-r--r-- | servant/servant.nix | 4 | ||||
-rw-r--r-- | servant/src/Main.hs | 105 | ||||
-rw-r--r-- | servant/src/PrintOut.hs | 14 |
5 files changed, 126 insertions, 12 deletions
diff --git a/servant/api/Thermoprint/Api.hs b/servant/api/Thermoprint/Api.hs index bd5744b..f3318c4 100644 --- a/servant/api/Thermoprint/Api.hs +++ b/servant/api/Thermoprint/Api.hs | |||
@@ -14,6 +14,8 @@ import GHC.Generics | |||
14 | 14 | ||
15 | import Control.Monad | 15 | import Control.Monad |
16 | 16 | ||
17 | import Data.Int (Int64) | ||
18 | |||
17 | instance ToJSON ByteString where | 19 | instance ToJSON ByteString where |
18 | toJSON = toJSON . Text.pack . ByteString.unpack | 20 | toJSON = toJSON . Text.pack . ByteString.unpack |
19 | instance FromJSON ByteString where | 21 | instance FromJSON ByteString where |
@@ -25,4 +27,9 @@ instance FromJSON c => FromJSON (Inline c) | |||
25 | instance ToJSON c => ToJSON (Block c) | 27 | instance ToJSON c => ToJSON (Block c) |
26 | instance FromJSON c => FromJSON (Block c) | 28 | instance FromJSON c => FromJSON (Block c) |
27 | 29 | ||
28 | type ThermoprintApi = "print" :> Capture "printerId" Integer :> ReqBody '[JSON] (Block String) :> Post '[JSON] () | 30 | type ThermoprintApi = "print" :> Capture "printerId" Integer :> ReqBody '[JSON] (Block String) :> Post '[JSON] () |
31 | :<|> "drafts" :> Get '[JSON] [(Int64, String)] | ||
32 | :<|> "drafts" :> ReqBody '[JSON] (String, Block String) :> Put '[JSON] Int64 | ||
33 | :<|> "drafts" :> Capture "draftId" Int64 :> Get '[JSON] (String, Block String) | ||
34 | :<|> "drafts" :> Capture "draftId" Int64 :> ReqBody '[JSON] (String, Block String) :> Put '[JSON] () | ||
35 | :<|> "drafts" :> Capture "draftId" Int64 :> Delete '[JSON] () | ||
diff --git a/servant/servant.cabal b/servant/servant.cabal index b877196..dce4490 100644 --- a/servant/servant.cabal +++ b/servant/servant.cabal | |||
@@ -48,4 +48,8 @@ executable thermoprint | |||
48 | , bytestring >=0.10.6 && <0.11 | 48 | , bytestring >=0.10.6 && <0.11 |
49 | , either >=4.4.1 && <4.5 | 49 | , either >=4.4.1 && <4.5 |
50 | , optparse-applicative >=0.11.0 && <0.12 | 50 | , optparse-applicative >=0.11.0 && <0.12 |
51 | , transformers >=0.4.2 && <0.5 \ No newline at end of file | 51 | , transformers >=0.4.2 && <0.5 |
52 | , persistent >=2.2 && <3 | ||
53 | , persistent-template >=2.1 && <3 | ||
54 | , persistent-sqlite >=2.2 && <3 | ||
55 | , monad-logger >=0.3.13 && <1 \ No newline at end of file | ||
diff --git a/servant/servant.nix b/servant/servant.nix index a84fc77..5ea8d59 100644 --- a/servant/servant.nix +++ b/servant/servant.nix | |||
@@ -2,7 +2,8 @@ | |||
2 | , stdenv | 2 | , stdenv |
3 | , base | 3 | , base |
4 | , thermoprint | 4 | , thermoprint |
5 | , aeson, wai, servant-server, warp, optparse-applicative | 5 | , aeson, wai, servant-server, warp, optparse-applicative, persistent |
6 | , persistent-template, persistent-sqlite, monad-logger | ||
6 | }: | 7 | }: |
7 | 8 | ||
8 | mkDerivation { | 9 | mkDerivation { |
@@ -13,6 +14,7 @@ mkDerivation { | |||
13 | isExecutable = true; | 14 | isExecutable = true; |
14 | executableHaskellDepends = [ | 15 | executableHaskellDepends = [ |
15 | base thermoprint aeson wai servant-server warp optparse-applicative | 16 | base thermoprint aeson wai servant-server warp optparse-applicative |
17 | persistent persistent-template persistent-sqlite monad-logger | ||
16 | ]; | 18 | ]; |
17 | homepage = "git://git.yggdrasil.li/thermoprint"; | 19 | homepage = "git://git.yggdrasil.li/thermoprint"; |
18 | description = "Server for interfacing to cheap thermoprinters"; | 20 | description = "Server for interfacing to cheap thermoprinters"; |
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" | ||