fix: Panic when attempting to run with unix socket on non-unix host

This commit is contained in:
monacoremo
2021-04-24 19:42:58 +02:00
committed by Remo Rechkemmer
parent 59f0fe5b32
commit 63e0292e23
4 changed files with 17 additions and 13 deletions
+3 -3
View File
@@ -31,11 +31,11 @@ installSignalHandlers = Unix.installSignalHandlers
installSignalHandlers _ = pass installSignalHandlers _ = pass
#endif #endif
runAppInSocket :: App.SocketRunner runAppInSocket :: Maybe App.SocketRunner
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
runAppInSocket = Unix.runAppInSocket runAppInSocket = Just Unix.runAppWithSocket
#else #else
runAppInSocket _ _ _ _ = pass runAppInSocket = Nothing
#endif #endif
setBuffering :: IO () setBuffering :: IO ()
+8 -4
View File
@@ -101,8 +101,8 @@ type SignalHandlerInstaller = AppState -> IO()
type SocketRunner = Warp.Settings -> Wai.Application -> FileMode -> FilePath -> IO() type SocketRunner = Warp.Settings -> Wai.Application -> FileMode -> FilePath -> IO()
run :: SignalHandlerInstaller -> SocketRunner -> AppState -> IO () run :: SignalHandlerInstaller -> Maybe SocketRunner -> AppState -> IO ()
run installHandlers runInSocket appState = do run installHandlers maybeRunWithSocket appState = do
conf@AppConfig{..} <- AppState.getConfig appState conf@AppConfig{..} <- AppState.getConfig appState
connectionWorker appState -- Loads the initial DbStructure connectionWorker appState -- Loads the initial DbStructure
installHandlers appState installHandlers appState
@@ -113,8 +113,12 @@ run installHandlers runInSocket appState = do
case configServerUnixSocket of case configServerUnixSocket of
Just socket -> Just socket ->
-- run the postgrest application with user defined socket. Only for UNIX systems. -- run the postgrest application with user defined socket. Only for UNIX systems
runInSocket (serverSettings conf) app configServerUnixSocketMode socket case maybeRunWithSocket of
Just runWithSocket ->
runWithSocket (serverSettings conf) app configServerUnixSocketMode socket
Nothing ->
panic "Cannot run with socket on non-unix plattforms."
Nothing -> Nothing ->
do do
putStrLn $ ("Listening on port " :: Text) <> show configServerPort putStrLn $ ("Listening on port " :: Text) <> show configServerPort
+3 -3
View File
@@ -31,8 +31,8 @@ import qualified PostgREST.Config as Config
import Protolude hiding (hPutStrLn) import Protolude hiding (hPutStrLn)
main :: App.SignalHandlerInstaller -> App.SocketRunner -> CLI -> IO () main :: App.SignalHandlerInstaller -> Maybe App.SocketRunner -> CLI -> IO ()
main installSignalHandlers runAppInSocket CLI{cliCommand, cliPath} = do main installSignalHandlers runAppWithSocket CLI{cliCommand, cliPath} = do
conf@AppConfig{..} <- conf@AppConfig{..} <-
either panic identity <$> Config.readAppConfig mempty cliPath Nothing either panic identity <$> Config.readAppConfig mempty cliPath Nothing
appState <- AppState.init conf appState <- AppState.init conf
@@ -47,7 +47,7 @@ main installSignalHandlers runAppInSocket CLI{cliCommand, cliPath} = do
exec :: Command -> AppState -> IO () exec :: Command -> AppState -> IO ()
exec CmdDumpConfig appState = putStr . Config.toText =<< AppState.getConfig appState exec CmdDumpConfig appState = putStr . Config.toText =<< AppState.getConfig appState
exec CmdDumpSchema appState = putStrLn =<< dumpSchema appState exec CmdDumpSchema appState = putStrLn =<< dumpSchema appState
exec CmdRun appState = App.run installSignalHandlers runAppInSocket appState exec CmdRun appState = App.run installSignalHandlers runAppWithSocket appState
-- | Dump DbStructure schema to JSON -- | Dump DbStructure schema to JSON
dumpSchema :: AppState -> IO LBS.ByteString dumpSchema :: AppState -> IO LBS.ByteString
+3 -3
View File
@@ -1,5 +1,5 @@
module PostgREST.Unix module PostgREST.Unix
( runAppInSocket ( runAppWithSocket
, installSignalHandlers , installSignalHandlers
) where ) where
@@ -20,8 +20,8 @@ import Protolude
-- | Run the PostgREST application with user defined socket. -- | Run the PostgREST application with user defined socket.
runAppInSocket :: Warp.Settings -> Application -> FileMode -> FilePath -> IO () runAppWithSocket :: Warp.Settings -> Application -> FileMode -> FilePath -> IO ()
runAppInSocket settings app socketFileMode socketFilePath = runAppWithSocket settings app socketFileMode socketFilePath =
bracket createAndBindSocket Socket.close $ \socket -> do bracket createAndBindSocket Socket.close $ \socket -> do
putStrLn $ ("Listening on unix socket " :: Text) <> show socketFilePath putStrLn $ ("Listening on unix socket " :: Text) <> show socketFilePath
Socket.listen socket Socket.maxListenQueue Socket.listen socket Socket.maxListenQueue