Skip to content

Add support for remote configs #43

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

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added support for specifying urls for the `--configPath` option in the backend.

## [2.0.0] - 2024-10-17

### Added
Expand Down
36 changes: 25 additions & 11 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AppContext, Repository } from "./types.js";
import { cloneRepository, fetchRepository } from "./git.js";
import { createLogger } from "./logger.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 stripJsonComments from 'strip-json-comments';

Expand Down Expand Up @@ -41,10 +41,22 @@ const indexRepository = async (repo: Repository, ctx: AppContext) => {
}

const syncConfig = async (configPath: string, db: Database, signal: AbortSignal, ctx: AppContext) => {
const configContent = await readFile(configPath, {
encoding: 'utf-8',
signal,
});
const configContent = await (async () => {
if (isRemotePath(configPath)) {
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.
const config = JSON.parse(stripJsonComments(configContent)) as SourcebotConfigurationSchema;
Expand Down Expand Up @@ -122,7 +134,7 @@ const syncConfig = async (configPath: string, db: Database, signal: AbortSignal,
});
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`);
process.exit(1);
}
Expand Down Expand Up @@ -173,11 +185,13 @@ const syncConfig = async (configPath: string, db: Database, signal: AbortSignal,
});
}

// Re-sync on file changes
watch(args.configPath, () => {
logger.info(`Config file ${args.configPath} changed. Re-syncing...`);
_syncConfig();
});
// Re-sync on file changes if the config file is local
if (!isRemotePath(args.configPath)) {
watch(args.configPath, () => {
logger.info(`Config file ${args.configPath} changed. Re-syncing...`);
_syncConfig();
});
}

// Re-sync every 24 hours
setInterval(() => {
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ export const getTokenFromConfig = (token: string | { env: string }, ctx: AppCont
throw new Error(`The environment variable '${token.env}' was referenced in ${ctx.configPath}, but was not set.`);
}
return tokenValue;
}

export const isRemotePath = (path: string) => {
return path.startsWith('https://') || path.startsWith('http://');
}