helics-wai (empty) → 0.1.0
raw patch · 5 files changed
+121/−0 lines, 5 filesdep +basedep +bytestringdep +data-default-classsetup-changed
Dependencies added: base, bytestring, data-default-class, helics, helics-wai, http-types, vault, wai, warp
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- example.hs +26/−0
- helics-wai.cabal +51/−0
- src/Network/Helics/Wai.hs +22/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 HirotomoMoriwaki++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ example.hs view
@@ -0,0 +1,26 @@+{-# LANGUAGE OverloadedStrings #-}++import System.Environment+import Control.Concurrent++import Network.HTTP.Types+import Network.Wai+import Network.Wai.Handler.Warp+import Network.Helics+import Network.Helics.Wai++import qualified Data.ByteString.Char8 as S8++main :: IO ()+main = do+ key:_ <- getArgs+ withHelics def { licenseKey = S8.pack key, appName = "Test3" } $ do+ _ <- forkIO $ sampler 20+ run 3000 $ helics app++app :: Application+app req send = case pathInfo req of+ [] -> threadDelay (10^(5::Int)) >> send (responseLBS status200 [] "root")+ ["foo"] -> threadDelay (2 * 10^(5::Int)) >> send (responseLBS status200 [] "foo")+ ["bar"] -> datastoreSegment autoScope (DatastoreSegment "bar" "select" "SELECT * FROM bar WHERE key = 'baz'" "select bars" Nothing) (threadDelay (3 * 10^(5::Int)) >> send (responseLBS status200 [] "bar")) (transactionId req)+ _ -> send (responseLBS status404 [] "not found")
+ helics-wai.cabal view
@@ -0,0 +1,51 @@+name: helics-wai+version: 0.1.0+synopsis: New Relic® agent SDK wrapper for wai.+description: + New Relic® agent SDK wrapper for wai.+ .+ Please read example: <https://github.com/philopon/helics/blob/master/helics-wai/example.hs>.+license: MIT+license-file: LICENSE+author: HirotomoMoriwaki<philopon.dependence@gmail.com>+maintainer: HirotomoMoriwaki<philopon.dependence@gmail.com>+Homepage: https://github.com/philopon/apiary+Bug-reports: https://github.com/philopon/apiary/issues+copyright: (c) 2014 Hirotomo Moriwaki+category: Network+build-type: Simple+stability: experimental+cabal-version: >=1.10++flag example+ default: False++library+ exposed-modules: Network.Helics.Wai+ -- other-extensions: + build-depends: base >=4.6 && <4.8+ , data-default-class >=0.0 && <0.1+ , wai >=3.0 && <3.1+ , vault >=0.3 && <0.4+ , helics >=0.1 && <0.2+ , bytestring >=0.10 && <0.11+ ghc-options: -Wall -O2+ hs-source-dirs: src+ default-language: Haskell2010++executable helics-wai-example+ main-is: example.hs+ if flag(example)+ build-depends: base >=4.7 && <4.8+ , wai+ , helics-wai+ , warp+ , helics+ , vault+ , http-types+ , bytestring+ buildable: True+ else+ buildable: False+ ghc-options: -Wall -O2 -threaded+ default-language: Haskell2010
+ src/Network/Helics/Wai.hs view
@@ -0,0 +1,22 @@+module Network.Helics.Wai (helics, transactionId) where++import Network.Wai+import Data.Vault.Lazy as V+import Network.Helics+import qualified Data.ByteString as S+import System.IO.Unsafe++tidKey :: Key TransactionId+tidKey = unsafePerformIO newKey+{-# NOINLINE tidKey #-}++-- | helics middleware.+helics :: Middleware+helics app req send =+ withTransaction (rawPathInfo req) def $ \tid -> do+ setRequestUrl (rawPathInfo req) tid+ app req { vault = insert tidKey tid (vault req) } send++-- | get TransactionId from request.+transactionId :: Request -> TransactionId+transactionId req = maybe (error "helics middleware is not installed.") id $ V.lookup tidKey (vault req)