Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:46
Show Gist options
  • Save rcook/50505fbf45dc006f31215c0e88da8ce8 to your computer and use it in GitHub Desktop.
Save rcook/50505fbf45dc006f31215c0e88da8ce8 to your computer and use it in GitHub Desktop.
AWS via Haskell Part 3 (SQS)
name: aws-via-haskell
version: 0.1.0.0
homepage: https://github.com/rcook/aws-via-haskell#readme
license: MIT
license-file: LICENSE
author: Richard Cook
maintainer: [email protected]
copyright: 2017 Richard Cook
category: Command Line
build-type: Simple
cabal-version: >= 1.10
extra-source-files: README.md
source-repository head
type: git
location: https://github.com/rcook/aws-via-haskell.git
library
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: lib
ghc-options: -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, base >= 4.7 && < 5
, bytestring
, lens
, resourcet
, text
exposed-modules: AWSViaHaskell
, AWSViaHaskell.AWSInfo
, AWSViaHaskell.Util
executable dynamodb-app
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: dynamodb
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, amazonka-dynamodb
, aws-via-haskell
, base >= 4.7 && < 5
, lens
, text
, unordered-containers
executable s3-app
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: s3
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka
, amazonka-s3
, aws-via-haskell
, base >= 4.7 && < 5
, bytestring
, conduit-extra
, lens
, resourcet
, text
executable sqs-app
default-language: Haskell2010
if os(darwin)
cpp-options: -DOS_MACOS
if os(linux)
cpp-options: -DOS_LINUX
if os(windows)
cpp-options: -DOS_WINDOWS
hs-source-dirs: sqs
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N -W -Wall -fwarn-incomplete-patterns -fwarn-unused-imports
build-depends: amazonka-sqs
, aws-via-haskell
, base >= 4.7 && < 5
The MIT License (MIT)
Copyright (c) 2017 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Main (main) where
import AWSViaHaskell
( AWSInfo
, LoggingState(..)
, ServiceEndpoint(..)
, getAWSInfo
, withAWS
)
import Control.Exception.Lens (handling)
import Control.Lens ((^.))
import Control.Monad (forM_, void)
import Data.Monoid ((<>))
import Data.Text (Text)
import qualified Data.Text.IO as Text
import Network.AWS (send)
import Network.AWS.SQS
( _QueueDoesNotExist
, createQueue
, getQueueURL
, gqursQueueURL
, listQueues
, lqrsQueueURLs
, mBody
, receiveMessage
, rmrsMessages
, sendMessage
, sqs
)
newtype QueueName = QueueName Text deriving Show
newtype QueueURL = QueueURL Text deriving Show
doListQueues :: AWSInfo -> IO [Text]
doListQueues = withAWS $ do
result <- send $ listQueues
return $ result ^. lqrsQueueURLs
doCreateQueue :: QueueName -> AWSInfo -> IO ()
doCreateQueue (QueueName queueName) = withAWS (void $ send $ createQueue queueName)
doGetQueueURL :: QueueName -> AWSInfo -> IO (Maybe QueueURL)
doGetQueueURL (QueueName queueName) = withAWS $ do
handling _QueueDoesNotExist (const (pure Nothing)) $ do
result <- send $ getQueueURL queueName
return $ Just (QueueURL $ result ^. gqursQueueURL)
doSendMessage :: QueueURL -> Text -> AWSInfo -> IO ()
doSendMessage (QueueURL s) m = withAWS $ do
void $ send $ sendMessage s m
doReceiveMessage :: QueueURL -> AWSInfo -> IO (Maybe Text)
doReceiveMessage (QueueURL s) = withAWS $ do
result <- send $ receiveMessage s
case result ^. rmrsMessages of
m : [] -> return $ m ^. mBody
_ -> return Nothing
main :: IO ()
main = do
let queueName = QueueName "my-queue"
awsInfo <- getAWSInfo LoggingDisabled (Local "localhost" 4576) sqs
putStrLn "CreateQueue"
doCreateQueue queueName awsInfo
putStrLn "ListQueues"
queueURLs <- doListQueues awsInfo
forM_ queueURLs $ \queueURL ->
Text.putStrLn $ " " <> queueURL
putStrLn "GetQueueURL"
mbQueueURL <- doGetQueueURL queueName awsInfo
case mbQueueURL of
Nothing -> Text.putStrLn " (not found)"
Just queueURL -> do
putStrLn $ " " <> show queueURL
putStrLn "SendMessage"
doSendMessage queueURL "a message" awsInfo
putStrLn "ReceiveMessage"
m <- doReceiveMessage queueURL awsInfo
putStrLn $ " " <> show m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment