|
1 | 1 | {-# LANGUAGE AllowAmbiguousTypes #-} |
2 | 2 |
|
3 | | -module BotPlutusInterface.Server (app, initState) where |
| 3 | +module BotPlutusInterface.Server ( |
| 4 | + app, |
| 5 | + initState, |
| 6 | + WebSocketEndpoint, |
| 7 | + ActivateContractEndpoint, |
| 8 | + RawTxEndpoint, |
| 9 | +) where |
4 | 10 |
|
5 | 11 | import BotPlutusInterface.Contract (runContract) |
6 | 12 | import BotPlutusInterface.Types ( |
@@ -61,18 +67,25 @@ initState :: IO AppState |
61 | 67 | initState = AppState <$> newTVarIO Map.empty |
62 | 68 |
|
63 | 69 | -- | Mock API Schema, stripped endpoints that we don't use in this project |
64 | | -type API a = |
65 | | - ("ws" :> WebSocketPending) -- Combined websocket (subscription protocol) |
66 | | - :<|> ( "api" |
67 | | - :> "contract" |
68 | | - :> "activate" |
69 | | - :> ReqBody '[JSON] (ContractActivationArgs a) |
70 | | - :> Post '[JSON] ContractInstanceId -- Start a new instance. |
71 | | - ) |
72 | | - :<|> ( "rawTx" |
73 | | - :> Capture "hash" Text |
74 | | - :> Get '[JSON] RawTx |
75 | | - ) |
| 70 | +type API a = WebSocketEndpoint :<|> ActivateContractEndpoint a :<|> RawTxEndpoint |
| 71 | + |
| 72 | +-- Endpoints are split up so it is easier to test them. In particular servant-client |
| 73 | +-- can not generate a client for the WebSocketEndpoint; this allows us to still |
| 74 | +-- use servant-client to test the other endpoints |
| 75 | + |
| 76 | +type WebSocketEndpoint = "ws" :> WebSocketPending -- Combined websocket (subscription protocol) |
| 77 | + |
| 78 | +type ActivateContractEndpoint a = |
| 79 | + "api" |
| 80 | + :> "contract" |
| 81 | + :> "activate" |
| 82 | + :> ReqBody '[JSON] (ContractActivationArgs a) |
| 83 | + :> Post '[JSON] ContractInstanceId -- Start a new instance. |
| 84 | + |
| 85 | +type RawTxEndpoint = |
| 86 | + "rawTx" |
| 87 | + :> Capture "hash" Text |
| 88 | + :> Get '[JSON] RawTx |
76 | 89 |
|
77 | 90 | server :: HasDefinitions t => PABConfig -> AppState -> Server (API t) |
78 | 91 | server pabConfig state = |
@@ -223,9 +236,9 @@ handleContract pabConf state@(AppState st) contract = liftIO $ do |
223 | 236 | rawTxHandler :: PABConfig -> Text -> Handler RawTx |
224 | 237 | rawTxHandler config hash = do |
225 | 238 | -- Check that endpoint is enabled |
226 | | - assert (pcEnableTxEndpoint config) |
| 239 | + assert config.pcEnableTxEndpoint |
227 | 240 | -- Absolute path to pcTxFileDir that is specified in the config |
228 | | - txFolderPath <- liftIO $ makeAbsolute (unpack $ pcTxFileDir config) |
| 241 | + txFolderPath <- liftIO $ makeAbsolute (unpack config.pcTxFileDir) |
229 | 242 |
|
230 | 243 | -- Add/Set .raw extension on path |
231 | 244 | let suppliedPath :: FilePath |
|
0 commit comments