Skip to content

Commit

Permalink
Extract wai example and common code to another library
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Oct 2, 2020
1 parent 8ba4ec6 commit c896829
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rollbar-wai/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.stack-work/
*~
3 changes: 3 additions & 0 deletions rollbar-wai/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog for rollbar-wai

## Unreleased changes
30 changes: 30 additions & 0 deletions rollbar-wai/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Author name here (c) 2020

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Author name here nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions rollbar-wai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# rollbar-wai
2 changes: 2 additions & 0 deletions rollbar-wai/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
27 changes: 27 additions & 0 deletions rollbar-wai/examples/warp/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

module Main
( main
) where

import qualified Data.ByteString.Char8 as BS

import Network.Wai
import Network.Wai.Handler.Warp hiding (Settings)
import Rollbar.Client.Settings (Settings(..))
import Rollbar.Wai (rollbarOnException)
import System.Environment

main :: IO ()
main = do
settings <- getSettings
let warpSettings = setOnException (rollbarOnException settings) defaultSettings
runSettings warpSettings app

getSettings :: IO Settings
getSettings =
Settings <$> BS.pack <$> getEnv "ROLLBAR_TOKEN"
<*> pure "test"

app :: Application
app _ _ = error "Hello World"
63 changes: 63 additions & 0 deletions rollbar-wai/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: rollbar-wai
version: 0.1.0.0
github: "githubuser/rollbar-wai"
license: BSD3
author: "Author name here"
maintainer: "[email protected]"
copyright: "2020 Author name here"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/rollbar-wai#readme>

dependencies:
- base >= 4.7 && < 5

library:
source-dirs: src
dependencies:
- aeson
- bytestring
- case-insensitive
- http-types
- req
- rollbar-client
- text
- unordered-containers
- wai
- wai-extra

executables:
warp:
source-dirs: examples/warp
main: Main.hs
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- bytestring
- rollbar-client
- rollbar-wai
- wai
- warp

tests:
rollbar-wai-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- rollbar-wai
76 changes: 76 additions & 0 deletions rollbar-wai/rollbar-wai.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.33.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: 029b25ee4af89209b19ea5db70b84dee6cd13de9f8de212543a7b241f65e6b96

name: rollbar-wai
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/rollbar-wai#readme>
homepage: https://github.com/githubuser/rollbar-wai#readme
bug-reports: https://github.com/githubuser/rollbar-wai/issues
author: Author name here
maintainer: [email protected]
copyright: 2020 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
ChangeLog.md

source-repository head
type: git
location: https://github.com/githubuser/rollbar-wai

library
exposed-modules:
Rollbar.Wai
other-modules:
Paths_rollbar_wai
hs-source-dirs:
src
build-depends:
aeson
, base >=4.7 && <5
, bytestring
, case-insensitive
, http-types
, req
, rollbar-client
, text
, unordered-containers
, wai
, wai-extra
default-language: Haskell2010

executable warp
main-is: Main.hs
other-modules:
Paths_rollbar_wai
hs-source-dirs:
examples/warp
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, bytestring
, rollbar-client
, rollbar-wai
, wai
, warp
default-language: Haskell2010

test-suite rollbar-wai-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_rollbar_wai
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, rollbar-wai
default-language: Haskell2010
66 changes: 66 additions & 0 deletions rollbar-wai/src/Rollbar/Wai.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{-# LANGUAGE OverloadedStrings #-}

module Rollbar.Wai
( rollbarOnException
) where

import qualified Data.ByteString.Lazy as BSL
import qualified Data.CaseInsensitive as CI
import qualified Data.HashMap.Strict as HM
import qualified Data.Text.Encoding as T
import qualified Network.Wai as W
import qualified Network.Wai.Parse as W

import Control.Concurrent (forkIO)
import Control.Exception
import Control.Monad (void)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Aeson
import Data.Bool
import Network.HTTP.Req (defaultHttpConfig)
import Network.HTTP.Types (renderQuery)
import Rollbar.Client
import Rollbar.Client.Item
import Rollbar.Client.Settings

rollbarOnException :: Settings -> Maybe W.Request -> SomeException -> IO ()
rollbarOnException settings mreq ex = void $ forkIO $
runRollbar defaultHttpConfig settings $ do
rdata <- mkData $ PayloadTrace $ Trace [] $ mkExceptionFromSomeException ex
rreq <- mapM mkRequest mreq
let item = Item rdata { dataRequest = rreq }
void $ createItem item

mkRequest :: MonadIO m => W.Request -> m Request
mkRequest req = liftIO $ do
(params, _) <- W.parseRequestBody ignoreFilesBackEnd req
body <- T.decodeUtf8 . BSL.toStrict <$> W.strictRequestBody req
return Request
{ requestUrl = maybe "" toUrl $ W.requestHeaderHost req
, requestMethod = T.decodeUtf8 $ W.requestMethod req
, requestHeaders = HM.fromList $ fmap toHeader $ W.requestHeaders req
, requestParams = mempty
, requestGet = HM.fromList $ fmap toQuery $ W.queryString req
, requestQueryStrings = T.decodeUtf8 $ renderQuery False $ W.queryString req
, requestPost = HM.fromList $ fmap toParam params
, requestBody = body
, requestUserIp = ""
}
where
toUrl host = T.decodeUtf8 $ mconcat
-- TODO: guessAppRoot
[ bool "http" "https" $ W.isSecure req
, "://"
, host
, W.rawPathInfo req
, W.rawQueryString req
]
toHeader (key, value) =
(T.decodeUtf8 $ CI.original key, toJSON $ T.decodeUtf8 value)
toQuery (key, value) =
(T.decodeUtf8 key, toJSON $ T.decodeUtf8 <$> value)
toParam (key, value) =
(T.decodeUtf8 key, toJSON $ T.decodeUtf8 value)

ignoreFilesBackEnd :: W.BackEnd ()
ignoreFilesBackEnd _ _ _ = pure ()
2 changes: 2 additions & 0 deletions rollbar-wai/test/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main :: IO ()
main = putStrLn "Test suite not yet implemented"

0 comments on commit c896829

Please sign in to comment.