rabbitmq-tutorials/haskell/emitLogTopic.hs
Erik Stevenson 3b6a5e908c Update haskell examples.
- Add support for running examples easily with `stack`.
- Fix calls made to `threadDelay` that were using milliseconds intead of
  nanoseconds.
- amqp-0.9 added a Bool parameter to the `qos` that was causing worker.hs
  to not compile.
- Removed almost all use of String in favor of Lazy ByteString.
2016-12-16 13:39:18 -05:00

45 lines
1.2 KiB
Haskell

#!/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 = "topic_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 = "topic",
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 "anonymous.info" DT.pack (atMay xs 0)