forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitLogDirect.hs
44 lines (36 loc) · 1.23 KB
/
emitLogDirect.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env stack
{- stack --install-ghc
runghc
--package amqp
--package bytestring
--package safe
--package text
-}
{-# LANGUAGE OverloadedStrings #-}
import Network.AMQP
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import qualified Data.Text as DT
import Safe (atMay)
import System.Environment (getArgs)
logsExchange = "direct_logs"
main :: IO ()
main = do
args <- getArgs
let body = bodyFor args
severity = severityFor args
conn <- openConnection "127.0.0.1" "/" "guest" "guest"
ch <- openChannel conn
declareExchange ch newExchange {exchangeName = logsExchange,
exchangeType = "direct",
exchangeDurable = False}
publishMsg ch logsExchange severity
(newMsg {msgBody = body,
msgDeliveryMode = Just NonPersistent})
BL.putStrLn $ " [x] Sent " <> body
closeConnection conn
bodyFor :: [String] -> BL.ByteString
bodyFor xs = maybe "Hello world" BL.pack (atMay xs 1)
severityFor :: [String] -> DT.Text
severityFor xs = maybe "info" DT.pack (atMay xs 0)