Skip to content

Commit cb2c7df

Browse files
authored
Merge pull request #53 from diogob/disable-static-server
Use a dummy app in case no path is passed for static server
2 parents 4d160d4 + 6398144 commit cb2c7df

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

app/Config.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import Env
2424
import Data.Text (intercalate)
2525
import Data.Version (versionBranch)
2626
import Paths_postgres_websockets (version)
27-
import Protolude hiding (intercalate, (<>))
27+
import Protolude hiding (intercalate, (<>), optional)
2828

2929
-- | Config file settings for the server
3030
data AppConfig = AppConfig {
3131
configDatabase :: Text
32-
, configPath :: Text
32+
, configPath :: Maybe Text
3333
, configHost :: Text
3434
, configPort :: Int
3535
, configListenChannel :: Text
@@ -47,7 +47,7 @@ readOptions :: IO AppConfig
4747
readOptions =
4848
Env.parse (header "You need to configure some environment variables to start the service.") $
4949
AppConfig <$> var (str <=< nonempty) "PGWS_DB_URI" (help "String to connect to PostgreSQL")
50-
<*> var str "PGWS_ROOT_PATH" (def "./" <> helpDef show <> help "Root path to serve static files")
50+
<*> optional (var str "PGWS_ROOT_PATH" (help "Root path to serve static files, unset to disable."))
5151
<*> var str "PGWS_HOST" (def "*4" <> helpDef show <> help "Address the server will listen for websocket connections")
5252
<*> var auto "PGWS_PORT" (def 3000 <> helpDef show <> help "Port the server will listen for websocket connections")
5353
<*> var str "PGWS_LISTEN_CHANNEL" (def "postgres-websockets-listener" <> helpDef show <> help "Master channel used in the database to send or read messages in any notification channel")

app/Main.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import qualified Hasql.Session as H
1818
import qualified Hasql.Decoders as HD
1919
import qualified Hasql.Encoders as HE
2020
import qualified Hasql.Pool as P
21-
import Network.Wai.Application.Static
21+
import Network.Wai.Application.Static
2222

23+
import Network.Wai (Application, responseLBS)
24+
import Network.HTTP.Types (status200)
2325
import Network.Wai.Handler.Warp
2426
import Network.Wai.Middleware.RequestLogger (logStdout)
2527
import System.IO (BufferMode (..),
@@ -32,7 +34,7 @@ isServerVersionSupported = do
3234
where
3335
pgVersion =
3436
H.Statement "SELECT current_setting('server_version_num')::integer"
35-
HE.noParams (HD.singleRow $ HD.column $ HD.nonNullable $ HD.int4) False
37+
HE.noParams (HD.singleRow $ HD.column $ HD.nonNullable HD.int4) False
3638

3739
main :: IO ()
3840
main = do
@@ -62,7 +64,13 @@ main = do
6264

6365
runSettings appSettings $
6466
postgresWsMiddleware listenChannel (configJwtSecret conf) pool multi $
65-
logStdout $ staticApp $ defaultFileServerSettings $ toS $ configPath conf
67+
logStdout $ maybe dummyApp staticApp' (configPath conf)
68+
where
69+
staticApp' :: Text -> Application
70+
staticApp' = staticApp . defaultFileServerSettings . toS
71+
dummyApp :: Application
72+
dummyApp _ respond =
73+
respond $ responseLBS status200 [("Content-Type", "text/plain")] "Hello, Web!"
6674

6775
loadSecretFile :: AppConfig -> IO AppConfig
6876
loadSecretFile conf = extractAndTransform secret

postgres-websockets.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ executable postgres-websockets
6767
, wai
6868
, wai-extra
6969
, wai-app-static
70+
, http-types
7071
, envparse
7172
default-language: Haskell2010
7273
default-extensions: OverloadedStrings, NoImplicitPrelude, QuasiQuotes

src/PostgresWebsockets.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ wsApp dbChannel secret pool multi pendingConn =
5555
hasRead m = m == ("r" :: ByteString) || m == ("rw" :: ByteString)
5656
hasWrite m = m == ("w" :: ByteString) || m == ("rw" :: ByteString)
5757
rejectRequest = WS.rejectRequest pendingConn . encodeUtf8
58-
-- the URI has one of the two formats - /:jwt or /:channel/:jwt
58+
-- the URI has one of the two formats - /:jwt or /:channel/:jwt
5959
pathElements = BS.split '/' $ BS.drop 1 $ WS.requestPath $ WS.pendingRequest pendingConn
6060
jwtToken
6161
| length pathElements > 1 = headDef "" $ tailSafe pathElements
@@ -74,7 +74,7 @@ wsApp dbChannel secret pool multi pendingConn =
7474
onMessage multi ch $ WS.sendTextData conn . B.payload
7575

7676
when (hasWrite mode) $
77-
let sendNotifications = void . (H.notifyPool pool dbChannel) . toS
77+
let sendNotifications = void . H.notifyPool pool dbChannel . toS
7878
in notifySession validClaims (toS ch) conn sendNotifications
7979

8080
waitForever <- newEmptyMVar

0 commit comments

Comments
 (0)