Skip to content

Commit

Permalink
fix: Panic when attempting to run with unix socket on non-unix host
Browse files Browse the repository at this point in the history
  • Loading branch information
monacoremo committed Apr 24, 2021
1 parent 6792dfd commit de54e54
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ installSignalHandlers = Unix.installSignalHandlers
installSignalHandlers _ = pass
#endif

runAppInSocket :: App.SocketRunner
runAppInSocket :: Maybe App.SocketRunner
#ifndef mingw32_HOST_OS
runAppInSocket = Unix.runAppInSocket
runAppInSocket = Just Unix.runAppWithSocket
#else
runAppInSocket _ _ _ _ = pass
runAppInSocket = Nothing
#endif

setBuffering :: IO ()
Expand Down
12 changes: 8 additions & 4 deletions src/PostgREST/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ type SignalHandlerInstaller = AppState -> IO()
type SocketRunner = Warp.Settings -> Wai.Application -> FileMode -> FilePath -> IO()


run :: SignalHandlerInstaller -> SocketRunner -> AppState -> IO ()
run installHandlers runInSocket appState = do
run :: SignalHandlerInstaller -> Maybe SocketRunner -> AppState -> IO ()
run installHandlers maybeRunWithSocket appState = do
conf@AppConfig{..} <- AppState.getConfig appState
connectionWorker appState -- Loads the initial DbStructure
installHandlers appState
Expand All @@ -113,8 +113,12 @@ run installHandlers runInSocket appState = do

case configServerUnixSocket of
Just socket ->
-- run the postgrest application with user defined socket. Only for UNIX systems.
runInSocket (serverSettings conf) app configServerUnixSocketMode socket
-- run the postgrest application with user defined socket. Only for UNIX systems
case maybeRunWithSocket of
Just runWithSocket ->
runWithSocket (serverSettings conf) app configServerUnixSocketMode socket
Nothing ->
panic "Cannot run with socket on non-unix plattforms."
Nothing ->
do
putStrLn $ ("Listening on port " :: Text) <> show configServerPort
Expand Down
6 changes: 3 additions & 3 deletions src/PostgREST/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import qualified PostgREST.Config as Config
import Protolude hiding (hPutStrLn)


main :: App.SignalHandlerInstaller -> App.SocketRunner -> CLI -> IO ()
main installSignalHandlers runAppInSocket CLI{cliCommand, cliPath} = do
main :: App.SignalHandlerInstaller -> Maybe App.SocketRunner -> CLI -> IO ()
main installSignalHandlers runAppWithSocket CLI{cliCommand, cliPath} = do
conf@AppConfig{..} <-
either panic identity <$> Config.readAppConfig mempty cliPath Nothing
appState <- AppState.init conf
Expand All @@ -47,7 +47,7 @@ main installSignalHandlers runAppInSocket CLI{cliCommand, cliPath} = do
exec :: Command -> AppState -> IO ()
exec CmdDumpConfig appState = putStr . Config.toText =<< AppState.getConfig 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
dumpSchema :: AppState -> IO LBS.ByteString
Expand Down
6 changes: 3 additions & 3 deletions src/PostgREST/Unix.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module PostgREST.Unix
( runAppInSocket
( runAppWithSocket
, installSignalHandlers
) where

Expand All @@ -20,8 +20,8 @@ import Protolude


-- | Run the PostgREST application with user defined socket.
runAppInSocket :: Warp.Settings -> Application -> FileMode -> FilePath -> IO ()
runAppInSocket settings app socketFileMode socketFilePath =
runAppWithSocket :: Warp.Settings -> Application -> FileMode -> FilePath -> IO ()
runAppWithSocket settings app socketFileMode socketFilePath =
bracket createAndBindSocket Socket.close $ \socket -> do
putStrLn $ ("Listening on unix socket " :: Text) <> show socketFilePath
Socket.listen socket Socket.maxListenQueue
Expand Down

0 comments on commit de54e54

Please sign in to comment.