Skip to content

Commit 3bb1028

Browse files
committed
Remove protolude since the library seems a bit stale and we can replace
the interesting bits with a simpler module.
1 parent 2bdcaa8 commit 3bb1028

16 files changed

+380
-259
lines changed

Setup.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import Distribution.Simple
2+
23
main = defaultMain

app/Main.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
module Main where
22

3-
import Protolude
3+
import APrelude
44
import PostgresWebsockets
5-
65
import System.IO (BufferMode (..), hSetBuffering)
76

87
main :: IO ()
98
main = do
109
hSetBuffering stdout LineBuffering
11-
hSetBuffering stdin LineBuffering
10+
hSetBuffering stdin LineBuffering
1211
hSetBuffering stderr NoBuffering
1312

14-
putStrLn $ ("postgres-websockets " :: Text)
15-
<> prettyVersion
16-
<> " / Connects websockets to PostgreSQL asynchronous notifications."
13+
putStrLn $
14+
("postgres-websockets ")
15+
<> (unpack prettyVersion)
16+
<> " / Connects websockets to PostgreSQL asynchronous notifications."
1717

1818
conf <- loadConfig
19-
void $ serve conf
19+
void $ serve conf

postgres-websockets.cabal

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ common warnings
1818

1919
common language
2020
default-language: Haskell2010
21-
default-extensions: OverloadedStrings, NoImplicitPrelude, LambdaCase, RecordWildCards, QuasiQuotes
21+
default-extensions: OverloadedStrings, LambdaCase, RecordWildCards, QuasiQuotes
2222

2323
library
2424
import: warnings
@@ -29,6 +29,7 @@ library
2929
, PostgresWebsockets.HasqlBroadcast
3030
, PostgresWebsockets.Claims
3131
, PostgresWebsockets.Config
32+
, APrelude
3233

3334
autogen-modules: Paths_postgres_websockets
3435
other-modules: Paths_postgres_websockets
@@ -49,8 +50,9 @@ library
4950
, http-types >= 0.12.3 && < 0.13
5051
, jose >= 0.11 && < 0.12
5152
, lens >= 5.2.3 && < 5.4
53+
, mtl
54+
, async
5255
, postgresql-libpq >= 0.10.0 && < 0.12
53-
, protolude >= 0.2.3 && < 0.4
5456
, retry >= 0.8.1.0 && < 0.10
5557
, stm >= 2.5.0.0 && < 2.6
5658
, stm-containers >= 1.1.0.2 && < 1.3
@@ -76,7 +78,6 @@ executable postgres-websockets
7678
ghc-options: -threaded -rtsopts -with-rtsopts=-N
7779
build-depends: base >= 4.7 && < 5
7880
, postgres-websockets
79-
, protolude >= 0.2.3 && < 0.4
8081
default-language: Haskell2010
8182

8283
test-suite postgres-websockets-test
@@ -90,7 +91,6 @@ test-suite postgres-websockets-test
9091
, HasqlBroadcastSpec
9192
, ServerSpec
9293
build-depends: base
93-
, protolude >= 0.2.3 && < 0.4
9494
, postgres-websockets
9595
, hspec >= 2.7.1 && < 2.12
9696
, aeson >= 2.0 && < 2.3

src/APrelude.hs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
module APrelude
2+
( Text,
3+
ByteString,
4+
LByteString,
5+
Generic,
6+
fromMaybe,
7+
putErrLn,
8+
fromRight,
9+
isJust,
10+
decodeUtf8,
11+
encodeUtf8,
12+
MVar,
13+
readMVar,
14+
swapMVar,
15+
newMVar,
16+
STM,
17+
atomically,
18+
ThreadId,
19+
forkFinally,
20+
forkIO,
21+
killThread,
22+
threadDelay,
23+
(>=>),
24+
when,
25+
forever,
26+
void,
27+
panic,
28+
SomeException,
29+
throwError,
30+
liftIO,
31+
runExceptT,
32+
unpack,
33+
pack,
34+
showText,
35+
showBS,
36+
LBS.fromStrict,
37+
stdin,
38+
stdout,
39+
stderr,
40+
hPutStrLn,
41+
Word16,
42+
forM,
43+
forM_,
44+
takeMVar,
45+
newEmptyMVar,
46+
wait,
47+
headDef,
48+
tailSafe,
49+
withAsync,
50+
putMVar,
51+
die,
52+
myThreadId,
53+
replicateM,
54+
bracket,
55+
)
56+
where
57+
58+
import Control.Concurrent (ThreadId, forkFinally, forkIO, killThread, myThreadId, threadDelay)
59+
import Control.Concurrent.Async (wait, withAsync)
60+
import Control.Concurrent.MVar (MVar, newEmptyMVar, newMVar, putMVar, readMVar, swapMVar, takeMVar)
61+
import Control.Concurrent.STM (STM, atomically)
62+
import Control.Exception (Exception, SomeException, bracket, throw)
63+
import Control.Monad (forM, forM_, forever, replicateM, void, when, (>=>))
64+
import Control.Monad.Error.Class (throwError)
65+
import Control.Monad.Except (runExceptT)
66+
import Control.Monad.IO.Class (liftIO)
67+
import Data.ByteString (ByteString)
68+
import qualified Data.ByteString.Char8 as BS
69+
import qualified Data.ByteString.Lazy as LBS
70+
import Data.Either (fromRight)
71+
import Data.Maybe (fromMaybe, isJust, listToMaybe)
72+
import Data.Text (Text, pack, unpack)
73+
import qualified Data.Text as T
74+
import Data.Text.Encoding
75+
import Data.Word (Word16)
76+
import GHC.Generics (Generic)
77+
import System.Exit (die)
78+
import System.IO (hPutStrLn, stderr, stdin, stdout)
79+
80+
showBS :: (Show a) => a -> BS.ByteString
81+
showBS = BS.pack . show
82+
83+
showText :: (Show a) => a -> Text
84+
showText = T.pack . show
85+
86+
type LByteString = LBS.ByteString
87+
88+
-- | Uncatchable exceptions thrown and never caught.
89+
newtype FatalError = FatalError {fatalErrorMessage :: Text}
90+
deriving (Show)
91+
92+
instance Exception FatalError
93+
94+
panic :: Text -> a
95+
panic a = throw (FatalError a)
96+
97+
putErrLn :: Text -> IO ()
98+
putErrLn = hPutStrLn stderr . unpack
99+
100+
headDef :: a -> [a] -> a
101+
headDef def = fromMaybe def . listToMaybe
102+
103+
tailSafe :: [a] -> [a]
104+
tailSafe = drop 1

src/PostgresWebsockets.hs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{-|
2-
Module : PostgresWebsockets
3-
Description : PostgresWebsockets main library interface.
4-
5-
These are all function necessary to configure and start the server.
6-
-}
1+
-- |
2+
-- Module : PostgresWebsockets
3+
-- Description : PostgresWebsockets main library interface.
4+
--
5+
-- These are all function necessary to configure and start the server.
76
module PostgresWebsockets
8-
( prettyVersion
9-
, loadConfig
10-
, serve
11-
, postgresWsMiddleware
12-
) where
7+
( prettyVersion,
8+
loadConfig,
9+
serve,
10+
postgresWsMiddleware,
11+
)
12+
where
1313

14-
import PostgresWebsockets.Middleware ( postgresWsMiddleware )
15-
import PostgresWebsockets.Server ( serve )
16-
import PostgresWebsockets.Config ( prettyVersion, loadConfig )
14+
import PostgresWebsockets.Config (loadConfig, prettyVersion)
15+
import PostgresWebsockets.Middleware (postgresWsMiddleware)
16+
import PostgresWebsockets.Server (serve)

src/PostgresWebsockets/Broadcast.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ module PostgresWebsockets.Broadcast
2626
)
2727
where
2828

29+
import APrelude
2930
import Control.Concurrent.STM.TChan
3031
import Control.Concurrent.STM.TQueue
3132
import qualified Data.Aeson as A
32-
import Protolude hiding (toS)
33-
import Protolude.Conv (toS)
3433
import qualified StmContainers.Map as M
3534

3635
data Message = Message
@@ -63,7 +62,7 @@ instance A.ToJSON MultiplexerSnapshot
6362
-- | Given a multiplexer derive a type that can be printed for debugging or logging purposes
6463
takeSnapshot :: Multiplexer -> IO MultiplexerSnapshot
6564
takeSnapshot multi =
66-
MultiplexerSnapshot <$> size <*> e <*> thread
65+
MultiplexerSnapshot <$> size <*> e <*> (pack <$> thread)
6766
where
6867
size = atomically $ M.size $ channels multi
6968
thread = show <$> readMVar (producerThreadId multi)
@@ -113,7 +112,7 @@ superviseMultiplexer multi msInterval shouldRestart = do
113112
new <- reopenProducer multi
114113
void $ swapMVar (producerThreadId multi) new
115114
snapAfter <- takeSnapshot multi
116-
putStrLn $
115+
print $
117116
"Restarting producer. Multiplexer updated: "
118117
<> A.encode snapBefore
119118
<> " -> "
@@ -142,7 +141,7 @@ onMessage multi chan action = do
142141
where
143142
disposeListener _ = atomically $ do
144143
mC <- M.lookup chan (channels multi)
145-
let c = fromMaybe (panic $ "trying to remove listener from non existing channel: " <> toS chan) mC
144+
let c = fromMaybe (panic $ "trying to remove listener from non existing channel: " <> chan) mC
146145
M.delete chan (channels multi)
147146
when (listeners c - 1 > 0) $
148147
M.insert Channel {broadcast = broadcast c, listeners = listeners c - 1} chan (channels multi)

0 commit comments

Comments
 (0)