2017-12-03 22:29:23 -05:00
"use strict" ;
2024-04-06 23:08:41 +03:00
import sqlInit = require ( '../../services/sql_init' ) ;
import setupService = require ( '../../services/setup' ) ;
import log = require ( '../../services/log' ) ;
import appInfo = require ( '../../services/app_info' ) ;
import { Request } from 'express' ;
2024-05-12 11:29:44 +03:00
import { InitDbOptions } from '../../types' ;
2018-09-10 20:05:10 +02:00
2024-05-12 11:29:44 +03:00
function buildRoutes ( initOptions : InitDbOptions ) {
function getStatus() {
return {
isInitialized : sqlInit.isDbInitialized ( ) ,
schemaExists : sqlInit.schemaExists ( ) ,
syncVersion : appInfo.syncVersion
} ;
2019-06-11 20:42:06 +02:00
}
2024-05-12 11:29:44 +03:00
async function setupNewDocument() {
await sqlInit . createInitialDatabase ( initOptions ) ;
}
function setupSyncFromServer ( req : Request ) {
const { syncServerHost , syncProxy , password } = req . body ;
return setupService . setupSyncFromSyncServer ( syncServerHost , syncProxy , password , initOptions ) ;
}
function saveSyncSeed ( req : Request ) {
const { options , syncVersion } = req . body ;
if ( appInfo . syncVersion !== syncVersion ) {
const message = ` Could not setup sync since local sync protocol version is ${ appInfo . syncVersion } while remote is ${ syncVersion } . To fix this issue, use same Trilium version on all instances. ` ;
log . error ( message ) ;
return [ 400 , {
error : message
} ]
}
log . info ( "Saved sync seed." ) ;
sqlInit . createDatabaseForSync ( options , initOptions ) ;
}
function getSyncSeed() {
log . info ( "Serving sync seed." ) ;
return {
options : setupService.getSyncSeedOptions ( ) ,
syncVersion : appInfo.syncVersion
} ;
}
2019-06-11 20:42:06 +02:00
return {
2024-05-12 11:29:44 +03:00
getStatus ,
setupNewDocument ,
setupSyncFromServer ,
getSyncSeed ,
saveSyncSeed
2019-06-11 20:42:06 +02:00
} ;
2018-07-25 09:46:57 +02:00
}
2024-05-12 11:29:44 +03:00
export = buildRoutes ;