From c20648c33a4396d5b19096cacb84d3d914cee88b Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 29 Apr 2022 10:48:58 -0400 Subject: [PATCH] Improve bootstrap process --- Dockerfile | 1 - account-db.js | 12 +++++++++++- app-sync.js | 8 ++++---- load-config.js | 5 ++++- sql/default-account.sqlite | Bin 28672 -> 0 bytes 5 files changed, 19 insertions(+), 7 deletions(-) delete mode 100644 sql/default-account.sqlite diff --git a/Dockerfile b/Dockerfile index 844f6af..0a5db9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,5 @@ ADD . . RUN yarn install --production RUN mkdir ./server-files RUN mkdir ./user-files -RUN cp ./sql/default-account.sqlite ./server-files/account.sqlite CMD ["yarn", "start"] \ No newline at end of file diff --git a/account-db.js b/account-db.js index f839df7..8a90459 100644 --- a/account-db.js +++ b/account-db.js @@ -1,10 +1,20 @@ +let fs = require('fs'); let { join } = require('path'); let { openDatabase } = require('./db'); +let config = require('./load-config'); let accountDb = null; function getAccountDb() { if (accountDb == null) { - accountDb = openDatabase(join(__dirname, 'server-files/account.sqlite')); + let dbPath = join(config.serverFiles, 'account.sqlite'); + let needsInit = !fs.existsSync(dbPath); + + accountDb = openDatabase(dbPath); + + if (needsInit) { + let initSql = fs.readFileSync(join(__dirname, 'sql/account.sql'), 'utf8'); + accountDb.exec(initSql); + } } return accountDb; diff --git a/app-sync.js b/app-sync.js index ca56cdb..9b7dbfb 100644 --- a/app-sync.js +++ b/app-sync.js @@ -19,7 +19,7 @@ const app = express(); app.use(errorMiddleware); async function init() { - let fileDir = join(__dirname, process.env.ACTUAL_USER_FILES || config.files); + let fileDir = join(__dirname, process.env.ACTUAL_USER_FILES || config.userFiles); console.log('Initializing Actual with user file dir:', fileDir); @@ -279,12 +279,12 @@ app.post('/upload-user-file', async (req, res) => { // supported yet in the self-hosted version because it's unclear if // it's still needed, given that you own your server // - // await fs.writeFile(join(config.files, `${fileId}.blob`), req.body); + // await fs.writeFile(join(config.userFiles, `${fileId}.blob`), req.body); let zip = new AdmZip(req.body); try { - zip.extractAllTo(join(config.files, fileId), true); + zip.extractAllTo(join(config.userFiles, fileId), true); } catch (err) { console.log('Error writing file', err); res.send(JSON.stringify({ status: 'error' })); @@ -339,7 +339,7 @@ app.get('/download-user-file', async (req, res) => { let zip = new AdmZip(); try { - zip.addLocalFolder(join(config.files, fileId), '/'); + zip.addLocalFolder(join(config.userFiles, fileId), '/'); } catch (e) { res.status(500).send('Error reading files'); return; diff --git a/load-config.js b/load-config.js index 4af565d..9dd97e0 100644 --- a/load-config.js +++ b/load-config.js @@ -3,11 +3,14 @@ try { config = require('./config'); } catch (e) { let fs = require('fs'); + let { join } = require('path'); + let root = fs.existsSync('/data') ? '/data' : '/'; config = { mode: 'development', port: 5006, - files: fs.existsSync('/data') ? '/data' : './user-files' + serverFiles: join(root, 'server-files'), + userFiles: join(root, 'user-files') }; } diff --git a/sql/default-account.sqlite b/sql/default-account.sqlite deleted file mode 100644 index 3878e6831c5c80d9af8539f4b3fa7fcc3de6b916..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 28672 zcmeI%&u-H&9Ki8p?NT&Ux=WQ)CEuVD0#zIl5?RTlY1syCkmxCd)q|>O(#6?Wj}tdu zj#uEeSKvr&5vf(x6SwFa$$#=oV#go*VtM=_m=#i7W%(j5#4GF2vTf_F5SC>%%G4@T zb?N(NbyJr1T76fwVZ9iCZg>7zPW!>?{OtVdJnl@|4^MvY`hYkB2q1s}0tg_000Iag zfWUtV+~4lET-UXKoEGtyL|)AjsZ8b!N4^*OBJ_>|UzpH=5YJ{)5&EA(@qTo2=8Y!e z)So;rLUHT`W8ZX=#tUg$>GvJSedF5JES<_b zb)A%(-6*~(GIJl9@kRqvw1S4?_Imch2q~%bh*VWJH=L@drYf?zOt-Z9FPn}#?Ct8f zY9Ja^wf&*(xZSQjH{F-N_)Vw1jc#>Bce#v}`kv+6bJTyj=eV!CyL#2%M+2Sfz5FA+ zqS`t;)EO572q1s}0tg_000IagfB*sr)K%a>4~+ePU8k3EA%Fk^2q1s}0tg_000Iag zU<5e-XCWYf00IagfB*srAb