From c911db1d7bdbc28bc9e459d6532375ccd6cf2d5e Mon Sep 17 00:00:00 2001 From: Edsko de Vries Date: Thu, 25 Jan 2024 14:46:02 +0100 Subject: [PATCH] Use `PortNumber` throughout Closes #65 --- demo-client/Demo/Client/Cmdline.hs | 5 +++-- demo-server/Demo/Server/Cmdline.hs | 6 +++--- grapesy.cabal | 1 + src/Network/GRPC/Server/Run.hs | 14 +++++++++----- src/Network/GRPC/Spec/PseudoHeaders.hs | 5 +++-- test-common/Test/Util/ClientServer.hs | 4 ++-- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/demo-client/Demo/Client/Cmdline.hs b/demo-client/Demo/Client/Cmdline.hs index f006fa82..22066a4a 100644 --- a/demo-client/Demo/Client/Cmdline.hs +++ b/demo-client/Demo/Client/Cmdline.hs @@ -27,6 +27,7 @@ import Data.ProtoLens import Data.ProtoLens.Labels () import Data.Text (Text) import GHC.TypeLits (Symbol) +import Network.Socket (HostName, PortNumber) import Options.Applicative qualified as Opt import Network.GRPC.Client qualified as Client @@ -135,8 +136,8 @@ parseServer = ]) where mkServer :: - String -- Host - -> Maybe Word -- Port + HostName -- Host + -> Maybe PortNumber -- Port -> Maybe Client.ServerValidation -- Secure? -> Maybe String -> Client.Server diff --git a/demo-server/Demo/Server/Cmdline.hs b/demo-server/Demo/Server/Cmdline.hs index 4aa2ca8d..319533ab 100644 --- a/demo-server/Demo/Server/Cmdline.hs +++ b/demo-server/Demo/Server/Cmdline.hs @@ -6,7 +6,7 @@ module Demo.Server.Cmdline ( import Data.Foldable (asum) import Options.Applicative qualified as Opt -import Network.Socket (ServiceName, PortNumber) +import Network.Socket (PortNumber) import Network.GRPC.Common import Network.GRPC.Server.Run @@ -57,13 +57,13 @@ parseInsecure = asum [ , Opt.help "Disable insecure server (without TLS)" ] , cfg - <$> Opt.option Opt.str (mconcat [ + <$> Opt.option Opt.auto (mconcat [ Opt.long "port-insecure" , Opt.help "Port number for the insecure server (without TLS)" ]) ] where - cfg :: ServiceName -> Maybe InsecureConfig + cfg :: PortNumber -> Maybe InsecureConfig cfg port = Just InsecureConfig { insecureHost = Nothing , insecurePort = port diff --git a/grapesy.cabal b/grapesy.cabal index 6bd0a392..96b918ba 100644 --- a/grapesy.cabal +++ b/grapesy.cabal @@ -283,6 +283,7 @@ executable demo-client , contra-tracer >= 0.2 && < 0.3 , data-default >= 0.7 && < 0.8 , lens >= 5.0 && < 5.3 + , network >= 3.1 && < 3.2 , optparse-applicative >= 0.16 && < 0.19 , pipes >= 4.3 && < 4.4 , pipes-safe >= 2.3 && < 2.4 diff --git a/src/Network/GRPC/Server/Run.hs b/src/Network/GRPC/Server/Run.hs index 88433541..aa984ae4 100644 --- a/src/Network/GRPC/Server/Run.hs +++ b/src/Network/GRPC/Server/Run.hs @@ -17,7 +17,7 @@ import Network.ByteOrder (BufferSize) import Network.HTTP2.Server qualified as HTTP2 import Network.HTTP2.TLS.Server qualified as HTTP2.TLS import Network.Run.TCP (runTCPServer) -import Network.Socket (HostName, ServiceName, PortNumber) +import Network.Socket (HostName, PortNumber) import Network.TLS qualified as TLS import Network.GRPC.Server (mkGrpcServer, ServerParams, RpcHandler) @@ -30,9 +30,13 @@ import Debug.Concurrent Configuration -------------------------------------------------------------------------------} --- | Describes the configuration of both an insecure server and a secure server. +-- | Server configuration +-- +-- Describes the configuration of both an insecure server and a secure server. -- See the documentation of 'runServer' for a description of what servers will -- result from various configurations. +-- +-- The default configuration enables the default insecure configuration only. data ServerConfig = ServerConfig { -- | Configuration for insecure communication (without TLS) -- @@ -59,14 +63,14 @@ data InsecureConfig = InsecureConfig { insecureHost :: Maybe HostName -- | Port number - , insecurePort :: ServiceName + , insecurePort :: PortNumber } deriving (Show) instance Default InsecureConfig where def = InsecureConfig { insecureHost = Nothing - , insecurePort = "50051" + , insecurePort = 50051 } -- | Offer secure connection (over TLS) @@ -128,7 +132,7 @@ runServerWithHandlers config params handlers = do runInsecure :: HTTP2.Server -> InsecureConfig -> IO () runInsecure server cfg = - runTCPServer (insecureHost cfg) (insecurePort cfg) $ \sock -> do + runTCPServer (insecureHost cfg) (show $ insecurePort cfg) $ \sock -> do bracket (HTTP2.allocSimpleConfig sock writeBufferSize) HTTP2.freeSimpleConfig $ \config -> HTTP2.run HTTP2.defaultServerConfig config server diff --git a/src/Network/GRPC/Spec/PseudoHeaders.hs b/src/Network/GRPC/Spec/PseudoHeaders.hs index d7ccdf07..766ff893 100644 --- a/src/Network/GRPC/Spec/PseudoHeaders.hs +++ b/src/Network/GRPC/Spec/PseudoHeaders.hs @@ -30,6 +30,7 @@ import Data.Hashable (Hashable) import Data.Proxy import Data.Text (Text) import GHC.Generics qualified as GHC +import Network.Socket (HostName, PortNumber) import Network.GRPC.Spec.PercentEncoding qualified as PercentEncoding import Network.GRPC.Spec.RPC @@ -92,10 +93,10 @@ data Scheme = Http | Https -- [pseudo-header](https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.3). data Address = Address { -- | Hostname - addressHost :: String + addressHost :: HostName -- | TCP port - , addressPort :: Word + , addressPort :: PortNumber -- | Authority -- diff --git a/test-common/Test/Util/ClientServer.hs b/test-common/Test/Util/ClientServer.hs index 9dd67911..f1ea3e77 100644 --- a/test-common/Test/Util/ClientServer.hs +++ b/test-common/Test/Util/ClientServer.hs @@ -372,14 +372,14 @@ runTestServer cfg serverTracer handlerLock serverHandlers = do Nothing -> Server.ServerConfig { serverInsecure = Just Server.InsecureConfig { insecureHost = Nothing - , insecurePort = "50051" + , insecurePort = 50051 } , serverSecure = Nothing } Just (TlsFail TlsFailUnsupported) -> Server.ServerConfig { serverInsecure = Just Server.InsecureConfig { insecureHost = Nothing - , insecurePort = "50052" + , insecurePort = 50052 } , serverSecure = Nothing }