mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
#12: Fix first setup in Electron
This commit is contained in:
parent
168977a52b
commit
780f2577c8
@ -77,5 +77,10 @@ process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
|
|||||||
*/
|
*/
|
||||||
electron.app.requestSingleInstanceLock();
|
electron.app.requestSingleInstanceLock();
|
||||||
|
|
||||||
const app = startTrilium();
|
const app = startTrilium({
|
||||||
|
setupCompleteCallback: () => {
|
||||||
|
windowService.createMainWindow(electron.app);
|
||||||
|
windowService.closeSetupWindow();
|
||||||
|
}
|
||||||
|
});
|
||||||
electronRouting(app);
|
electronRouting(app);
|
@ -6,8 +6,9 @@ import helmet = require('helmet');
|
|||||||
import compression = require('compression');
|
import compression = require('compression');
|
||||||
import sessionParser = require('./routes/session_parser');
|
import sessionParser = require('./routes/session_parser');
|
||||||
import utils = require('./services/utils');
|
import utils = require('./services/utils');
|
||||||
|
import { RouteConfig } from './routes/types';
|
||||||
|
|
||||||
function buildApp() {
|
function buildApp(routeConfig: RouteConfig) {
|
||||||
require('./services/handlers');
|
require('./services/handlers');
|
||||||
require('./becca/becca_loader');
|
require('./becca/becca_loader');
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ function buildApp() {
|
|||||||
app.use(favicon(`${__dirname}/../../common/images/app-icons/win/icon.ico`));
|
app.use(favicon(`${__dirname}/../../common/images/app-icons/win/icon.ico`));
|
||||||
|
|
||||||
require('./routes/assets').register(app);
|
require('./routes/assets').register(app);
|
||||||
require('./routes/routes').register(app);
|
require('./routes/routes').register(app, routeConfig);
|
||||||
require('./routes/custom').register(app);
|
require('./routes/custom').register(app);
|
||||||
require('./routes/error_handlers').register(app);
|
require('./routes/error_handlers').register(app);
|
||||||
|
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
import startTrilium = require("./www");
|
import startTrilium = require("./www");
|
||||||
|
|
||||||
startTrilium();
|
startTrilium({
|
||||||
|
setupCompleteCallback: (res) => {
|
||||||
|
res.redirect('.');
|
||||||
|
}
|
||||||
|
});
|
@ -71,6 +71,7 @@ import etapiSpecialNoteRoutes = require('../etapi/special_notes');
|
|||||||
import etapiSpecRoute = require('../etapi/spec');
|
import etapiSpecRoute = require('../etapi/spec');
|
||||||
import etapiBackupRoute = require('../etapi/backup');
|
import etapiBackupRoute = require('../etapi/backup');
|
||||||
import { AppRequest, AppRequestHandler } from './route-interface';
|
import { AppRequest, AppRequestHandler } from './route-interface';
|
||||||
|
import { RouteConfig } from './types';
|
||||||
|
|
||||||
const csrfMiddleware = csurf({
|
const csrfMiddleware = csurf({
|
||||||
cookie: {
|
cookie: {
|
||||||
@ -101,7 +102,7 @@ const uploadMiddlewareWithErrorHandling = function (req: express.Request, res: e
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function register(app: express.Application) {
|
function register(app: express.Application, config: RouteConfig) {
|
||||||
route(GET, '/', [auth.checkAuth, csrfMiddleware], indexRoute.index);
|
route(GET, '/', [auth.checkAuth, csrfMiddleware], indexRoute.index);
|
||||||
route(GET, '/login', [auth.checkAppInitialized, auth.checkPasswordSet], loginRoute.loginPage);
|
route(GET, '/login', [auth.checkAppInitialized, auth.checkPasswordSet], loginRoute.loginPage);
|
||||||
route(GET, '/set-password', [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPasswordPage);
|
route(GET, '/set-password', [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPasswordPage);
|
||||||
@ -115,7 +116,7 @@ function register(app: express.Application) {
|
|||||||
route(PST, '/login', [loginRateLimiter], loginRoute.login);
|
route(PST, '/login', [loginRateLimiter], loginRoute.login);
|
||||||
route(PST, '/logout', [csrfMiddleware, auth.checkAuth], loginRoute.logout);
|
route(PST, '/logout', [csrfMiddleware, auth.checkAuth], loginRoute.logout);
|
||||||
route(PST, '/set-password', [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPassword);
|
route(PST, '/set-password', [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPassword);
|
||||||
route(GET, '/setup', [], setupRoute.setupPage);
|
route(GET, '/setup', [], setupRoute.buildSetupRoute(config.setupCompleteCallback));
|
||||||
|
|
||||||
apiRoute(GET, '/api/tree', treeApiRoute.getTree);
|
apiRoute(GET, '/api/tree', treeApiRoute.getTree);
|
||||||
apiRoute(PST, '/api/tree/load', treeApiRoute.load);
|
apiRoute(PST, '/api/tree/load', treeApiRoute.load);
|
||||||
|
@ -2,42 +2,35 @@
|
|||||||
|
|
||||||
import sqlInit = require('../services/sql_init');
|
import sqlInit = require('../services/sql_init');
|
||||||
import setupService = require('../services/setup');
|
import setupService = require('../services/setup');
|
||||||
import utils = require('../services/utils');
|
|
||||||
import assetPath = require('../services/asset_path');
|
import assetPath = require('../services/asset_path');
|
||||||
import appPath = require('../services/app_path');
|
import appPath = require('../services/app_path');
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
|
import { SetupCompleteCallback } from './types';
|
||||||
|
|
||||||
function setupPage(req: Request, res: Response) {
|
function buildSetupRoute(setupCompleteCallback: SetupCompleteCallback) {
|
||||||
if (sqlInit.isDbInitialized()) {
|
return (req: Request, res: Response) => {
|
||||||
if (utils.isElectron()) {
|
if (sqlInit.isDbInitialized()) {
|
||||||
const windowService = require('../services/window');
|
setupCompleteCallback(res);
|
||||||
const { app } = require('electron');
|
return;
|
||||||
windowService.createMainWindow(app);
|
|
||||||
windowService.closeSetupWindow();
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
res.redirect('.');
|
// we got here because DB is not completely initialized, so if schema exists,
|
||||||
|
// it means we're in "sync in progress" state.
|
||||||
|
const syncInProgress = sqlInit.schemaExists();
|
||||||
|
|
||||||
|
if (syncInProgress) {
|
||||||
|
// trigger sync if it's not already running
|
||||||
|
setupService.triggerSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
res.render('setup', {
|
||||||
|
syncInProgress: syncInProgress,
|
||||||
|
assetPath: assetPath,
|
||||||
|
appPath: appPath
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// we got here because DB is not completely initialized, so if schema exists,
|
|
||||||
// it means we're in "sync in progress" state.
|
|
||||||
const syncInProgress = sqlInit.schemaExists();
|
|
||||||
|
|
||||||
if (syncInProgress) {
|
|
||||||
// trigger sync if it's not already running
|
|
||||||
setupService.triggerSync();
|
|
||||||
}
|
|
||||||
|
|
||||||
res.render('setup', {
|
|
||||||
syncInProgress: syncInProgress,
|
|
||||||
assetPath: assetPath,
|
|
||||||
appPath: appPath
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export = {
|
export = {
|
||||||
setupPage
|
buildSetupRoute
|
||||||
};
|
};
|
||||||
|
8
server/src/routes/types.ts
Normal file
8
server/src/routes/types.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { Response } from "express";
|
||||||
|
|
||||||
|
export type SetupCompleteCallback = (res: Response) => void;
|
||||||
|
|
||||||
|
export interface RouteConfig {
|
||||||
|
/** Callback to be invoked when first setup is complete. */
|
||||||
|
setupCompleteCallback: SetupCompleteCallback;
|
||||||
|
}
|
@ -31,8 +31,9 @@ import port = require('./services/port');
|
|||||||
import host = require('./services/host');
|
import host = require('./services/host');
|
||||||
import semver = require('semver');
|
import semver = require('semver');
|
||||||
import type { Express } from "express";
|
import type { Express } from "express";
|
||||||
|
import { RouteConfig } from './routes/types';
|
||||||
|
|
||||||
function startTrilium() {
|
function startTrilium(routeConfig: RouteConfig) {
|
||||||
if (!semver.satisfies(process.version, ">=10.5.0")) {
|
if (!semver.satisfies(process.version, ">=10.5.0")) {
|
||||||
console.error("Trilium only supports node.js 10.5 and later");
|
console.error("Trilium only supports node.js 10.5 and later");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@ -40,7 +41,7 @@ function startTrilium() {
|
|||||||
|
|
||||||
log.info(JSON.stringify(appInfo, null, 2));
|
log.info(JSON.stringify(appInfo, null, 2));
|
||||||
|
|
||||||
const app = buildApp();
|
const app = buildApp(routeConfig);
|
||||||
|
|
||||||
const cpuInfos = require('os').cpus();
|
const cpuInfos = require('os').cpus();
|
||||||
if (cpuInfos && cpuInfos[0] !== undefined) { // https://github.com/zadam/trilium/pull/3957
|
if (cpuInfos && cpuInfos[0] !== undefined) { // https://github.com/zadam/trilium/pull/3957
|
||||||
|
Loading…
x
Reference in New Issue
Block a user