Add support for remote configs (#43)

This commit is contained in:
Brendan Kellam 2024-10-17 16:20:09 -07:00 committed by GitHub
parent 1266a46458
commit ae05d8f68e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 11 deletions

View file

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- Added support for specifying urls for the `--configPath` option in the backend.
## [2.0.0] - 2024-10-17 ## [2.0.0] - 2024-10-17
### Added ### Added

View file

@ -10,7 +10,7 @@ import { AppContext, Repository } from "./types.js";
import { cloneRepository, fetchRepository } from "./git.js"; import { cloneRepository, fetchRepository } from "./git.js";
import { createLogger } from "./logger.js"; import { createLogger } from "./logger.js";
import { createRepository, Database, loadDB, updateRepository } from './db.js'; import { createRepository, Database, loadDB, updateRepository } from './db.js';
import { measure } from "./utils.js"; import { isRemotePath, measure } from "./utils.js";
import { REINDEX_INTERVAL_MS, RESYNC_CONFIG_INTERVAL_MS } from "./constants.js"; import { REINDEX_INTERVAL_MS, RESYNC_CONFIG_INTERVAL_MS } from "./constants.js";
import stripJsonComments from 'strip-json-comments'; import stripJsonComments from 'strip-json-comments';
@ -41,10 +41,22 @@ const indexRepository = async (repo: Repository, ctx: AppContext) => {
} }
const syncConfig = async (configPath: string, db: Database, signal: AbortSignal, ctx: AppContext) => { const syncConfig = async (configPath: string, db: Database, signal: AbortSignal, ctx: AppContext) => {
const configContent = await readFile(configPath, { const configContent = await (async () => {
encoding: 'utf-8', if (isRemotePath(configPath)) {
signal, const response = await fetch(configPath, {
}); signal,
});
if (!response.ok) {
throw new Error(`Failed to fetch config file ${configPath}: ${response.statusText}`);
}
return response.text();
} else {
return readFile(configPath, {
encoding: 'utf-8',
signal,
});
}
})();
// @todo: we should validate the configuration file's structure here. // @todo: we should validate the configuration file's structure here.
const config = JSON.parse(stripJsonComments(configContent)) as SourcebotConfigurationSchema; const config = JSON.parse(stripJsonComments(configContent)) as SourcebotConfigurationSchema;
@ -122,7 +134,7 @@ const syncConfig = async (configPath: string, db: Database, signal: AbortSignal,
}); });
const args = parser.parse_args() as Arguments; const args = parser.parse_args() as Arguments;
if (!existsSync(args.configPath)) { if (!isRemotePath(args.configPath) && !existsSync(args.configPath)) {
console.error(`Config file ${args.configPath} does not exist`); console.error(`Config file ${args.configPath} does not exist`);
process.exit(1); process.exit(1);
} }
@ -173,11 +185,13 @@ const syncConfig = async (configPath: string, db: Database, signal: AbortSignal,
}); });
} }
// Re-sync on file changes // Re-sync on file changes if the config file is local
watch(args.configPath, () => { if (!isRemotePath(args.configPath)) {
logger.info(`Config file ${args.configPath} changed. Re-syncing...`); watch(args.configPath, () => {
_syncConfig(); logger.info(`Config file ${args.configPath} changed. Re-syncing...`);
}); _syncConfig();
});
}
// Re-sync every 24 hours // Re-sync every 24 hours
setInterval(() => { setInterval(() => {

View file

@ -56,3 +56,7 @@ export const getTokenFromConfig = (token: string | { env: string }, ctx: AppCont
} }
return tokenValue; return tokenValue;
} }
export const isRemotePath = (path: string) => {
return path.startsWith('https://') || path.startsWith('http://');
}