Fix ServeStaticDirectoryComponent while developping swaf
Also move core version detection to Application
This commit is contained in:
parent
a21719d305
commit
f7fa3ca67f
@ -21,6 +21,7 @@ import TemplateError = lib.TemplateError;
|
||||
|
||||
export default abstract class Application implements Extendable<ApplicationComponent | WebSocketListener<Application>> {
|
||||
private readonly version: string;
|
||||
private coreVersion: string = 'unknown';
|
||||
private readonly ignoreCommandLine: boolean;
|
||||
private readonly controllers: Controller[] = [];
|
||||
private readonly webSocketListeners: { [p: string]: WebSocketListener<Application> } = {};
|
||||
@ -58,7 +59,20 @@ export default abstract class Application implements Extendable<ApplicationCompo
|
||||
}
|
||||
|
||||
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', () => {
|
||||
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> } {
|
||||
return this.webSocketListeners;
|
||||
}
|
||||
@ -292,4 +298,20 @@ export default abstract class Application implements Extendable<ApplicationCompo
|
||||
Object.values(this.webSocketListeners).find(listener => listener.constructor === type);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -21,17 +21,6 @@ export default class NunjucksComponent extends ApplicationComponent {
|
||||
}
|
||||
|
||||
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 = {
|
||||
autoescape: true,
|
||||
noCache: !config.get('view.cache'),
|
||||
@ -51,7 +40,7 @@ export default class NunjucksComponent extends ApplicationComponent {
|
||||
return Controller.route(route, params, query, absolute);
|
||||
})
|
||||
.addGlobal('app_version', this.getApp().getVersion())
|
||||
.addGlobal('core_version', coreVersion)
|
||||
.addGlobal('core_version', this.getApp().getCoreVersion())
|
||||
.addGlobal('querystring', querystring)
|
||||
.addGlobal('app', config.get('app'))
|
||||
|
||||
|
@ -2,6 +2,7 @@ import ApplicationComponent from "../ApplicationComponent";
|
||||
import express, {Router} from "express";
|
||||
import {PathParams} from "express-serve-static-core";
|
||||
import * as path from "path";
|
||||
import {logger} from "../Logger";
|
||||
|
||||
export default class ServeStaticDirectoryComponent extends ApplicationComponent {
|
||||
private readonly root: string;
|
||||
@ -9,16 +10,20 @@ export default class ServeStaticDirectoryComponent extends ApplicationComponent
|
||||
|
||||
public constructor(root: string, routePath?: PathParams) {
|
||||
super();
|
||||
this.root = path.join(__dirname, '../../../', root);
|
||||
this.root = root;
|
||||
this.path = routePath;
|
||||
}
|
||||
|
||||
public async init(router: Router): Promise<void> {
|
||||
const resolvedRoot = path.join(__dirname, this.getApp().isInNodeModules() ? '../../../' : '../../', this.root);
|
||||
|
||||
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 {
|
||||
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 || '/');
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user