Skip to content

Commit 1ef5d5a

Browse files
authored
Merge branch 'main' into feat-accept-external-stats
2 parents 2ab210f + 349757e commit 1ef5d5a

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const envSchema = z.object({
1414
GITHUB_TOKEN: z.string().optional(),
1515
LOG_PATH: z.string().optional(),
1616
POSTGRES_URL: z.string().optional(),
17+
SOURCE_DATABASE_URL: z.string().optional(),
1718
DEBUG: z.stringbool().default(false),
1819
STATISTICS_PATH: z.string().optional(),
1920

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ async function runOutsideCI() {
5959
env.HOST,
6060
env.PORT,
6161
Connectable.fromString(env.POSTGRES_URL),
62+
env.SOURCE_DATABASE_URL ? Connectable.fromString(env.SOURCE_DATABASE_URL) : undefined,
6263
);
6364

6465
const shutdown = async () => {

src/server/http.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export async function createServer(
117117
hostname: string,
118118
port: number,
119119
targetDb?: Connectable,
120+
sourceDb?: Connectable,
120121
): Promise<FastifyInstance> {
121122
const fastify = Fastify({ logger: false });
122123

@@ -171,13 +172,15 @@ export async function createServer(
171172
});
172173

173174
if (remoteController) {
174-
fastify.post("/postgres", async (request, reply) => {
175-
log.info(`[POST] /postgres`, "http");
176-
const result = await remoteController.onFullSync(
177-
JSON.stringify(request.body),
178-
);
179-
return reply.status(result.status).send(result.body);
180-
});
175+
if (!sourceDb) {
176+
fastify.post("/postgres", async (request, reply) => {
177+
log.info(`[POST] /postgres`, "http");
178+
const result = await remoteController.onFullSync(
179+
JSON.stringify(request.body),
180+
);
181+
return reply.status(result.status).send(result.body);
182+
});
183+
}
181184

182185
fastify.get("/postgres", async (request, reply) => {
183186
log.info(`[GET] /postgres`, "http");
@@ -223,6 +226,20 @@ export async function createServer(
223226
}
224227

225228
await fastify.listen({ host: hostname, port });
229+
230+
if (remoteController && sourceDb) {
231+
log.info(`SOURCE_DATABASE_URL set, triggering initial sync`, "http");
232+
remoteController.onFullSync(JSON.stringify({ db: sourceDb.toString() })).then((result) => {
233+
if (result.status >= 400) {
234+
log.error(`Initial sync failed: ${JSON.stringify(result.body)}`, "http");
235+
process.exit(1);
236+
}
237+
}).catch((error) => {
238+
log.error(`Initial sync failed: ${error}`, "http");
239+
process.exit(1);
240+
});
241+
}
242+
226243
return fastify;
227244
}
228245

0 commit comments

Comments
 (0)