Merge pull request #7 from zerobuzz/2015-01-06-explicit-import-lists
Explicit import lists
This commit is contained in:
commit
9ef336b50f
5 changed files with 65 additions and 62 deletions
|
@ -11,10 +11,12 @@ module Servant.Server
|
||||||
HasServer(..)
|
HasServer(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Proxy
|
import Data.Proxy (Proxy)
|
||||||
import Network.Wai
|
import Network.Wai (Application)
|
||||||
|
|
||||||
import Servant.Server.Internal
|
import Servant.Server.Internal
|
||||||
|
|
||||||
|
|
||||||
-- * Implementing Servers
|
-- * Implementing Servers
|
||||||
|
|
||||||
-- | 'serve' allows you to implement an API and produce a wai 'Application'.
|
-- | 'serve' allows you to implement an API and produce a wai 'Application'.
|
||||||
|
@ -39,4 +41,3 @@ import Servant.Server.Internal
|
||||||
-- > main = Network.Wai.Handler.Warp.run 8080 app
|
-- > main = Network.Wai.Handler.Warp.run 8080 app
|
||||||
serve :: HasServer layout => Proxy layout -> Server layout -> Application
|
serve :: HasServer layout => Proxy layout -> Server layout -> Application
|
||||||
serve p server = toApplication (route p server)
|
serve p server = toApplication (route p server)
|
||||||
|
|
||||||
|
|
|
@ -7,24 +7,26 @@
|
||||||
{-# LANGUAGE ScopedTypeVariables #-}
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
module Servant.Server.Internal where
|
module Servant.Server.Internal where
|
||||||
|
|
||||||
import Control.Applicative
|
import Control.Applicative ((<$>))
|
||||||
import Control.Monad.Trans.Either
|
import Control.Monad.Trans.Either (EitherT, runEitherT)
|
||||||
import Data.Aeson
|
import Data.Aeson (ToJSON, FromJSON, encode, decode')
|
||||||
import qualified Data.ByteString as B
|
import qualified Data.ByteString as B
|
||||||
import qualified Data.ByteString.Lazy as BL
|
import qualified Data.ByteString.Lazy as BL
|
||||||
import Data.IORef
|
import Data.IORef (newIORef, readIORef, writeIORef)
|
||||||
import Data.Maybe (catMaybes)
|
import Data.Maybe (catMaybes)
|
||||||
import Data.Monoid
|
import Data.Monoid (Monoid, mempty, mappend)
|
||||||
import Data.Proxy
|
import Data.Proxy (Proxy(Proxy))
|
||||||
import Data.String
|
import Data.String (fromString)
|
||||||
import Data.String.Conversions
|
import Data.String.Conversions (cs, (<>))
|
||||||
import Data.Text.Encoding (decodeUtf8)
|
import Data.Text.Encoding (decodeUtf8)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import GHC.TypeLits
|
import GHC.TypeLits (KnownSymbol, symbolVal)
|
||||||
import Network.HTTP.Types hiding (Header)
|
import Network.HTTP.Types hiding (Header)
|
||||||
import Network.Wai
|
import Network.Wai (Response, Request, ResponseReceived, Application, pathInfo, requestBody,
|
||||||
import Servant.API
|
strictRequestBody, lazyRequestBody, requestHeaders, requestMethod,
|
||||||
import Servant.Common.Text
|
rawQueryString, responseLBS)
|
||||||
|
import Servant.API (QueryParams, QueryParam, QueryFlag, ReqBody, Header, Capture, Get, Delete, Put, Post, Raw, (:>), (:<|>)(..))
|
||||||
|
import Servant.Common.Text (FromText, fromText)
|
||||||
|
|
||||||
data ReqBodyState = Uncalled
|
data ReqBodyState = Uncalled
|
||||||
| Called !B.ByteString
|
| Called !B.ByteString
|
||||||
|
|
|
@ -7,9 +7,9 @@ module Servant.Utils.StaticFiles (
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Filesystem.Path.CurrentOS (decodeString)
|
import Filesystem.Path.CurrentOS (decodeString)
|
||||||
import Network.Wai.Application.Static
|
import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
|
||||||
import Servant.API.Raw
|
import Servant.API.Raw (Raw)
|
||||||
import Servant.Server.Internal
|
import Servant.Server.Internal (Server)
|
||||||
|
|
||||||
-- | Serve anything under the specified directory as a 'Raw' endpoint.
|
-- | Serve anything under the specified directory as a 'Raw' endpoint.
|
||||||
--
|
--
|
||||||
|
|
|
@ -8,28 +8,28 @@
|
||||||
module Servant.ServerSpec where
|
module Servant.ServerSpec where
|
||||||
|
|
||||||
|
|
||||||
import Control.Monad.Trans.Either
|
import Control.Monad.Trans.Either (EitherT, left)
|
||||||
import Data.Aeson
|
import Data.Aeson (ToJSON, FromJSON, encode, decode')
|
||||||
import Data.Char
|
import Data.Char (toUpper)
|
||||||
import Data.Proxy
|
import Data.Proxy (Proxy(Proxy))
|
||||||
import Data.String
|
import Data.String (fromString)
|
||||||
import Data.String.Conversions
|
import Data.String.Conversions (cs)
|
||||||
import GHC.Generics
|
import GHC.Generics (Generic)
|
||||||
import Network.HTTP.Types
|
import Network.HTTP.Types (parseQuery, ok200)
|
||||||
import Network.Wai
|
import Network.Wai (Application, Request, responseLBS, pathInfo, queryString, rawQueryString)
|
||||||
import Network.Wai.Test
|
import Network.Wai.Test (runSession, request, defaultRequest, simpleBody)
|
||||||
import Test.Hspec
|
import Test.Hspec (Spec, describe, it, shouldBe)
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai (liftIO, with, get, post, shouldRespondWith, matchStatus)
|
||||||
|
|
||||||
import Servant.API.Capture
|
import Servant.API.Capture (Capture)
|
||||||
import Servant.API.Get
|
import Servant.API.Get (Get)
|
||||||
import Servant.API.ReqBody
|
import Servant.API.ReqBody (ReqBody)
|
||||||
import Servant.API.Post
|
import Servant.API.Post (Post)
|
||||||
import Servant.API.QueryParam
|
import Servant.API.QueryParam (QueryParam, QueryParams, QueryFlag)
|
||||||
import Servant.API.Raw
|
import Servant.API.Raw (Raw)
|
||||||
import Servant.API.Sub
|
import Servant.API.Sub ((:>))
|
||||||
import Servant.API.Alternative
|
import Servant.API.Alternative ((:<|>)((:<|>)))
|
||||||
import Servant.Server
|
import Servant.Server (Server, serve)
|
||||||
|
|
||||||
|
|
||||||
-- * test data types
|
-- * test data types
|
||||||
|
@ -94,8 +94,8 @@ captureSpec = do
|
||||||
|
|
||||||
with (return (serve
|
with (return (serve
|
||||||
(Proxy :: Proxy (Capture "captured" String :> Raw))
|
(Proxy :: Proxy (Capture "captured" String :> Raw))
|
||||||
(\ "captured" request respond ->
|
(\ "captured" request_ respond ->
|
||||||
respond $ responseLBS ok200 [] (cs $ show $ pathInfo request)))) $ do
|
respond $ responseLBS ok200 [] (cs $ show $ pathInfo request_)))) $ do
|
||||||
it "strips the captured path snippet from pathInfo" $ do
|
it "strips the captured path snippet from pathInfo" $ do
|
||||||
get "/captured/foo" `shouldRespondWith` (fromString (show ["foo" :: String]))
|
get "/captured/foo" `shouldRespondWith` (fromString (show ["foo" :: String]))
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ qpServer = queryParamServer :<|> qpNames :<|> qpCapitalize
|
||||||
qpCapitalize False = return alice
|
qpCapitalize False = return alice
|
||||||
qpCapitalize True = return alice { name = map toUpper (name alice) }
|
qpCapitalize True = return alice { name = map toUpper (name alice) }
|
||||||
|
|
||||||
queryParamServer (Just name) = return alice{name = name}
|
queryParamServer (Just name_) = return alice{name = name_}
|
||||||
queryParamServer Nothing = return alice
|
queryParamServer Nothing = return alice
|
||||||
|
|
||||||
queryParamSpec :: Spec
|
queryParamSpec :: Spec
|
||||||
|
@ -222,7 +222,7 @@ type RawApi = "foo" :> Raw
|
||||||
rawApi :: Proxy RawApi
|
rawApi :: Proxy RawApi
|
||||||
rawApi = Proxy
|
rawApi = Proxy
|
||||||
rawApplication :: Show a => (Request -> a) -> Application
|
rawApplication :: Show a => (Request -> a) -> Application
|
||||||
rawApplication f request respond = respond $ responseLBS ok200 [] (cs $ show $ f request)
|
rawApplication f request_ respond = respond $ responseLBS ok200 [] (cs $ show $ f request_)
|
||||||
|
|
||||||
rawSpec :: Spec
|
rawSpec :: Spec
|
||||||
rawSpec = do
|
rawSpec = do
|
||||||
|
@ -264,7 +264,7 @@ unionSpec = do
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
decode' (simpleBody response) `shouldBe`
|
decode' (simpleBody response) `shouldBe`
|
||||||
Just alice
|
Just alice
|
||||||
response <- get "/bar"
|
response_ <- get "/bar"
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
decode' (simpleBody response) `shouldBe`
|
decode' (simpleBody response_) `shouldBe`
|
||||||
Just jerry
|
Just jerry
|
||||||
|
|
|
@ -5,22 +5,22 @@
|
||||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||||
module Servant.Utils.StaticFilesSpec where
|
module Servant.Utils.StaticFilesSpec where
|
||||||
|
|
||||||
import Control.Exception
|
import Control.Exception (bracket)
|
||||||
import Data.Proxy
|
import Data.Proxy (Proxy(Proxy))
|
||||||
import Network.Wai
|
import Network.Wai (Application)
|
||||||
import System.Directory
|
import System.Directory (getCurrentDirectory, setCurrentDirectory, createDirectory)
|
||||||
import System.IO.Temp
|
import System.IO.Temp (withSystemTempDirectory)
|
||||||
import Test.Hspec hiding (pending)
|
import Test.Hspec (Spec, describe, it, around_)
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai (with, get, shouldRespondWith)
|
||||||
|
|
||||||
import Servant.API.Alternative
|
import Servant.API.Alternative ((:<|>)((:<|>)))
|
||||||
import Servant.API.Capture
|
import Servant.API.Capture (Capture)
|
||||||
import Servant.API.Get
|
import Servant.API.Get (Get)
|
||||||
import Servant.API.Raw
|
import Servant.API.Raw (Raw)
|
||||||
import Servant.API.Sub
|
import Servant.API.Sub ((:>))
|
||||||
import Servant.Server
|
import Servant.Server (Server, serve)
|
||||||
import Servant.ServerSpec
|
import Servant.ServerSpec (Person(Person))
|
||||||
import Servant.Utils.StaticFiles
|
import Servant.Utils.StaticFiles (serveDirectory)
|
||||||
|
|
||||||
type Api =
|
type Api =
|
||||||
"dummy_api" :> Capture "person_name" String :> Get Person
|
"dummy_api" :> Capture "person_name" String :> Get Person
|
||||||
|
@ -35,7 +35,7 @@ app = serve api server
|
||||||
|
|
||||||
server :: Server Api
|
server :: Server Api
|
||||||
server =
|
server =
|
||||||
(\ name -> return (Person name 42))
|
(\ name_ -> return (Person name_ 42))
|
||||||
:<|> serveDirectory "static"
|
:<|> serveDirectory "static"
|
||||||
|
|
||||||
withStaticFiles :: IO () -> IO ()
|
withStaticFiles :: IO () -> IO ()
|
||||||
|
|
Loading…
Reference in a new issue