mirror of
https://github.com/bpatrik/pigallery2.git
synced 2025-01-02 03:37:54 +02:00
Add basic configuring options #753
This commit is contained in:
parent
4b215c1e57
commit
75d277040d
@ -9,6 +9,7 @@ import {SharingDTO} from '../../common/entities/SharingDTO';
|
||||
import {Utils} from '../../common/Utils';
|
||||
import {LoggerRouter} from '../routes/LoggerRouter';
|
||||
import {TAGS} from '../../common/config/public/ClientConfig';
|
||||
import {ExtensionConfigWrapper} from '../model/extension/ExtensionConfigWrapper';
|
||||
|
||||
const forcedDebug = process.env['NODE_ENV'] === 'debug';
|
||||
|
||||
@ -107,7 +108,7 @@ export class RenderingMWs {
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const originalConf = await Config.original();
|
||||
const originalConf = await ExtensionConfigWrapper.original();
|
||||
// These are sensitive information, do not send to the client side
|
||||
originalConf.Server.sessionSecret = null;
|
||||
const message = new Message<PrivateConfigClass>(
|
||||
|
@ -6,6 +6,7 @@ import {ConfigDiagnostics} from '../../model/diagnostics/ConfigDiagnostics';
|
||||
import {ConfigClassBuilder} from '../../../../node_modules/typeconfig/node';
|
||||
import {TAGS} from '../../../common/config/public/ClientConfig';
|
||||
import {ObjectManagers} from '../../model/ObjectManagers';
|
||||
import {ExtensionConfigWrapper} from '../../model/extension/ExtensionConfigWrapper';
|
||||
|
||||
const LOG_TAG = '[SettingsMWs]';
|
||||
|
||||
@ -28,7 +29,7 @@ export class SettingsMWs {
|
||||
try {
|
||||
let settings = req.body.settings; // Top level settings JSON
|
||||
const settingsPath: string = req.body.settingsPath; // Name of the top level settings
|
||||
const transformer = await Config.original();
|
||||
const transformer = await ExtensionConfigWrapper.original();
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
transformer[settingsPath] = settings;
|
||||
@ -37,7 +38,7 @@ export class SettingsMWs {
|
||||
settings = ConfigClassBuilder.attachPrivateInterface(transformer[settingsPath]).toJSON({
|
||||
skipTags: {secret: true} as TAGS
|
||||
});
|
||||
const original = await Config.original();
|
||||
const original = await ExtensionConfigWrapper.original();
|
||||
// only updating explicitly set config (not saving config set by the diagnostics)
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
|
50
src/backend/model/extension/ExtensionConfigWrapper.ts
Normal file
50
src/backend/model/extension/ExtensionConfigWrapper.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import {IConfigClass} from '../../../../node_modules/typeconfig/common';
|
||||
import {Config, PrivateConfigClass} from '../../../common/config/private/Config';
|
||||
import {ConfigClassBuilder} from '../../../../node_modules/typeconfig/node';
|
||||
import {IExtensionConfig} from './IExtension';
|
||||
import {Utils} from '../../../common/Utils';
|
||||
import {ObjectManagers} from '../ObjectManagers';
|
||||
|
||||
/**
|
||||
* Wraps to original config and makes sure all extension related config is loaded
|
||||
*/
|
||||
export class ExtensionConfigWrapper {
|
||||
static async original(): Promise<PrivateConfigClass & IConfigClass> {
|
||||
const pc = ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass());
|
||||
try {
|
||||
await pc.load();
|
||||
for (const ext of Object.values(ObjectManagers.getInstance().ExtensionManager.extObjects)) {
|
||||
ext.config.loadToConfig(ConfigClassBuilder.attachPrivateInterface(pc));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error during loading original config. Reverting to defaults.');
|
||||
console.error(e);
|
||||
}
|
||||
return pc;
|
||||
}
|
||||
}
|
||||
|
||||
export class ExtensionConfig<C> implements IExtensionConfig<C> {
|
||||
public template: new() => C;
|
||||
|
||||
constructor(private readonly extensionId: string) {
|
||||
}
|
||||
|
||||
public getConfig(): C {
|
||||
return Config.Extensions.configs[this.extensionId] as C;
|
||||
}
|
||||
|
||||
public setTemplate(template: new() => C): void {
|
||||
this.template = template;
|
||||
this.loadToConfig(Config);
|
||||
}
|
||||
|
||||
loadToConfig(config: PrivateConfigClass) {
|
||||
if (!this.template) {
|
||||
return;
|
||||
}
|
||||
const conf = ConfigClassBuilder.attachPrivateInterface(new this.template());
|
||||
conf.__loadJSONObject(Utils.clone(config.Extensions.configs[this.extensionId] || {}));
|
||||
config.Extensions.configs[this.extensionId] = conf;
|
||||
}
|
||||
}
|
@ -3,15 +3,13 @@ import {Config} from '../../../common/config/private/Config';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import {IObjectManager} from '../database/IObjectManager';
|
||||
import {createLoggerWrapper, Logger} from '../../Logger';
|
||||
import {Logger} from '../../Logger';
|
||||
import {IExtensionEvents, IExtensionObject, IServerExtension} from './IExtension';
|
||||
import {Server} from '../../server';
|
||||
import {ExtensionEvent} from './ExtensionEvent';
|
||||
import {ExpressRouterWrapper} from './ExpressRouterWrapper';
|
||||
import * as express from 'express';
|
||||
import {ExtensionApp} from './ExtensionApp';
|
||||
import {ExtensionDB} from './ExtensionDB';
|
||||
import {SQLConnection} from '../database/SQLConnection';
|
||||
import {ExtensionObject} from './ExtensionObject';
|
||||
|
||||
const LOG_TAG = '[ExtensionManager]';
|
||||
|
||||
@ -20,7 +18,7 @@ export class ExtensionManager implements IObjectManager {
|
||||
public static EXTENSION_API_PATH = Config.Server.apiPath + '/extension';
|
||||
|
||||
events: IExtensionEvents;
|
||||
extObjects: { [key: string]: IExtensionObject } = {};
|
||||
extObjects: { [key: string]: ExtensionObject<unknown> } = {};
|
||||
router: express.Router;
|
||||
|
||||
constructor() {
|
||||
@ -73,7 +71,7 @@ export class ExtensionManager implements IObjectManager {
|
||||
Logger.debug(LOG_TAG, 'Extensions found ', JSON.stringify(Config.Extensions.list));
|
||||
}
|
||||
|
||||
private async callServerFN(fn: (ext: IServerExtension, extName: string) => Promise<void>) {
|
||||
private async callServerFN(fn: (ext: IServerExtension<unknown>, extName: string) => Promise<void>) {
|
||||
for (let i = 0; i < Config.Extensions.list.length; ++i) {
|
||||
const extName = Config.Extensions.list[i];
|
||||
const extPath = path.join(ProjectPath.ExtensionFolder, extName);
|
||||
@ -88,17 +86,9 @@ export class ExtensionManager implements IObjectManager {
|
||||
}
|
||||
}
|
||||
|
||||
private createExtensionObject(name: string): IExtensionObject {
|
||||
private createExtensionObject(name: string): IExtensionObject<unknown> {
|
||||
if (!this.extObjects[name]) {
|
||||
const logger = createLoggerWrapper(`[Extension][${name}]`);
|
||||
this.extObjects[name] = {
|
||||
_app: new ExtensionApp(),
|
||||
db: new ExtensionDB(logger),
|
||||
paths: ProjectPath,
|
||||
Logger: logger,
|
||||
events: this.events,
|
||||
RESTApi: new ExpressRouterWrapper(this.router, name, logger)
|
||||
};
|
||||
this.extObjects[name] = new ExtensionObject(name, this.router, this.events);
|
||||
}
|
||||
return this.extObjects[name];
|
||||
}
|
||||
|
31
src/backend/model/extension/ExtensionObject.ts
Normal file
31
src/backend/model/extension/ExtensionObject.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import {IExtensionEvents, IExtensionObject} from './IExtension';
|
||||
import {ExtensionApp} from './ExtensionApp';
|
||||
import {ExtensionConfig} from './ExtensionConfigWrapper';
|
||||
import {ExtensionDB} from './ExtensionDB';
|
||||
import {ProjectPath} from '../../ProjectPath';
|
||||
import {ExpressRouterWrapper} from './ExpressRouterWrapper';
|
||||
import {createLoggerWrapper} from '../../Logger';
|
||||
import * as express from 'express';
|
||||
|
||||
export class ExtensionObject<C> implements IExtensionObject<C> {
|
||||
|
||||
public readonly _app;
|
||||
public readonly config;
|
||||
public readonly db;
|
||||
public readonly paths;
|
||||
public readonly Logger;
|
||||
public readonly events;
|
||||
public readonly RESTApi;
|
||||
|
||||
constructor(public readonly extensionId: string, extensionRouter: express.Router, events: IExtensionEvents) {
|
||||
const logger = createLoggerWrapper(`[Extension][${extensionId}]`);
|
||||
this._app = new ExtensionApp();
|
||||
this.config = new ExtensionConfig<C>(extensionId);
|
||||
this.db = new ExtensionDB(logger);
|
||||
this.paths = ProjectPath;
|
||||
this.Logger = logger;
|
||||
this.events = events;
|
||||
this.RESTApi = new ExpressRouterWrapper(extensionRouter, extensionId, logger);
|
||||
}
|
||||
|
||||
}
|
@ -6,7 +6,7 @@ import {ProjectPathClass} from '../../ProjectPath';
|
||||
import {ILogger} from '../../Logger';
|
||||
import {UserDTO, UserRoles} from '../../../common/entities/UserDTO';
|
||||
import {ParamsDictionary} from 'express-serve-static-core';
|
||||
import {Connection, EntitySchema} from 'typeorm';
|
||||
import {Connection} from 'typeorm';
|
||||
|
||||
|
||||
export type IExtensionBeforeEventHandler<I, O> = (input: { inputs: I }, event: { stopPropagation: boolean }) => Promise<{ inputs: I } | O>;
|
||||
@ -66,17 +66,17 @@ export interface IExtensionApp {
|
||||
export interface IExtensionRESTRoute {
|
||||
/**
|
||||
* Sends a pigallery2 standard JSON object with payload or error message back to the client.
|
||||
* @param paths
|
||||
* @param minRole
|
||||
* @param cb
|
||||
* @param paths RESTapi path, relative to the extension base endpoint
|
||||
* @param minRole set to null to omit auer check (ie make the endpoint public)
|
||||
* @param cb function callback
|
||||
*/
|
||||
jsonResponse(paths: string[], minRole: UserRoles, cb: (params?: ParamsDictionary, body?: any, user?: UserDTO) => Promise<unknown> | unknown): void;
|
||||
|
||||
/**
|
||||
* Exposes a standard expressjs middleware
|
||||
* @param paths
|
||||
* @param minRole
|
||||
* @param mw
|
||||
* @param paths RESTapi path, relative to the extension base endpoint
|
||||
* @param minRole set to null to omit auer check (ie make the endpoint public)
|
||||
* @param mw expressjs middleware
|
||||
*/
|
||||
rawMiddleware(paths: string[], minRole: UserRoles, mw: (req: Request, res: Response, next: NextFunction) => void | Promise<void>): void;
|
||||
}
|
||||
@ -110,13 +110,24 @@ export interface IExtensionDB {
|
||||
_getAllTables(): Function[];
|
||||
}
|
||||
|
||||
export interface IExtensionObject {
|
||||
export interface IExtensionConfig<C> {
|
||||
setTemplate(template: new() => C): void;
|
||||
|
||||
getConfig(): C;
|
||||
}
|
||||
|
||||
export interface IExtensionObject<C> {
|
||||
/**
|
||||
* Inner functionality of the app. Use this with caution.
|
||||
* If you want to go deeper than the standard exposed APIs, you can try doing so here.
|
||||
*/
|
||||
_app: IExtensionApp;
|
||||
|
||||
/**
|
||||
* Create extension related configuration
|
||||
*/
|
||||
config: IExtensionConfig<C>;
|
||||
|
||||
/**
|
||||
* Create new SQL tables and access SQL connection
|
||||
*/
|
||||
@ -144,12 +155,12 @@ export interface IExtensionObject {
|
||||
/**
|
||||
* Extension interface. All extension is expected to implement and export these methods
|
||||
*/
|
||||
export interface IServerExtension {
|
||||
export interface IServerExtension<C> {
|
||||
/**
|
||||
* Extension init function. Extension should at minimum expose this function.
|
||||
* @param extension
|
||||
*/
|
||||
init(extension: IExtensionObject): Promise<void>;
|
||||
init(extension: IExtensionObject<C>): Promise<void>;
|
||||
|
||||
cleanUp?: (extension: IExtensionObject) => Promise<void>;
|
||||
cleanUp?: (extension: IExtensionObject<C>) => Promise<void>;
|
||||
}
|
||||
|
@ -85,16 +85,6 @@ export class PrivateConfigClass extends ServerConfig {
|
||||
this.Environment.isDocker = !!process.env.PI_DOCKER;
|
||||
}
|
||||
|
||||
async original(): Promise<PrivateConfigClass & IConfigClass> {
|
||||
const pc = ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass());
|
||||
try {
|
||||
await pc.load();
|
||||
} catch (e) {
|
||||
console.error('Error during loading original config. Reverting to defaults.');
|
||||
console.error(e);
|
||||
}
|
||||
return pc;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1014,12 +1014,14 @@ export class ServerServiceConfig extends ClientServiceConfig {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SubConfigClass<TAGS>({softReadonly: true})
|
||||
export class ServerExtensionsConfig {
|
||||
@ConfigProperty({volatile: true})
|
||||
list: string[] = [];
|
||||
|
||||
@ConfigProperty({type: 'object'})
|
||||
configs: Record<string, unknown> = {};
|
||||
|
||||
@ConfigProperty({
|
||||
tags: {
|
||||
name: $localize`Clean up unused tables`,
|
||||
|
@ -79,7 +79,10 @@ export class SettingsEntryComponent
|
||||
public arrayType: string;
|
||||
public uiType: string;
|
||||
newThemeModalRef: any;
|
||||
iconModal: { ref?: any, error?: string };
|
||||
iconModal: {
|
||||
ref?: any,
|
||||
error?: string
|
||||
};
|
||||
@Input() noChangeDetection = false;
|
||||
public readonly ConfigStyle = ConfigStyle;
|
||||
protected readonly SortByTypes = SortByTypes;
|
||||
@ -177,9 +180,9 @@ export class SettingsEntryComponent
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof this.state.value === 'object') {
|
||||
this.state.value = JSON.parse(value);
|
||||
return;
|
||||
}
|
||||
|
||||
this.state.value = value;
|
||||
@ -198,7 +201,9 @@ export class SettingsEntryComponent
|
||||
}
|
||||
|
||||
|
||||
get SelectedThemeSettings(): { theme: string } {
|
||||
get SelectedThemeSettings(): {
|
||||
theme: string
|
||||
} {
|
||||
return (this.state.value as ThemeConfig[]).find(th => th.name === (this.state.rootConfig as any).__state.selectedTheme.value) || {theme: 'N/A'};
|
||||
}
|
||||
|
||||
@ -306,7 +311,12 @@ export class SettingsEntryComponent
|
||||
}
|
||||
}
|
||||
|
||||
getOptionsView(state: IState & { optionsView?: { key: number | string; value: string | number }[] }) {
|
||||
getOptionsView(state: IState & {
|
||||
optionsView?: {
|
||||
key: number | string;
|
||||
value: string | number
|
||||
}[]
|
||||
}) {
|
||||
if (!state.optionsView) {
|
||||
const eClass = state.isEnumType
|
||||
? state.type
|
||||
|
@ -7,6 +7,7 @@ import {ProjectPath} from '../../../../../src/backend/ProjectPath';
|
||||
import {TAGS} from '../../../../../src/common/config/public/ClientConfig';
|
||||
import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers';
|
||||
import {UserRoles} from '../../../../../src/common/entities/UserDTO';
|
||||
import {ExtensionConfigWrapper} from '../../../../../src/backend/model/extension/ExtensionConfigWrapper';
|
||||
|
||||
process.env.NODE_ENV = 'test';
|
||||
const chai: any = require('chai');
|
||||
@ -34,7 +35,7 @@ describe('SettingsRouter', () => {
|
||||
it('it should GET the settings', async () => {
|
||||
Config.Users.authenticationRequired = false;
|
||||
Config.Users.unAuthenticatedUserRole = UserRoles.Admin;
|
||||
const originalSettings = await Config.original();
|
||||
const originalSettings = await ExtensionConfigWrapper.original();
|
||||
const srv = new Server();
|
||||
await srv.onStarted.wait();
|
||||
const result = await chai.request(srv.App)
|
||||
|
@ -9,6 +9,7 @@ import {UserRoles} from '../../../../../src/common/entities/UserDTO';
|
||||
import {ConfigClassBuilder} from '../../../../../node_modules/typeconfig/node';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import {ExtensionConfigWrapper} from '../../../../../src/backend/model/extension/ExtensionConfigWrapper';
|
||||
|
||||
|
||||
declare const describe: any;
|
||||
@ -74,7 +75,7 @@ describe('Settings middleware', () => {
|
||||
expect(Config.Users.enforcedUsers.length).to.be.equal(1);
|
||||
expect(Config.Users.enforcedUsers[0].name).to.be.equal('Apple');
|
||||
expect(Config.Users.enforcedUsers.length).to.be.equal(1);
|
||||
Config.original().then((cfg) => {
|
||||
ExtensionConfigWrapper.original().then((cfg) => {
|
||||
try {
|
||||
expect(cfg.Users.enforcedUsers.length).to.be.equal(1);
|
||||
expect(cfg.Users.enforcedUsers[0].name).to.be.equal('Apple');
|
||||
|
Loading…
Reference in New Issue
Block a user