From 6cec96067510f6023801f88b41aec45848cd535f Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Fri, 30 Aug 2019 12:11:05 -0400 Subject: [PATCH] Move the server/API details away from the server run details --- PaymentServer.cabal | 1 + src/PaymentServer/Main.hs | 31 ++++------------------------- src/PaymentServer/Server.hs | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 src/PaymentServer/Server.hs diff --git a/PaymentServer.cabal b/PaymentServer.cabal index b29d04b..4bba8ee 100644 --- a/PaymentServer.cabal +++ b/PaymentServer.cabal @@ -17,6 +17,7 @@ library hs-source-dirs: src exposed-modules: PaymentServer.Processors.Stripe , PaymentServer.Persistence + , PaymentServer.Server , PaymentServer.Main build-depends: base >= 4.7 && < 5 , aeson diff --git a/src/PaymentServer/Main.hs b/src/PaymentServer/Main.hs index cf965e6..adbc78b 100644 --- a/src/PaymentServer/Main.hs +++ b/src/PaymentServer/Main.hs @@ -1,7 +1,4 @@ -{-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE DataKinds #-} -{-# LANGUAGE TypeOperators #-} - +-- | This module implements the main entrypoint to the PaymentServer. module PaymentServer.Main ( main ) where @@ -9,13 +6,6 @@ module PaymentServer.Main import Data.Default ( def ) -import Servant - ( Proxy(Proxy) - , Server - , Application - , serve - , (:>) - ) import Network.Wai.Handler.Warp ( run ) @@ -28,25 +18,12 @@ import Network.Wai.Middleware.RequestLogger.JSON ( formatAsJSON ) import PaymentServer.Persistence - ( VoucherDatabase - , memory + ( memory ) -import PaymentServer.Processors.Stripe - ( StripeAPI - , stripeServer +import PaymentServer.Server + ( paymentServerApp ) -type PaymentServerAPI = "v1" :> "stripe" :> StripeAPI - -paymentServer :: VoucherDatabase d => d -> Server PaymentServerAPI -paymentServer = stripeServer - -paymentServerAPI :: Proxy PaymentServerAPI -paymentServerAPI = Proxy - -paymentServerApp :: VoucherDatabase d => d -> Application -paymentServerApp = (serve paymentServerAPI) . paymentServer - main :: IO () main = do db <- memory diff --git a/src/PaymentServer/Server.hs b/src/PaymentServer/Server.hs new file mode 100644 index 0000000..f4ecaf3 --- /dev/null +++ b/src/PaymentServer/Server.hs @@ -0,0 +1,39 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeOperators #-} + +-- | This module exposes a Servant-based Network.Wai server for payment +-- interactions. +module PaymentServer.Server + ( paymentServerApp + ) where + +import Servant + ( Proxy(Proxy) + , Server + , Application + , serve + , (:>) + ) +import PaymentServer.Processors.Stripe + ( StripeAPI + , stripeServer + ) +import PaymentServer.Persistence + ( VoucherDatabase + ) + +-- | This is the complete type of the server API. +type PaymentServerAPI = "v1" :> "stripe" :> StripeAPI + +-- | Create a server which uses the given database. +paymentServer :: VoucherDatabase d => d -> Server PaymentServerAPI +paymentServer = stripeServer + +paymentServerAPI :: Proxy PaymentServerAPI +paymentServerAPI = Proxy + +-- | Create a Servant Application which serves the payment server API using +-- the given database. +paymentServerApp :: VoucherDatabase d => d -> Application +paymentServerApp = (serve paymentServerAPI) . paymentServer -- GitLab