packages feed

legion-discovery-client 0.1.0.2 → 0.1.0.3

raw patch · 3 files changed

+75/−12 lines, 3 filesdep +resourcetdep +transformers

Dependencies added: resourcet, transformers

Files

+ README.md view
@@ -0,0 +1,61 @@+# legion-discovery-client++This is a Haskell client library used to communicate with the Legion+Discovery discovery service.+++## Performing a query without registering a service.++If you want to perform a query without registering yourself a service,+the following example is how you might typically accomplish that. Note+that even though you are not starting a service, you must still specify+a name and a version for the local program. The reason for this is+that Legion Discovery keeps track of all requests in order to build a+dependency graph of your service ecosystem.++```haskell+import Network.HTTP.Client as C+import OM.Discovery (connect, query)++...++let+  name = "my-program" {- the name of this client program. -}+  version = "0.1" {- the version of this client program.  -}+  targetService = "some-service" {- the name of the service you are looking for. -}+  targetRange = "> 1.1.1 && < 1.2" {- the range of acceptable versions you wish to find. -}+in do+  manager <- C.newManager C.defaultManagerSettings+  discovery <- connect name version manager++  servicesInstances <- query targetService targetRange discovery+  ...+```+++## Registering a service.++The following example shows how to register a service, using+`withService`.++```haskell+import Network.HTTP.Client as C+import OM.Discovery (connect, withService)++...++let+  name = "my-program" {- the name of this client program. -}+  version = "0.1" {- the version of this client program.  -}+  serviceAddr = "http://ec2-foo-bar.amazon.com:8080" {- the address on which your service is running. -}+in do+  manager <- C.newManager C.defaultManagerSettings+  discovery <- connect name version manager++  withService serviceAddr discovery $ do+    {-+      Your service code here. When this IO block exits (for any reason,+      including exceptions) then the service will unregister itself.+    -}+```+
legion-discovery-client.cabal view
@@ -1,5 +1,5 @@ name:                legion-discovery-client-version:             0.1.0.2+version:             0.1.0.3 synopsis:            Client library for communicating with legion-discovery. description:         Please see README.md homepage:            https://github.com/owensmurray/legion-discovery-client#readme@@ -10,7 +10,8 @@ copyright:           2016 Rick Owens category:            Web build-type:          Simple--- extra-source-files:+extra-source-files:+  README.md cabal-version:       >=1.10  library@@ -29,7 +30,9 @@     http-types         >= 0.9.1    && < 0.10,     load-balancing     >= 1.0      && < 1.1,     network            >= 2.6.3.1  && < 2.7,-    text               >= 1.2.2.1  && < 1.3+    resourcet          >= 1.1.7.5  && < 1.2,+    text               >= 1.2.2.1  && < 1.3,+    transformers       >= 0.4.2.0  && < 0.6   default-language:     Haskell2010 
src/Network/Legion/Discovery/Client.hs view
@@ -26,8 +26,9 @@   forkIO, threadDelay) import Control.Concurrent.LoadDistribution (withResource,   evenlyDistributed, LoadBalanced)-import Control.Exception (bracket) import Control.Monad (void)+import Control.Monad.Trans.Class (lift)+import Control.Monad.Trans.Resource (runResourceT, allocate) import Data.Aeson (eitherDecode, Value, encode, object, (.=)) import Data.ByteString.Lazy (fromChunks) import Data.Default.Class (def)@@ -89,8 +90,10 @@   -> IO a     {- ^ The IO action to perform while registered as a service. -}   -> IO a-withService addy d =-    bracket launchPing stopPing . const+withService addy d io =+    runResourceT $ do+      void $ allocate launchPing stopPing+      lift io   where     launchPing :: IO (MVar ())     launchPing = do@@ -113,11 +116,7 @@         userAgent = encodeUtf8           $ unName (dName d) <> "/" <> pack (display (dVersion d))         req = def {-            path = -              "/v1/ping/"-              <> urlEncode False (encodeUtf8 (unName (dName d)))-              <> "/"-              <> urlEncode False (encodeUtf8 (pack (display (dVersion d)))),+            path = "/v1/ping",             method = "POST",             requestHeaders = [                 ("user-agent", userAgent),@@ -128,7 +127,7 @@                 "serviceAddress" .= unServiceAddr addy               ]           }-      {- TODO figure out what we want to do when the ping failes. -}+      {- TODO figure out what we want to do when the ping fails. -}       withResponse req (dLb d) (const (return ()))      tenSeconds :: Int