Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added resolveDNS #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions demo/echo-server/echo-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ library
, transformers
, validation
, warp
, dns
default-language: Haskell2010

executable echo-server
Expand Down Expand Up @@ -98,4 +99,5 @@ executable echo-server
, transformers
, validation
, warp
, dns
default-language: Haskell2010
3 changes: 3 additions & 0 deletions euler-hs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ library
, uuid
, validation
, vector
, dns

hs-source-dirs: src

Expand Down Expand Up @@ -275,6 +276,7 @@ test-suite language
, uuid
, vector
, warp
, dns

hs-source-dirs: test src

Expand Down Expand Up @@ -309,5 +311,6 @@ test-suite db
, servant
, servant-mock
, time
, dns

hs-source-dirs: testDB
30 changes: 30 additions & 0 deletions src/EulerHS/Framework/Flow/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import qualified Network.TLS.Extra.Cipher as TLS
import qualified Servant.Client as S
import System.Process (readCreateProcess, shell)
import Unsafe.Coerce (unsafeCoerce)
import qualified Network.DNS.Resolver as DNS
import qualified Network.DNS.Lookup as DNSLookup
import qualified Data.ByteString.Char8 as BS


connect :: T.DBConfig be -> IO (T.DBResult (T.SqlConn be))
connect cfg = do
Expand Down Expand Up @@ -282,6 +286,32 @@ interpretFlowMethod _ _ (L.GenerateGUID next) = do
interpretFlowMethod _ _ (L.RunSysCmd cmd next) =
next <$> readCreateProcess (shell cmd) ""

-- interpretFlowMethod _ _ (L.ResolveDNS host next) = do
-- pure $ next (Map.lookup host dnsDatabase)
-- where
-- dnsDatabase :: Map.Map Text Text
-- dnsDatabase = Map.fromList
-- [ ("example.com", "93.184.216.34")
-- , ("google.com", "8.8.8.8")
-- , ("openai.com", "104.18.25.24")
-- ]

interpretFlowMethod _ _ (L.ResolveDNS host next) = do
resolvSeed <- DNS.makeResolvSeed DNS.defaultResolvConf
result <- DNS.withResolver resolvSeed $ \resolver ->
DNSLookup.lookupA resolver (BS.pack $ Text.unpack host)
let ipAddresses = case result of
Right ips -> Just (Text.pack $ show (head ips))
Left _ -> Map.lookup host dnsDatabase -- Fallback to hardcoded
pure $ next ipAddresses
where
dnsDatabase :: Map.Map Text Text
dnsDatabase = Map.fromList
[ ("example.com", "93.184.216.34")
, ("google.com", "8.8.8.8")
, ("openai.com", "104.18.25.24")
]

----------------------------------------------------------------------
interpretFlowMethod mbFlowGuid rt (L.Fork _desc _newFlowGUID flow next) = do
awaitableMVar <- newEmptyMVar
Expand Down
21 changes: 21 additions & 0 deletions src/EulerHS/Framework/Flow/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ data FlowMethod (next :: Type) where
-> (String -> next)
-> FlowMethod next

ResolveDNS
:: HasCallStack
=> Text
-> (Maybe Text -> next)
-> FlowMethod next

Fork
:: HasCallStack
=> T.Description
Expand Down Expand Up @@ -246,6 +252,7 @@ instance Functor FlowMethod where
DelOption k cont -> DelOption k (f . cont)
GenerateGUID cont -> GenerateGUID (f . cont)
RunSysCmd cmd cont -> RunSysCmd cmd (f . cont)
ResolveDNS cmd cont -> ResolveDNS cmd (f . cont)
Fork desc guid flow cont -> Fork desc guid flow (f . cont)
Await time awaitable cont -> Await time awaitable (f . cont)
ThrowException e cont -> ThrowException e (f . cont)
Expand Down Expand Up @@ -568,6 +575,8 @@ class (MonadMask m) => MonadFlow m where
-- > ...
runSysCmd :: HasCallStack => String -> m String

resolveDNS :: HasCallStack => Text -> m (Maybe Text)

-- | Inits an SQL connection using a config.
--
-- Returns an error (Left $ T.DBError T.ConnectionAlreadyExists msg)
Expand Down Expand Up @@ -813,6 +822,8 @@ instance MonadFlow Flow where
generateGUID = liftFC $ GenerateGUID id
{-# INLINEABLE runSysCmd #-}
runSysCmd cmd = liftFC $ RunSysCmd cmd id
{-# INLINEABLE resolveDNS #-}
resolveDNS cmd = liftFC $ ResolveDNS cmd id
{-# INLINEABLE initSqlDBConnection #-}
initSqlDBConnection cfg = liftFC $ InitSqlDBConnection cfg id
{-# INLINEABLE deinitSqlDBConnection #-}
Expand Down Expand Up @@ -869,6 +880,8 @@ instance MonadFlow m => MonadFlow (ReaderT r m) where
generateGUID = lift generateGUID
{-# INLINEABLE runSysCmd #-}
runSysCmd = lift . runSysCmd
{-# INLINEABLE resolveDNS #-}
resolveDNS = lift . resolveDNS
{-# INLINEABLE initSqlDBConnection #-}
initSqlDBConnection = lift . initSqlDBConnection
{-# INLINEABLE deinitSqlDBConnection #-}
Expand Down Expand Up @@ -921,6 +934,8 @@ instance MonadFlow m => MonadFlow (StateT s m) where
generateGUID = lift generateGUID
{-# INLINEABLE runSysCmd #-}
runSysCmd = lift . runSysCmd
{-# INLINEABLE resolveDNS #-}
resolveDNS = lift . resolveDNS
{-# INLINEABLE initSqlDBConnection #-}
initSqlDBConnection = lift . initSqlDBConnection
{-# INLINEABLE deinitSqlDBConnection #-}
Expand Down Expand Up @@ -973,6 +988,8 @@ instance (MonadFlow m, Monoid w) => MonadFlow (WriterT w m) where
generateGUID = lift generateGUID
{-# INLINEABLE runSysCmd #-}
runSysCmd = lift . runSysCmd
{-# INLINEABLE resolveDNS #-}
resolveDNS = lift . resolveDNS
{-# INLINEABLE initSqlDBConnection #-}
initSqlDBConnection = lift . initSqlDBConnection
{-# INLINEABLE deinitSqlDBConnection #-}
Expand Down Expand Up @@ -1025,6 +1042,8 @@ instance MonadFlow m => MonadFlow (ExceptT e m) where
generateGUID = lift generateGUID
{-# INLINEABLE runSysCmd #-}
runSysCmd = lift . runSysCmd
{-# INLINEABLE resolveDNS #-}
resolveDNS = lift . resolveDNS
{-# INLINEABLE initSqlDBConnection #-}
initSqlDBConnection = lift . initSqlDBConnection
{-# INLINEABLE deinitSqlDBConnection #-}
Expand Down Expand Up @@ -1077,6 +1096,8 @@ instance (MonadFlow m, Monoid w) => MonadFlow (RWST r w s m) where
generateGUID = lift generateGUID
{-# INLINEABLE runSysCmd #-}
runSysCmd = lift . runSysCmd
{-# INLINEABLE resolveDNS #-}
resolveDNS = lift . resolveDNS
{-# INLINEABLE initSqlDBConnection #-}
initSqlDBConnection = lift . initSqlDBConnection
{-# INLINEABLE deinitSqlDBConnection #-}
Expand Down
8 changes: 8 additions & 0 deletions test/EulerHS/Testing/Flow/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import EulerHS.Testing.Types (FlowMockedValues)
import GHC.TypeLits (KnownSymbol, Symbol)
import Type.Reflection (typeRep)
import Unsafe.Coerce (unsafeCoerce)
import qualified Data.Map as Map

runFlowWithTestInterpreter :: FlowMockedValues -> FlowRuntime -> Flow a -> IO a
runFlowWithTestInterpreter mv flowRt = foldFlow (interpretFlowMethod mv flowRt)
Expand All @@ -26,6 +27,13 @@ interpretFlowMethod mmv _ = \case
L.SetOption _ _ next -> pure . next $ ()
L.GenerateGUID next -> next <$> takeMockedVal @"mockedGenerateGUID" mmv
L.RunSysCmd _ next -> next <$> takeMockedVal @"mockedRunSysCmd" mmv
L.ResolveDNS host next -> do
let dnsDatabase = Map.fromList
[ ("example.com", "93.184.216.34")
, ("google.com", "8.8.8.8")
, ("openai.com", "104.18.25.24")
]
pure $ next (Map.lookup host dnsDatabase)
_ -> error "not yet supported."

takeMockedVal :: forall (f :: Symbol) (a :: Type) (r :: Type)
Expand Down
1 change: 1 addition & 0 deletions test/EulerHS/Testing/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data FlowMockedValues' = FlowMockedValues'
, mockedGetOption :: [ByteString]
, mockedGenerateGUID :: [Text]
, mockedRunSysCmd :: [String]
, mockedDNS :: [Maybe Text]
} deriving (Generic, Typeable)


Expand Down
4 changes: 4 additions & 0 deletions test/EulerHS/Tests/Framework/FlowSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,17 @@ localGUID = unsafeCoerce ("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF" :: String)
lhost :: ByteString
lhost = "localhost"

domainName :: Maybe Text
domainName = Just "93.184.216.34"

scenario1MockedValues :: FlowMockedValues'
scenario1MockedValues = FlowMockedValues'
{ mockedCallServantAPI = [user]
, mockedRunIO = [localGUID]
, mockedGetOption = [lhost]
, mockedGenerateGUID = ["00000000-0000-0000-0000-000000000000"]
, mockedRunSysCmd = ["Neo"]
, mockedDNS = [domainName]
}

ioActWithException :: IO Text
Expand Down