Skip to content
Snippets Groups Projects
Commit af4d2c40 authored by Alexander Rose's avatar Alexander Rose
Browse files

Merge branch 'master' of https://github.com/molstar/molstar

parents 3b1a2f19 96103458
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* Swagger UI support. * Swagger UI support.
* Response schemas. * Response schemas.
* Bug fixes. * Bug fixes.
* Refactored config which can now be provided as a seprate JSON file.
# 0.8.0 # 0.8.0
* Let's call this an initial version. * Let's call this an initial version.
\ No newline at end of file
/** /**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
const config = { const DefaultModelServerConfig = {
/** /**
* Determine if and how long to cache entries after a request. * Determine if and how long to cache entries after a request.
*/ */
...@@ -50,6 +50,7 @@ const config = { ...@@ -50,6 +50,7 @@ const config = {
/** /**
* Provide a property config or a path a JSON file with the config. * Provide a property config or a path a JSON file with the config.
*/ */
// TODO: finish customProperty support
customProperties: <import('./property-provider').ModelPropertyProviderConfig | string>{ customProperties: <import('./property-provider').ModelPropertyProviderConfig | string>{
sources: [ sources: [
// 'pdbe', // 'pdbe',
...@@ -79,23 +80,37 @@ const config = { ...@@ -79,23 +80,37 @@ const config = {
} }
}, },
/**
* Default source for fileMapping.
*/
defaultSource: 'pdb-cif' as string,
/** /**
* Maps a request identifier to a filename. * Maps a request identifier to a filename given a 'source' and 'id' variables.
* *
* @param source * /static query uses 'pdb-cif' and 'pdb-bcif' source names.
* Source of the data.
* @param id
* Id provided in the request.
*/ */
mapFile(source: string, id: string) { fileMapping: [
switch (source.toLowerCase()) { ['pdb-cif', 'e:/test/quick/${id}_updated.cif'],
case 'pdb': return `e:/test/quick/${id}_updated.cif`; // ['pdb-bcif', 'e:/test/quick/${id}.bcif'],
// case 'pdb': return `e:/test/mol-star/model/out/${id}_updated.bcif`; ] as [string, string][]
// case 'pdb-bcif': return `c:/test/mol-star/model/out/${id}_updated.bcif`;
// case 'pdb-cif': return `c:/test/mol-star/model/out/${id}_updated.cif`;
default: return void 0;
}
}
}; };
export default config; export type ModelServerConfig = typeof DefaultModelServerConfig
\ No newline at end of file export const ModelServerConfig = DefaultModelServerConfig
export let mapSourceAndIdToFilename: (source: string, id: string) => string = () => {
throw new Error('call setupConfig to initialize this function');
}
export function setupConfig(cfg?: ModelServerConfig) {
if (cfg) Object.assign(ModelServerConfig, cfg);
if (!ModelServerConfig.fileMapping) return;
mapSourceAndIdToFilename = new Function('source', 'id', [
'switch (source.toLowerCase()) {',
...ModelServerConfig.fileMapping.map(([source, path]) => `case '${source.toLowerCase()}': return \`${path}\`;`),
'}',
].join('\n')) as any;
}
\ No newline at end of file
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
import * as fs from 'fs' import * as fs from 'fs'
import { Model } from '../../mol-model/structure'; import { Model } from '../../mol-model/structure';
import Config from './config'; import { ModelServerConfig as Config } from './config';
import { ConsoleLogger } from '../../mol-util/console-logger'; import { ConsoleLogger } from '../../mol-util/console-logger';
// TODO enable dynamic imports again // TODO enable dynamic imports again
......
/** /**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
* *
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
import * as express from 'express' import * as express from 'express'
import * as compression from 'compression' import * as compression from 'compression'
import ServerConfig from './config' import * as fs from 'fs'
import * as argparse from 'argparse'
import { ModelServerConfig as ServerConfig, setupConfig } from './config'
import { ConsoleLogger } from '../../mol-util/console-logger'; import { ConsoleLogger } from '../../mol-util/console-logger';
import { PerformanceMonitor } from '../../mol-util/performance-monitor'; import { PerformanceMonitor } from '../../mol-util/performance-monitor';
import { initWebApi } from './server/api-web'; import { initWebApi } from './server/api-web';
...@@ -39,34 +41,37 @@ function setupShutdown() { ...@@ -39,34 +41,37 @@ function setupShutdown() {
} }
} }
const port = process.env.port || ServerConfig.defaultPort; const cmdParser = new argparse.ArgumentParser({
addHelp: true
});
cmdParser.addArgument(['--cfg'], { help: 'Config file path.', required: false });
interface CmdArgs {
cfg?: string
}
const cmdArgs = cmdParser.parseArgs() as CmdArgs;
function startServer() { function startServer() {
let app = express(); let app = express();
app.use(compression(<any>{ level: 6, memLevel: 9, chunkSize: 16 * 16384, filter: () => true })); app.use(compression(<any>{ level: 6, memLevel: 9, chunkSize: 16 * 16384, filter: () => true }));
// app.get(ServerConfig.appPrefix + '/documentation', (req, res) => { const cfg = cmdArgs.cfg ? JSON.parse(fs.readFileSync(cmdArgs.cfg, 'utf8')) : void 0;
// res.writeHead(200, { 'Content-Type': 'text/html' }); setupConfig(cfg);
// res.write(Documentation.getHTMLDocs(ServerConfig.appPrefix));
// res.end();
// });
initWebApi(app); initWebApi(app);
// app.get('*', (req, res) => { const port = process.env.port || ServerConfig.defaultPort;
// res.writeHead(200, { 'Content-Type': 'text/html' });
// res.write(Documentation.getHTMLDocs(ServerConfig.appPrefix));
// res.end();
// });
app.listen(port); app.listen(port);
console.log(`Mol* ModelServer ${Version}`);
console.log(``);
console.log(`The server is running on port ${port}.`);
console.log(``);
} }
startServer(); startServer();
console.log(`Mol* ModelServer ${Version}`);
console.log(``);
console.log(`The server is running on port ${port}.`);
console.log(``);
if (ServerConfig.shutdownParams && ServerConfig.shutdownParams.timeoutMinutes > 0) { if (ServerConfig.shutdownParams && ServerConfig.shutdownParams.timeoutMinutes > 0) {
setupShutdown(); setupShutdown();
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
import VERSION from '../version' import VERSION from '../version'
import { QueryParamInfo, QueryParamType, QueryDefinition, CommonQueryParamsInfo, QueryList } from './api'; import { QueryParamInfo, QueryParamType, QueryDefinition, CommonQueryParamsInfo, QueryList } from './api';
import ServerConfig from '../config'; import { ModelServerConfig as ServerConfig } from '../config';
export const shortcutIconLink = `<link rel='shortcut icon' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURQAAAMIrHrspHr0oH7soILonHrwqH7onILsoHrsoH7soH7woILwpIKgVokoAAAAMdFJOUwAQHzNxWmBHS5XO6jdtAmoAAACZSURBVDjLxZNRCsQgDAVNXmwb9f7nXZEaLRgXloXOhwQdjMYYwpOLw55fBT46KhbOKhmRR2zLcFJQj8UR+HxFgArIF5BKJbEncC6NDEdI5SatBRSDJwGAoiFDONrEJXWYhGMIcRJGCrb1TOtDahfUuQXd10jkFYq0ViIrbUpNcVT6redeC1+b9tH2WLR93Sx2VCzkv/7NjfABxjQHksGB7lAAAAAASUVORK5CYII=' />` export const shortcutIconLink = `<link rel='shortcut icon' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURQAAAMIrHrspHr0oH7soILonHrwqH7onILsoHrsoH7soH7woILwpIKgVokoAAAAMdFJOUwAQHzNxWmBHS5XO6jdtAmoAAACZSURBVDjLxZNRCsQgDAVNXmwb9f7nXZEaLRgXloXOhwQdjMYYwpOLw55fBT46KhbOKhmRR2zLcFJQj8UR+HxFgArIF5BKJbEncC6NDEdI5SatBRSDJwGAoiFDONrEJXWYhGMIcRJGCrb1TOtDahfUuQXd10jkFYq0ViIrbUpNcVT6redeC1+b9tH2WLR93Sx2VCzkv/7NjfABxjQHksGB7lAAAAAASUVORK5CYII=' />`
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as express from 'express'; import * as express from 'express';
import Config from '../config'; import { ModelServerConfig as Config, ModelServerConfig, mapSourceAndIdToFilename } from '../config';
import { ConsoleLogger } from '../../../mol-util/console-logger'; import { ConsoleLogger } from '../../../mol-util/console-logger';
import { resolveJob } from './query'; import { resolveJob } from './query';
import { JobManager } from './jobs'; import { JobManager } from './jobs';
...@@ -92,7 +92,7 @@ function mapQuery(app: express.Express, queryName: string, queryDefinition: Quer ...@@ -92,7 +92,7 @@ function mapQuery(app: express.Express, queryName: string, queryDefinition: Quer
const queryParams = normalizeRestQueryParams(queryDefinition, req.query); const queryParams = normalizeRestQueryParams(queryDefinition, req.query);
const commonParams = normalizeRestCommonParams(req.query); const commonParams = normalizeRestCommonParams(req.query);
const jobId = JobManager.add({ const jobId = JobManager.add({
sourceId: commonParams.data_source || 'pdb', sourceId: commonParams.data_source || ModelServerConfig.defaultSource,
entryId, entryId,
queryName: queryName as any, queryName: queryName as any,
queryParams, queryParams,
...@@ -107,7 +107,7 @@ export function initWebApi(app: express.Express) { ...@@ -107,7 +107,7 @@ export function initWebApi(app: express.Express) {
app.get(makePath('static/:format/:id'), async (req, res) => { app.get(makePath('static/:format/:id'), async (req, res) => {
const binary = req.params.format === 'bcif'; const binary = req.params.format === 'bcif';
const id = req.params.id; const id = req.params.id;
const fn = Config.mapFile(binary ? 'pdb-bcif' : 'pdb-cif', id); const fn = mapSourceAndIdToFilename(binary ? 'pdb-bcif' : 'pdb-cif', id);
if (!fn || !fs.existsSync(fn)) { if (!fn || !fs.existsSync(fn)) {
res.status(404); res.status(404);
res.end(); res.end();
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
import { ConsoleLogger } from '../../../mol-util/console-logger' import { ConsoleLogger } from '../../../mol-util/console-logger'
import { LinkedList } from '../../../mol-data/generic'; import { LinkedList } from '../../../mol-data/generic';
import ServerConfig from '../config'; import { ModelServerConfig as ServerConfig } from '../config';
interface CacheEntry<T> { interface CacheEntry<T> {
key: string, key: string,
......
...@@ -12,7 +12,7 @@ import { Progress } from '../../../mol-task'; ...@@ -12,7 +12,7 @@ import { Progress } from '../../../mol-task';
import { now } from '../../../mol-util/now'; import { now } from '../../../mol-util/now';
import { ConsoleLogger } from '../../../mol-util/console-logger'; import { ConsoleLogger } from '../../../mol-util/console-logger';
import { PerformanceMonitor } from '../../../mol-util/performance-monitor'; import { PerformanceMonitor } from '../../../mol-util/performance-monitor';
import Config from '../config'; import { ModelServerConfig as Config } from '../config';
import Version from '../version'; import Version from '../version';
import { Job } from './jobs'; import { Job } from './jobs';
import { createStructureWrapperFromJob, StructureWrapper, resolveStructures } from './structure-wrapper'; import { createStructureWrapperFromJob, StructureWrapper, resolveStructures } from './structure-wrapper';
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
import { Structure, Model } from '../../../mol-model/structure'; import { Structure, Model } from '../../../mol-model/structure';
import { PerformanceMonitor } from '../../../mol-util/performance-monitor'; import { PerformanceMonitor } from '../../../mol-util/performance-monitor';
import { Cache } from './cache'; import { Cache } from './cache';
import Config from '../config'; import { ModelServerConfig as Config, mapSourceAndIdToFilename } from '../config';
import { CIF, CifFrame, CifBlock } from '../../../mol-io/reader/cif' import { CIF, CifFrame, CifBlock } from '../../../mol-io/reader/cif'
import * as util from 'util' import * as util from 'util'
import * as fs from 'fs' import * as fs from 'fs'
...@@ -109,7 +109,7 @@ export async function readDataAndFrame(filename: string, key?: string): Promise< ...@@ -109,7 +109,7 @@ export async function readDataAndFrame(filename: string, key?: string): Promise<
} }
export async function readStructureWrapper(key: string, sourceId: string | '_local_', entryId: string, propertyProvider: ModelPropertiesProvider | undefined) { export async function readStructureWrapper(key: string, sourceId: string | '_local_', entryId: string, propertyProvider: ModelPropertiesProvider | undefined) {
const filename = sourceId === '_local_' ? entryId : Config.mapFile(sourceId, entryId); const filename = sourceId === '_local_' ? entryId : mapSourceAndIdToFilename(sourceId, entryId);
if (!filename) throw new Error(`Cound not map '${key}' to a valid filename.`); if (!filename) throw new Error(`Cound not map '${key}' to a valid filename.`);
if (!fs.existsSync(filename)) throw new Error(`Could not find source file for '${key}'.`); if (!fs.existsSync(filename)) throw new Error(`Could not find source file for '${key}'.`);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment