diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-25 17:56:13 +0000 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-25 17:56:13 +0000 |
| commit | 9db2c42f4880362cf098358de830415c14f6878c (patch) | |
| tree | 2b0b9257f01eec926152746fc2e7646764063c3a /servant/src/Main.hs | |
| parent | 08eee2f0de77ffa631e84ccf734e8e95817b7c81 (diff) | |
| download | thermoprint-9db2c42f4880362cf098358de830415c14f6878c.tar thermoprint-9db2c42f4880362cf098358de830415c14f6878c.tar.gz thermoprint-9db2c42f4880362cf098358de830415c14f6878c.tar.bz2 thermoprint-9db2c42f4880362cf098358de830415c14f6878c.tar.xz thermoprint-9db2c42f4880362cf098358de830415c14f6878c.zip | |
Cleaned tree for rewrite
Diffstat (limited to 'servant/src/Main.hs')
| -rw-r--r-- | servant/src/Main.hs | 158 |
1 files changed, 0 insertions, 158 deletions
diff --git a/servant/src/Main.hs b/servant/src/Main.hs deleted file mode 100644 index 0aa9eeb..0000000 --- a/servant/src/Main.hs +++ /dev/null | |||
| @@ -1,158 +0,0 @@ | |||
| 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 #-} | ||
| 10 | |||
| 11 | import Thermoprint | ||
| 12 | import Thermoprint.Api | ||
| 13 | import PrintOut | ||
| 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 | |||
| 22 | import Data.Aeson | ||
| 23 | import Network.Wai | ||
| 24 | import Network.Wai.Handler.Warp | ||
| 25 | import Servant | ||
| 26 | import GHC.Generics | ||
| 27 | |||
| 28 | import Control.Monad | ||
| 29 | import Control.Monad.Trans.Class | ||
| 30 | import Control.Monad.IO.Class | ||
| 31 | import Control.Monad.Trans.Either | ||
| 32 | |||
| 33 | import Control.Monad.Logger | ||
| 34 | |||
| 35 | import Options.Applicative | ||
| 36 | |||
| 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 | |||
| 54 | |||
| 55 | print :: Options -> Integer -> Block String -> EitherT ServantErr IO () | ||
| 56 | print Options{..} printerNo printOut = do | ||
| 57 | printerPath <- case genericIndex printers printerNo of | ||
| 58 | Just path -> return path | ||
| 59 | Nothing -> left $ err404 { errBody = "printerId out of bounds" } | ||
| 60 | liftIO $ withFile printerPath WriteMode doPrint | ||
| 61 | where | ||
| 62 | doPrint handle = do | ||
| 63 | hSetBuffering handle NoBuffering | ||
| 64 | LBS.hPut handle $ render' printOut | ||
| 65 | genericIndex :: Integral i => [a] -> i -> Maybe a | ||
| 66 | genericIndex (x:_) 0 = Just x | ||
| 67 | genericIndex (_:xs) n | ||
| 68 | | n > 0 = genericIndex xs (n - 1) | ||
| 69 | | otherwise = Nothing | ||
| 70 | genericIndex _ _ = Nothing | ||
| 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 | |||
| 98 | data Options = Options | ||
| 99 | { port :: Int | ||
| 100 | , connStr :: String | ||
| 101 | , connNmbr :: Int | ||
| 102 | , printers :: [FilePath] | ||
| 103 | } | ||
| 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 | |||
| 113 | options :: Parser Options | ||
| 114 | options = Options | ||
| 115 | <$> option auto ( | ||
| 116 | long "port" | ||
| 117 | <> short 'p' | ||
| 118 | <> metavar "PORT" | ||
| 119 | <> help "The port we'll run the server on" | ||
| 120 | <> value 8080 | ||
| 121 | <> showDefault | ||
| 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 | ) | ||
| 138 | <*> some (strArgument ( | ||
| 139 | metavar "PATH [...]" | ||
| 140 | <> help "Path to one of the printers to use" | ||
| 141 | )) | ||
| 142 | |||
| 143 | thermoprintApi :: Proxy ThermoprintApi | ||
| 144 | thermoprintApi = Proxy | ||
| 145 | |||
| 146 | main :: IO () | ||
| 147 | main = do | ||
| 148 | execParser opts >>= runNoLoggingT . main' | ||
| 149 | where | ||
| 150 | opts = info (helper <*> options) ( | ||
| 151 | fullDesc | ||
| 152 | <> header "thermoprint-servant - A REST server for interacting with a cheap thermoprinter" | ||
| 153 | ) | ||
| 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) | ||
