Fix ServeStaticDirectoryComponent while developping swaf

Also move core version detection to Application
This commit is contained in:
Alice Gaudon 2021-03-24 16:08:50 +01:00
parent 69e9f3ce9c
commit 4cbc73a25f
3 changed files with 40 additions and 26 deletions

View File

@ -21,6 +21,7 @@ import TemplateError = lib.TemplateError;
export default abstract class Application implements Extendable<ApplicationComponent | WebSocketListener<Application>> { export default abstract class Application implements Extendable<ApplicationComponent | WebSocketListener<Application>> {
private readonly version: string; private readonly version: string;
private coreVersion: string = 'unknown';
private readonly ignoreCommandLine: boolean; private readonly ignoreCommandLine: boolean;
private readonly controllers: Controller[] = []; private readonly controllers: Controller[] = [];
private readonly webSocketListeners: { [p: string]: WebSocketListener<Application> } = {}; private readonly webSocketListeners: { [p: string]: WebSocketListener<Application> } = {};
@ -58,7 +59,20 @@ export default abstract class Application implements Extendable<ApplicationCompo
} }
public async start(): Promise<void> { public async start(): Promise<void> {
logger.info(`${config.get('app.name')} v${this.version} - hi`); // Load core version
const file = this.isInNodeModules() ?
path.join(__dirname, '../../package.json') :
path.join(__dirname, '../package.json');
try {
this.coreVersion = JSON.parse(fs.readFileSync(file).toString()).version;
} catch (e) {
logger.warn('Couldn\'t determine coreVersion.', e);
}
logger.info(`${config.get('app.name')} v${this.version} | swaf v${this.coreVersion}`);
// Catch interrupt signals
process.once('SIGINT', () => { process.once('SIGINT', () => {
this.stop().catch(console.error); this.stop().catch(console.error);
}); });
@ -264,14 +278,6 @@ export default abstract class Application implements Extendable<ApplicationCompo
}); });
} }
public isReady(): boolean {
return this.ready;
}
public getVersion(): string {
return this.version;
}
public getWebSocketListeners(): { [p: string]: WebSocketListener<Application> } { public getWebSocketListeners(): { [p: string]: WebSocketListener<Application> } {
return this.webSocketListeners; return this.webSocketListeners;
} }
@ -292,4 +298,20 @@ export default abstract class Application implements Extendable<ApplicationCompo
Object.values(this.webSocketListeners).find(listener => listener.constructor === type); Object.values(this.webSocketListeners).find(listener => listener.constructor === type);
return module ? module as C : null; return module ? module as C : null;
} }
public isInNodeModules(): boolean {
return fs.existsSync(path.join(__dirname, '../../package.json'));
}
public isReady(): boolean {
return this.ready;
}
public getVersion(): string {
return this.version;
}
public getCoreVersion(): string {
return this.coreVersion;
}
} }

View File

@ -7,8 +7,6 @@ import * as querystring from "querystring";
import {ParsedUrlQueryInput} from "querystring"; import {ParsedUrlQueryInput} from "querystring";
import * as util from "util"; import * as util from "util";
import * as path from "path"; import * as path from "path";
import * as fs from "fs";
import {logger} from "../Logger";
import Middleware from "../Middleware"; import Middleware from "../Middleware";
export default class NunjucksComponent extends ApplicationComponent { export default class NunjucksComponent extends ApplicationComponent {
@ -21,17 +19,6 @@ export default class NunjucksComponent extends ApplicationComponent {
} }
public async start(app: Express): Promise<void> { public async start(app: Express): Promise<void> {
let coreVersion = 'unknown';
const file = fs.existsSync(path.join(__dirname, '../../package.json')) ?
path.join(__dirname, '../../package.json') :
path.join(__dirname, '../package.json');
try {
coreVersion = JSON.parse(fs.readFileSync(file).toString()).version;
} catch (e) {
logger.warn('Couldn\'t determine coreVersion.', e);
}
const opts = { const opts = {
autoescape: true, autoescape: true,
noCache: !config.get('view.cache'), noCache: !config.get('view.cache'),
@ -51,7 +38,7 @@ export default class NunjucksComponent extends ApplicationComponent {
return Controller.route(route, params, query, absolute); return Controller.route(route, params, query, absolute);
}) })
.addGlobal('app_version', this.getApp().getVersion()) .addGlobal('app_version', this.getApp().getVersion())
.addGlobal('core_version', coreVersion) .addGlobal('core_version', this.getApp().getCoreVersion())
.addGlobal('querystring', querystring) .addGlobal('querystring', querystring)
.addGlobal('app', config.get('app')) .addGlobal('app', config.get('app'))

View File

@ -2,6 +2,7 @@ import ApplicationComponent from "../ApplicationComponent";
import express, {Router} from "express"; import express, {Router} from "express";
import {PathParams} from "express-serve-static-core"; import {PathParams} from "express-serve-static-core";
import * as path from "path"; import * as path from "path";
import {logger} from "../Logger";
export default class ServeStaticDirectoryComponent extends ApplicationComponent { export default class ServeStaticDirectoryComponent extends ApplicationComponent {
private readonly root: string; private readonly root: string;
@ -9,16 +10,20 @@ export default class ServeStaticDirectoryComponent extends ApplicationComponent
public constructor(root: string, routePath?: PathParams) { public constructor(root: string, routePath?: PathParams) {
super(); super();
this.root = path.join(__dirname, '../../../', root); this.root = root;
this.path = routePath; this.path = routePath;
} }
public async init(router: Router): Promise<void> { public async init(router: Router): Promise<void> {
const resolvedRoot = path.join(__dirname, this.getApp().isInNodeModules() ? '../../../' : '../../', this.root);
if (this.path) { if (this.path) {
router.use(this.path, express.static(this.root, {maxAge: 1000 * 3600 * 72})); router.use(this.path, express.static(resolvedRoot, {maxAge: 1000 * 3600 * 72}));
} else { } else {
router.use(express.static(this.root, {maxAge: 1000 * 3600 * 72})); router.use(express.static(resolvedRoot, {maxAge: 1000 * 3600 * 72}));
} }
logger.info('Serving static files in', resolvedRoot, ' on ', this.path || '/');
} }
} }