From 091eff9bc2c24bc090473acfceb274ce5b1edf1b Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 17 May 2023 18:16:29 +0100 Subject: [PATCH] Server: Allow giving a different version number to forks --- packages/server/src/app.ts | 4 ++-- packages/server/src/config.ts | 14 +++++++++++++- .../server/src/middleware/apiVersionHandler.ts | 6 +++--- packages/server/src/services/MustacheService.ts | 6 +++--- packages/server/src/utils/types.ts | 1 + packages/server/src/views/partials/footer.mustache | 2 +- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index edcbaad15a..427f945b60 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -4,7 +4,7 @@ require('source-map-support').install(); import * as Koa from 'koa'; import * as fs from 'fs-extra'; import Logger, { LoggerWrapper, TargetType } from '@joplin/lib/Logger'; -import config, { initConfig, runningInDocker } from './config'; +import config, { fullVersionString, initConfig, runningInDocker } from './config'; import { migrateLatest, waitForConnection, sqliteDefaultDir, latestMigration } from './db'; import { AppContext, Env, KoaNext } from './utils/types'; import FsDriverNode from '@joplin/lib/fs-driver-node'; @@ -252,7 +252,7 @@ async function main() { } else { runCommandAndExitApp = false; - appLogger().info(`Starting server v${config().appVersion} (${env}) on port ${config().port} and PID ${process.pid}...`); + appLogger().info(`Starting server ${fullVersionString(config())} (${env}) on port ${config().port} and PID ${process.pid}...`); if (config().maxTimeDrift) { appLogger().info(`Checking for time drift using NTP server: ${config().NTP_SERVER}`); diff --git a/packages/server/src/config.ts b/packages/server/src/config.ts index 240a9bc69e..b641fce71d 100644 --- a/packages/server/src/config.ts +++ b/packages/server/src/config.ts @@ -7,6 +7,9 @@ import parseStorageDriverConnectionString from './models/items/storage/parseStor interface PackageJson { version: string; + joplinServer: { + forkVersion: string; + }; } const packageJson: PackageJson = require(`${__dirname}/packageInfo.js`); @@ -32,6 +35,13 @@ function databaseHostFromEnv(runningInDocker: boolean, env: EnvVariables): strin return null; } +export const fullVersionString = (config: Config) => { + const output: string[] = []; + output.push(`v${config.appVersion}`); + if (config.appVersion !== config.joplinServerVersion) output.push(`(Based on Joplin Server v${config.joplinServerVersion})`); + return output.join(' '); +}; + function databaseConfigFromEnv(runningInDocker: boolean, env: EnvVariables): DatabaseConfig { const baseConfig: DatabaseConfig = { client: DatabaseConfigClient.Null, @@ -114,10 +124,12 @@ export async function initConfig(envType: Env, env: EnvVariables, overrides: any const baseUrl = baseUrlFromEnv(env, appPort); const apiBaseUrl = env.API_BASE_URL ? env.API_BASE_URL : baseUrl; const supportEmail = env.SUPPORT_EMAIL; + const forkVersion = packageJson.joplinServer?.forkVersion; config_ = { ...env, - appVersion: packageJson.version, + appVersion: forkVersion ? forkVersion : packageJson.version, + joplinServerVersion: packageJson.version, appName, isJoplinCloud: apiBaseUrl.includes('.joplincloud.com') || apiBaseUrl.includes('.joplincloud.local'), env: envType, diff --git a/packages/server/src/middleware/apiVersionHandler.ts b/packages/server/src/middleware/apiVersionHandler.ts index 28a7c227f7..1bdd834d12 100644 --- a/packages/server/src/middleware/apiVersionHandler.ts +++ b/packages/server/src/middleware/apiVersionHandler.ts @@ -7,20 +7,20 @@ const compareVersions = require('compare-versions'); export default async function(ctx: AppContext, next: KoaNext): Promise { if (!isApiRequest(ctx)) return next(); - const appVersion = config().appVersion; + const joplinServerVersion = config().joplinServerVersion; const minVersion = ctx.headers['x-api-min-version']; // For now we don't require this header to be set to keep compatibility with // older clients. if (!minVersion) return next(); - const diff = compareVersions(appVersion, minVersion); + const diff = compareVersions(joplinServerVersion, minVersion); // We only throw an error if the client requires a version of Joplin Server // that's ahead of what's installed. This is mostly to automatically notify // those who self-host so that they know they need to upgrade Joplin Server. if (diff < 0) { - throw new ErrorPreconditionFailed(`Joplin Server v${minVersion} is required but v${appVersion} is installed. Please upgrade Joplin Server.`); + throw new ErrorPreconditionFailed(`Joplin Server v${minVersion} is required but v${joplinServerVersion} is installed. Please upgrade Joplin Server.`); } return next(); diff --git a/packages/server/src/services/MustacheService.ts b/packages/server/src/services/MustacheService.ts index 296ef5fd34..2a89515090 100644 --- a/packages/server/src/services/MustacheService.ts +++ b/packages/server/src/services/MustacheService.ts @@ -1,7 +1,7 @@ import * as Mustache from 'mustache'; import * as fs from 'fs-extra'; import { extname } from 'path'; -import config from '../config'; +import config, { fullVersionString } from '../config'; import { filename } from '@joplin/lib/path-utils'; import { NotificationView } from '../utils/types'; import { User } from '../services/database/types'; @@ -40,7 +40,7 @@ interface GlobalParams { notifications?: NotificationView[]; hasNotifications?: boolean; owner?: User; - appVersion?: string; + fullVersionString?: string; appName?: string; termsUrl?: string; privacyUrl?: string; @@ -171,7 +171,7 @@ export default class MustacheService { baseUrl: config().baseUrl, joplinAppBaseUrl: config().joplinAppBaseUrl, prefersDarkEnabled: this.prefersDarkEnabled_, - appVersion: config().appVersion, + fullVersionString: fullVersionString(config()), appName: config().appName, termsUrl: config().termsEnabled ? makeUrl(UrlType.Terms) : '', privacyUrl: config().termsEnabled ? makeUrl(UrlType.Privacy) : '', diff --git a/packages/server/src/utils/types.ts b/packages/server/src/utils/types.ts index c54b9809f1..4cfd519bea 100644 --- a/packages/server/src/utils/types.ts +++ b/packages/server/src/utils/types.ts @@ -133,6 +133,7 @@ export interface StorageDriverConfig { export interface Config extends EnvVariables { appVersion: string; + joplinServerVersion: string; // May be different from appVersion, if this is a fork of JS appName: string; env: Env; port: number; diff --git a/packages/server/src/views/partials/footer.mustache b/packages/server/src/views/partials/footer.mustache index 5d890c3155..28046b30d0 100644 --- a/packages/server/src/views/partials/footer.mustache +++ b/packages/server/src/views/partials/footer.mustache @@ -1,5 +1,5 @@ \ No newline at end of file