Unverified Commit 0a61c670 authored by Pavel Ševčík's avatar Pavel Ševčík
Browse files

Add Electrs URL to config

parent 765a7db5
......@@ -19,7 +19,11 @@ type ExplorerConfig = O.Option<{
password: string;
}>;
interface Config {
type ElectrsConfig = O.Option<{
url: string;
}>;
interface DojoConfig {
ronin: {
version: string;
};
......@@ -31,16 +35,15 @@ interface Config {
};
}
type ConfigWithExplorer = Config & {
type Config = DojoConfig & {
explorer: ExplorerConfig;
electrs: ElectrsConfig;
};
/**
* We have to keep this config in memory because loading it takes too much time
*/
let configWithExplorerStore: E.Either<Error, ConfigWithExplorer> = E.left(
createError(503, "Config was not yet loaded")
);
let configWithExplorerStore: E.Either<Error, Config> = E.left(createError(503, "Config was not yet loaded"));
const getDojoUrl: TE.TaskEither<BoomError, string> = pipe(
execAndGetResult("tor", { Cmd: ["cat", "/var/lib/tor/hsv3dojo/hostname"] }),
......@@ -52,6 +55,11 @@ const getExplorerUrl: TE.TaskEither<BoomError, string> = pipe(
TE.map((explorerUrl) => explorerUrl.replace(/\n/, ""))
);
const getElectrsUrl: TE.TaskEither<BoomError, string> = pipe(
execAndGetResult("tor", { Cmd: ["cat", "/var/lib/tor/hsv3electrs/hostname"] }),
TE.map((electrsUrl) => electrsUrl.replace(/\n/, ""))
);
const getExplorerConfig: T.Task<ExplorerConfig> = pipe(
sequenceS(TE.taskEither)({
url: getExplorerUrl,
......@@ -63,7 +71,17 @@ const getExplorerConfig: T.Task<ExplorerConfig> = pipe(
)
);
const readConfig: TE.TaskEither<Error, Config> = sequenceS(TE.taskEither)({
const getElectrsConfig: T.Task<ElectrsConfig> = pipe(
sequenceS(TE.taskEither)({
url: getElectrsUrl,
}),
TE.fold(
() => T.of(O.none),
(config) => T.of(O.some(config))
)
);
const readConfig: TE.TaskEither<Error, DojoConfig> = sequenceS(TE.taskEither)({
ronin: sequenceS(TE.taskEither)({
version: TE.right(version),
}),
......@@ -75,7 +93,7 @@ const readConfig: TE.TaskEither<Error, Config> = sequenceS(TE.taskEither)({
}),
});
export const Config = (req: IncomingMessage, res: ServerResponse): Promise<ConfigWithExplorer> => {
export const Config = (req: IncomingMessage, res: ServerResponse): Promise<Config> => {
const handleRequest = pipe(
configWithExplorerStore,
E.fold(
......@@ -90,7 +108,7 @@ export const Config = (req: IncomingMessage, res: ServerResponse): Promise<Confi
};
// Function to load config for dojo and explorer
const loadConfig: TE.TaskEither<Error, ConfigWithExplorer> = pipe(
const loadConfig: TE.TaskEither<Error, Config> = pipe(
readConfig,
TE.chain((config) =>
pipe(
......@@ -99,6 +117,13 @@ const loadConfig: TE.TaskEither<Error, ConfigWithExplorer> = pipe(
TE.chain((explorerConfig) => TE.right({ ...config, explorer: explorerConfig }))
)
),
TE.chain((config) =>
pipe(
getElectrsConfig,
TE.fromTask,
TE.chain((electrsConfig) => TE.right({ ...config, electrs: electrsConfig }))
)
),
TE.mapLeft((err) => {
console.error(err);
return createError(503, "Unable to load Dojo configuration.");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment