@@ -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