Skip to content

Commit

Permalink
Merge pull request #66 from well-typed/edsko/issue65
Browse files Browse the repository at this point in the history
Use `PortNumber` throughout
  • Loading branch information
edsko authored Jan 25, 2024
2 parents 7645559 + c911db1 commit aa8314a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
5 changes: 3 additions & 2 deletions demo-client/Demo/Client/Cmdline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions demo-server/Demo/Server/Cmdline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions grapesy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/Network/GRPC/Server/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
--
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Network/GRPC/Spec/PseudoHeaders.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
--
Expand Down
4 changes: 2 additions & 2 deletions test-common/Test/Util/ClientServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit aa8314a

Please sign in to comment.