1
+ {-# LANGUAGE LambdaCase #-}
1
2
{-# LANGUAGE NamedFieldPuns #-}
2
3
{-# LANGUAGE QuasiQuotes #-}
3
4
{-# LANGUAGE RecordWildCards #-}
@@ -12,8 +13,10 @@ import qualified Data.Aeson as JSON
12
13
import qualified Data.ByteString.Char8 as BS
13
14
import qualified Data.ByteString.Lazy as LBS
14
15
import qualified Hasql.Transaction.Sessions as SQL
16
+ import qualified Network.HTTP.Types.Status as HTTP
15
17
import qualified Options.Applicative as O
16
18
19
+ import Data.Maybe (fromJust )
17
20
import Text.Heredoc (str )
18
21
19
22
import PostgREST.AppState (AppState )
@@ -24,23 +27,41 @@ import PostgREST.Version (prettyVersion)
24
27
25
28
import qualified PostgREST.App as App
26
29
import qualified PostgREST.AppState as AppState
30
+ import qualified PostgREST.Client as Client
27
31
import qualified PostgREST.Config as Config
28
32
29
33
import Protolude
30
34
31
35
32
36
main :: CLI -> IO ()
33
37
main CLI {cliCommand, cliPath} = do
34
- conf@ AppConfig { .. } <-
38
+ conf <-
35
39
either panic identity <$> Config. readAppConfig mempty cliPath Nothing mempty mempty
40
+ case cliCommand of
41
+ Admin adminCmd -> runAdminCommand conf adminCmd
42
+ Run runCmd -> runAppCommand conf runCmd
36
43
44
+ -- | Run command using http-client to communicate with an already running postgrest
45
+ runAdminCommand :: AppConfig -> AdminCommand -> IO ()
46
+ runAdminCommand conf CmdReady = do
47
+ status <- Client. ready conf
48
+ if status >= HTTP. status200 && status < HTTP. status300
49
+ then do
50
+ putStrLn $ BS. pack $ " OK: http://:" <> show (fromJust $ configAdminServerPort conf) <> " /ready"
51
+ exitSuccess
52
+ else
53
+ exitWith $ ExitFailure 1
54
+
55
+ -- | Run postgrest with command
56
+ runAppCommand :: AppConfig -> RunCommand -> IO ()
57
+ runAppCommand conf@ AppConfig {.. } runCmd = do
37
58
-- Per https://github.com/PostgREST/postgrest/issues/268, we want to
38
59
-- explicitly close the connections to PostgreSQL on shutdown.
39
60
-- 'AppState.destroy' takes care of that.
40
61
bracket
41
62
(AppState. init conf)
42
63
AppState. destroy
43
- (\ appState -> case cliCommand of
64
+ (\ appState -> case runCmd of
44
65
CmdDumpConfig -> do
45
66
when configDbConfig $ AppState. readInDbConfig True appState
46
67
putStr . Config. toText =<< AppState. getConfig appState
@@ -71,6 +92,13 @@ data CLI = CLI
71
92
}
72
93
73
94
data Command
95
+ = Admin AdminCommand
96
+ | Run RunCommand
97
+
98
+ data AdminCommand
99
+ = CmdReady
100
+
101
+ data RunCommand
74
102
= CmdRun
75
103
| CmdDumpConfig
76
104
| CmdDumpSchema
@@ -105,7 +133,7 @@ readCLIShowHelp =
105
133
cliParser :: O. Parser CLI
106
134
cliParser =
107
135
CLI
108
- <$> (dumpConfigFlag <|> dumpSchemaFlag)
136
+ <$> (dumpConfigFlag <|> dumpSchemaFlag <|> readyFlag )
109
137
<*> O. optional configFileOption
110
138
111
139
configFileOption =
@@ -114,15 +142,21 @@ readCLIShowHelp =
114
142
<> O. help " Path to configuration file"
115
143
116
144
dumpConfigFlag =
117
- O. flag CmdRun CmdDumpConfig $
145
+ O. flag ( Run CmdRun ) ( Run CmdDumpConfig ) $
118
146
O. long " dump-config"
119
147
<> O. help " Dump loaded configuration and exit"
120
148
121
149
dumpSchemaFlag =
122
- O. flag CmdRun CmdDumpSchema $
150
+ O. flag ( Run CmdRun ) ( Run CmdDumpSchema ) $
123
151
O. long " dump-schema"
124
152
<> O. help " Dump loaded schema as JSON and exit (for debugging, output structure is unstable)"
125
153
154
+ readyFlag =
155
+ O. flag (Run CmdRun ) (Admin CmdReady ) $
156
+ O. long " ready"
157
+ <> O. help " Checks the health of PostgREST by doing a request on the admin server /ready endpoint"
158
+
159
+
126
160
exampleConfigFile :: [Char ]
127
161
exampleConfigFile =
128
162
[str |## Admin server used for checks. It's disabled by default unless a port is specified.
0 commit comments