Skip to content

Commit 0c43b0c

Browse files
committed
Add servant server
1 parent 7c9b872 commit 0c43b0c

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

app/App.hs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
--------------------------------------------------------------------------------
44

55
import Cardano.Api qualified as C
6+
import Control.Concurrent.Async
67
import Data.Function ((&))
78
import Data.Map qualified as Map
89
import Data.Set qualified as Set
910
import Data.Yaml (decodeFileThrow)
1011
import Options
1112
import Options.Applicative
13+
import qualified PSR.Web as Web
1214
import PSR.Chain
1315
import PSR.ConfigMap qualified as CM
1416
import PSR.ContextBuilder
@@ -36,18 +38,21 @@ main = do
3638
-- TODO: Use a logging interface instead of using putStrLn.
3739
putStrLn "Started..."
3840

39-
let confPolicyMap = Map.fromList [(CM.script_hash x, x) | x <- scripts]
40-
conn = mkLocalNodeConnectInfo networkId socketPath
41-
streamChainSyncEvents conn points
42-
& Stream.filter (not . isByron)
43-
& fmap getEventTransactions
44-
& Stream.postscanl trackPreviousChainPoint
45-
-- TODO: Try to replace "concatMap" with "unfoldEach".
46-
& Stream.concatMap (Stream.fromList . (\(a, b) -> (a,) <$> b))
47-
& Stream.mapM (mkContext1 conn . uncurry mkContext0)
48-
& Stream.filter
49-
( \ctx1@Context1{..} ->
50-
not . Map.null . Map.restrictKeys confPolicyMap $
51-
Set.union (getMintPolicies context0) (getSpendPolicies ctx1)
52-
)
53-
& Stream.fold (Fold.drainMapM print)
41+
withAsync (Web.run webServerPort) $ \serverAsync -> do
42+
link serverAsync
43+
44+
let confPolicyMap = Map.fromList [(CM.script_hash x, x) | x <- scripts]
45+
conn = mkLocalNodeConnectInfo networkId socketPath
46+
streamChainSyncEvents conn points
47+
& Stream.filter (not . isByron)
48+
& fmap getEventTransactions
49+
& Stream.postscanl trackPreviousChainPoint
50+
-- TODO: Try to replace "concatMap" with "unfoldEach".
51+
& Stream.concatMap (Stream.fromList . (\(a, b) -> (a,) <$> b))
52+
& Stream.mapM (mkContext1 conn . uncurry mkContext0)
53+
& Stream.filter
54+
( \ctx1@Context1{..} ->
55+
not . Map.null . Map.restrictKeys confPolicyMap $
56+
Set.union (getMintPolicies context0) (getSpendPolicies ctx1)
57+
)
58+
& Stream.fold (Fold.drainMapM print)

app/Options.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Options where
22

33
import Cardano.Api
44
import Options.Applicative
5+
import Network.Wai.Handler.Warp (Port)
56

67
--------------------------------------------------------------------------------
78
-- Options
@@ -17,6 +18,7 @@ data Options = Options
1718
{ socketPath :: SocketPath
1819
, networkId :: NetworkId
1920
, scriptYaml :: FilePath
21+
, webServerPort :: Port
2022
}
2123
deriving (Show, Eq)
2224

@@ -26,6 +28,7 @@ parseOptions =
2628
<$> optSocketPath
2729
<*> optNetworkId
2830
<*> optScriptYaml
31+
<*> optWebServerPort
2932
where
3033
optSocketPath =
3134
File
@@ -51,6 +54,13 @@ parseOptions =
5154
<> metavar "PATH"
5255
<> help "Path to script.yaml"
5356
)
57+
optWebServerPort =
58+
option auto
59+
( long "web-server-port"
60+
<> metavar "WEB_SERVER_PORT"
61+
<> help "Port of the web server API"
62+
<> value 8080
63+
)
5464

5565
psrOpts :: ParserInfo Options
5666
psrOpts =

lib/PSR/Web.hs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module PSR.Web (
2+
run
3+
) where
4+
5+
import Data.Text (Text)
6+
import Data.Aeson (FromJSON, ToJSON)
7+
import GHC.Generics
8+
import qualified Network.Wai.Handler.Warp as Warp
9+
import Servant
10+
import Servant.Server.Generic ()
11+
12+
data Event = Event {
13+
content :: Text
14+
} deriving (Generic, Show)
15+
16+
instance FromJSON Event
17+
instance ToJSON Event
18+
19+
type ServerAPI = "events" :> Get '[JSON] [Event]
20+
21+
serverApi :: Proxy ServerAPI
22+
serverApi = Proxy
23+
24+
server :: Server ServerAPI
25+
server = eventsH
26+
where
27+
eventsH = do
28+
pure []
29+
30+
run :: Warp.Port -> IO ()
31+
run port = Warp.run port (serve serverApi server)

plutus-script-reexecutor.cabal

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ library
5151
PSR.ConfigMap
5252
PSR.ContextBuilder
5353
PSR.Streaming
54+
PSR.Web
5455

5556
build-depends:
5657
, base >=4.9 && <5
58+
, aeson
5759
, cardano-api
5860
, cardano-ledger-alonzo
5961
, cardano-ledger-core
@@ -62,9 +64,14 @@ library
6264
, ouroboros-consensus
6365
, ouroboros-consensus-cardano
6466
, ouroboros-network-protocols
67+
, servant
68+
, servant-server
6569
, streamly
6670
, streamly-core
71+
, text
6772
, yaml
73+
, wai
74+
, warp
6875

6976
executable exec
7077
import: lang
@@ -73,13 +80,15 @@ executable exec
7380
hs-source-dirs: app
7481
build-depends:
7582
, base >=4.9 && <5
83+
, async
7684
, cardano-api
7785
, containers
7886
, optparse-applicative
7987
, plutus-script-reexecutor
8088
, streamly
8189
, streamly-core
8290
, yaml
91+
, warp
8392

8493
benchmark bench
8594
import: lang

0 commit comments

Comments
 (0)