aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-23 11:09:04 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-23 11:09:04 +0000
commit08a6ee538ced1afb059491c7fd25f233999f5ca4 (patch)
tree656c8b2051c9bf7f9ba8200dfab5b9db6f109139
parent6648020d5ce7a41833da9f136affc569a601fcf3 (diff)
downloadthermoprint-08a6ee538ced1afb059491c7fd25f233999f5ca4.tar
thermoprint-08a6ee538ced1afb059491c7fd25f233999f5ca4.tar.gz
thermoprint-08a6ee538ced1afb059491c7fd25f233999f5ca4.tar.bz2
thermoprint-08a6ee538ced1afb059491c7fd25f233999f5ca4.tar.xz
thermoprint-08a6ee538ced1afb059491c7fd25f233999f5ca4.zip
Split out database defs & minor cleanup
-rw-r--r--server/src/Thermoprint/Server.hs65
-rw-r--r--server/src/Thermoprint/Server/Database.hs24
-rw-r--r--server/thermoprint-server.cabal2
-rw-r--r--server/thermoprint-server.nix4
4 files changed, 54 insertions, 41 deletions
diff --git a/server/src/Thermoprint/Server.hs b/server/src/Thermoprint/Server.hs
index 0918e83..d1ee6ee 100644
--- a/server/src/Thermoprint/Server.hs
+++ b/server/src/Thermoprint/Server.hs
@@ -1,15 +1,8 @@
1{-# LANGUAGE RecordWildCards #-} 1{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE EmptyDataDecls #-} 2{-# LANGUAGE TemplateHaskell #-}
3{-# LANGUAGE FlexibleContexts #-} 3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE GADTs #-} 4{-# LANGUAGE TypeOperators #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving #-} 5{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE QuasiQuotes #-}
9{-# LANGUAGE TemplateHaskell #-}
10{-# LANGUAGE TypeFamilies #-}
11{-# LANGUAGE OverloadedStrings #-}
12{-# LANGUAGE TypeOperators #-}
13 6
14module Thermoprint.Server 7module Thermoprint.Server
15 ( thermoprintServer 8 ( thermoprintServer
@@ -28,6 +21,8 @@ import Control.Monad ((<=<), mapM_, liftM2)
28import Prelude hiding ((.), id) 21import Prelude hiding ((.), id)
29import Control.Category 22import Control.Category
30 23
24import Data.Maybe (maybe)
25
31import Control.Monad.Logger 26import Control.Monad.Logger
32import Control.Monad.Reader 27import Control.Monad.Reader
33import Control.Monad.Trans.Resource 28import Control.Monad.Trans.Resource
@@ -45,6 +40,9 @@ import qualified Data.Sequence as Seq
45import Data.Map (Map) 40import Data.Map (Map)
46import qualified Data.Map as Map 41import qualified Data.Map as Map
47 42
43import Data.Text (Text)
44import qualified Data.Text as T (pack)
45
48import qualified Network.Wai.Handler.Warp as Warp 46import qualified Network.Wai.Handler.Warp as Warp
49import Network.Wai (Application) 47import Network.Wai (Application)
50 48
@@ -52,12 +50,13 @@ import Servant
52import Servant.Server 50import Servant.Server
53import Servant.Server.Internal.Enter 51import Servant.Server.Internal.Enter
54 52
55import Database.Persist 53import Database.Persist.Sql (runMigrationSilent, ConnectionPool, runSqlPool)
56import Database.Persist.Sql 54
57import Database.Persist.TH
58 55
59data Config = Config { dyreError :: Maybe String 56import Thermoprint.Server.Database
60 , warpSettings :: Warp.Settings 57
58data Config = Config { dyreError :: Maybe String -- ^ Set by 'Dyre' -- sent to log as an error
59 , warpSettings :: Warp.Settings -- ^ Configure 'Warp's behaviour
61 } 60 }
62 61
63instance Default Config where 62instance Default Config where
@@ -69,48 +68,36 @@ data HandlerInput = HandlerInput { sqlPool :: ConnectionPool
69 } 68 }
70 69
71 70
72share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
73Job
74 content Printout
75Draft
76 title DraftTitle Maybe
77 content Printout
78|]
79
80
81thermoprintServer :: ( MonadLoggerIO m 71thermoprintServer :: ( MonadLoggerIO m
82 , MonadIO m 72 , MonadIO m
83 , MonadBaseControl IO m 73 , MonadBaseControl IO m
84 , MonadReader ConnectionPool m 74 , MonadReader ConnectionPool m
85 ) => (m :~> IO) -> Config -> IO () 75 ) => (m :~> IO) -> Config -> IO ()
76-- ^ Run the server
86thermoprintServer io = Dyre.wrapMain $ Dyre.defaultParams 77thermoprintServer io = Dyre.wrapMain $ Dyre.defaultParams
87 { Dyre.projectName = "thermoprint-server" 78 { Dyre.projectName = "thermoprint-server"
88 , Dyre.realMain = realMain <=< handleDyreErrors 79 , Dyre.realMain = realMain
89 , Dyre.showError = (\cfg msg -> cfg { dyreError = Just msg }) 80 , Dyre.showError = (\cfg msg -> cfg { dyreError = Just msg })
90 } 81 }
91 where 82 where
92 handleDyreErrors cfg@(Config{..}) 83 realMain Config{..} = unNat io $ do
93 | Just msg <- dyreError = do 84 maybe (return ()) ($(logErrorS) "Dyre" . T.pack) dyreError
94 hPutStrLn stderr msg 85
95 exitFailure
96 undefined
97 | otherwise = return cfg
98
99 realMain (Config{..}) = enter io $ do
100 sqlPool <- ask 86 sqlPool <- ask
101 runSqlPool (runMigrationSilent migrateAll) sqlPool >>= mapM_ ($(logWarnS) "DB")
102
103 logFunc <- askLoggerIO 87 logFunc <- askLoggerIO
88
89 runSqlPool (runMigrationSilent migrateAll) sqlPool >>= mapM_ ($(logWarnS) "DB")
104 90
105 let 91 let
106 handlerInput = HandlerInput 92 handlerInput = HandlerInput
107 { sqlPool = sqlPool 93 { sqlPool = sqlPool
108 } 94 }
109 io' :: ReaderT HandlerInput (LoggingT IO) :~> IO 95 io' :: ProtoHandler :~> IO
110 io' = Nat (($ logFunc) . runLoggingT) . runReaderTNat handlerInput 96 io' = Nat (($ logFunc) . runLoggingT) . runReaderTNat handlerInput
111 liftIO . Warp.runSettings warpSettings . serve thermoprintAPI $ enter (hoistNat io') thermoprintServer' 97 liftIO . Warp.runSettings warpSettings . serve thermoprintAPI $ enter (hoistNat io') thermoprintServer'
112 98
113type Handler = EitherT ServantErr (ReaderT HandlerInput (LoggingT IO)) 99type ProtoHandler = ReaderT HandlerInput (LoggingT IO)
100type Handler = EitherT ServantErr ProtoHandler
114 101
115(<||>) :: Monad m => m a -> m b -> m (a :<|> b) 102(<||>) :: Monad m => m a -> m b -> m (a :<|> b)
116(<||>) = liftM2 (:<|>) 103(<||>) = liftM2 (:<|>)
diff --git a/server/src/Thermoprint/Server/Database.hs b/server/src/Thermoprint/Server/Database.hs
new file mode 100644
index 0000000..61179e6
--- /dev/null
+++ b/server/src/Thermoprint/Server/Database.hs
@@ -0,0 +1,24 @@
1{-# LANGUAGE TemplateHaskell #-}
2{-# LANGUAGE QuasiQuotes #-}
3{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE TypeFamilies #-}
5{-# LANGUAGE ExistentialQuantification #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7
8module Thermoprint.Server.Database
9 ( Job(..), JobId
10 , Draft(..), DraftId
11 , migrateAll
12 ) where
13
14import Thermoprint.API (Printout, DraftTitle)
15
16import Database.Persist.TH
17
18share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
19Job
20 content Printout
21Draft
22 title DraftTitle Maybe
23 content Printout
24|]
diff --git a/server/thermoprint-server.cabal b/server/thermoprint-server.cabal
index 0aa1870..45e57a6 100644
--- a/server/thermoprint-server.cabal
+++ b/server/thermoprint-server.cabal
@@ -18,6 +18,7 @@ cabal-version: >=1.10
18 18
19library 19library
20 exposed-modules: Thermoprint.Server 20 exposed-modules: Thermoprint.Server
21 , Thermoprint.Server.Database
21 -- other-modules: 22 -- other-modules:
22 -- other-extensions: 23 -- other-extensions:
23 build-depends: base >=4.8 && <5 24 build-depends: base >=4.8 && <5
@@ -35,6 +36,7 @@ library
35 , monad-logger >=0.3.13 && <1 36 , monad-logger >=0.3.13 && <1
36 , containers >=0.5.6 && <1 37 , containers >=0.5.6 && <1
37 , either >=4.4.1 && <5 38 , either >=4.4.1 && <5
39 , text >=1.2.1 && <2
38 hs-source-dirs: src 40 hs-source-dirs: src
39 default-language: Haskell2010 41 default-language: Haskell2010
40 42
diff --git a/server/thermoprint-server.nix b/server/thermoprint-server.nix
index c6a6224..c48ec7f 100644
--- a/server/thermoprint-server.nix
+++ b/server/thermoprint-server.nix
@@ -1,6 +1,6 @@
1{ mkDerivation, base, containers, data-default-class, dyre, either 1{ mkDerivation, base, containers, data-default-class, dyre, either
2, monad-logger, mtl, persistent, persistent-sqlite 2, monad-logger, mtl, persistent, persistent-sqlite
3, persistent-template, resourcet, servant-server, stdenv 3, persistent-template, resourcet, servant-server, stdenv, text
4, thermoprint-spec, transformers, wai, warp 4, thermoprint-spec, transformers, wai, warp
5}: 5}:
6mkDerivation { 6mkDerivation {
@@ -11,7 +11,7 @@ mkDerivation {
11 isExecutable = true; 11 isExecutable = true;
12 libraryHaskellDepends = [ 12 libraryHaskellDepends = [
13 base containers data-default-class dyre either monad-logger mtl 13 base containers data-default-class dyre either monad-logger mtl
14 persistent persistent-template resourcet servant-server 14 persistent persistent-template resourcet servant-server text
15 thermoprint-spec transformers wai warp 15 thermoprint-spec transformers wai warp
16 ]; 16 ];
17 executableHaskellDepends = [ 17 executableHaskellDepends = [