packages feed

servant-nix (empty) → 0.1

raw patch · 7 files changed

+187/−0 lines, 7 filesdep +basedep +bytestringdep +hnixsetup-changed

Dependencies added: base, bytestring, hnix, http-client, http-media, servant, servant-client, servant-nix, servant-server, text, wai, warp

Files

+ CHANGELOG.md view
@@ -0,0 +1,4 @@+0.1+---++* First version
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Servant Contributors++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 Julian K. Arni 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.
+ README.md view
@@ -0,0 +1,16 @@+# servant-nix++Use Nix expressions as request or response bodies in servant applications++![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png)++## Example++Run the example:++```+cabal new-test+```++It performs a simple roundtrip test for a fixed Nix expression+which is a list of numbers. See the code in `example/Main.hs`.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ example/Main.hs view
@@ -0,0 +1,35 @@+{-# LANGUAGE DataKinds, TypeOperators #-}+import Control.Concurrent+import Network.HTTP.Client hiding (Proxy)+import Network.Wai.Handler.Warp+import Nix+import Servant+import Servant.Client+import Servant.Nix++-- returns Nix expressions received as input+type IdAPI = ReqBody '[Nix] NExpr :> Post '[Nix] NExpr++api :: Proxy IdAPI+api = Proxy++server :: Server IdAPI+server = pure++postExpr :: NExpr -> ClientM NExpr+postExpr = client api++main :: IO ()+main = do+  mgr <- newManager defaultManagerSettings+  tid <- forkIO $ run 8080 (serve api server)+  threadDelay 500000+  runClientM (postExpr testExpr) (env mgr) >>= \res -> do+    killThread tid+    case res of+      Left err   -> error $ "Problem: " ++ show err+      Right expr -> putStrLn "Roundtrip successful!" >> print expr++  where testExpr = mkList [ mkInt n | n <- [1..10] ]+        env m = ClientEnv m url Nothing+        url   = BaseUrl Http "localhost" 8080 ""
+ servant-nix.cabal view
@@ -0,0 +1,58 @@+name:                servant-nix+version:             0.1+synopsis:            Servant Nix content-type+description:+  Servant Nix bindings.+  .+  Provides @MineRender@ and @MimeUnrender@ instances.+  So you can accept and return Nix expressions, with the help+  of the hnix library.+homepage:            http://haskell-servant.readthedocs.org/+license:             BSD3+license-file:        LICENSE+author:              Servant Contributors+maintainer:          haskell-servant-maintainers@googlegroups.com+copyright:           2018 Servant Contributors+category:            Web, Servant, Nix+build-type:          Simple+cabal-version:       >=1.10+bug-reports:         http://github.com/haskell-servant/servant-nix/issues+tested-with:+  GHC==7.10.3,+  GHC==8.0.2,+  GHC==8.2.2,+  GHC==8.4.3+extra-source-files: README.md+                  , CHANGELOG.md++source-repository head+  type: git+  location: http://github.com/haskell-servant/servant-nix.git++library+  exposed-modules:     Servant.Nix+  build-depends:       base          >=4.8      && <4.12+                     , bytestring    >=0.10.4.0 && <0.11+                     , servant       >=0.13     && <0.14+                     , text          >=1.2.3.0  && <1.3+                     , http-media    >= 0.7     && <0.8+                     , hnix          >= 0.5     && <0.6+  hs-source-dirs:      src+  default-language:    Haskell2010+  ghc-options: -Wall++test-suite example+  type: exitcode-stdio-1.0+  main-is: Main.hs+  hs-source-dirs: example+  ghc-options: -Wall+  build-depends: base+               , hnix+               , http-client+               , servant+               , servant-nix+               , servant-server+               , servant-client+               , wai+               , warp+  default-language: Haskell2010
+ src/Servant/Nix.hs view
@@ -0,0 +1,42 @@+{-# LANGUAGE MultiParamTypeClasses, OverloadedStrings #-}+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}+module Servant.Nix where++import Data.Text (Text)+import Network.HTTP.Media ((//))+import Nix+import Servant.API.ContentTypes++import qualified Data.ByteString.Lazy    as LBS+import qualified Data.Text.Lazy          as T+import qualified Data.Text.Lazy.Encoding as T++-- | UTF-8 text representing a Nix expression+data Nix++instance Accept Nix where+  contentType _ = "text" // "nix"++-- | 'NExpr' and 'NExprLoc's can be used as request+--   bodies in servant API types.+instance FromNix a => MimeUnrender Nix a where+  mimeUnrender _ = parseAsNix++instance MimeRender Nix NExpr where+  mimeRender _ = T.encodeUtf8 . T.pack . show . prettyNix++-- | Types that can be parsed out of the text of a Nix+--   expression, i.e 'NExpr' and 'NExprLoc'.+class FromNix a where+  fromNixText :: Text -> Result a++instance FromNix NExpr where+  fromNixText = parseNixText++instance FromNix NExprLoc where+  fromNixText = parseNixTextLoc++parseAsNix :: FromNix a => LBS.ByteString -> Either String a+parseAsNix lbs = case fromNixText (T.toStrict $ T.decodeUtf8 lbs) of+  Failure doc -> Left (show doc)+  Success a   -> pure a