Browse Source

model-server: config can be (partially) provided as a separate JSON file

David Sehnal 5 years ago
parent
commit
961034584a
3 changed files with 27 additions and 107 deletions
  1. 1 0
      src/servers/model/CHANGELOG.md
  2. 3 99
      src/servers/model/config.ts
  3. 23 8
      src/servers/model/server.ts

+ 1 - 0
src/servers/model/CHANGELOG.md

@@ -3,6 +3,7 @@
 * Swagger UI support.
 * Response schemas.
 * Bug fixes.
+* Refactored config which can now be provided as a seprate JSON file.
 
 # 0.8.0
 * Let's call this an initial version.

+ 3 - 99
src/servers/model/config.ts

@@ -50,7 +50,7 @@ const DefaultModelServerConfig = {
     /**
      * Provide a property config or a path a JSON file with the config.
      */
-    // TODO: finish this
+    // TODO: finish customProperty support
     customProperties: <import('./property-provider').ModelPropertyProviderConfig | string>{
         sources: [
             // 'pdbe',
@@ -88,7 +88,7 @@ const DefaultModelServerConfig = {
 
     /**
      * Maps a request identifier to a filename given a 'source' and 'id' variables.
-     * 
+     *
      * /static query uses 'pdb-cif' and 'pdb-bcif' source names.
      */
     fileMapping: [
@@ -113,100 +113,4 @@ export function setupConfig(cfg?: ModelServerConfig) {
         ...ModelServerConfig.fileMapping.map(([source, path]) => `case '${source.toLowerCase()}': return \`${path}\`;`),
         '}',
     ].join('\n')) as any;
-}
-
-// const config = {
-//     /**
-//      * Determine if and how long to cache entries after a request.
-//      */
-//     cacheParams: {
-//         useCache: true,
-//         maxApproximateSizeInBytes: 2 * 1014 * 1024 * 1024, // 2 GB
-//         entryTimeoutInMs: 10 * 60 * 1000 // 10 minutes
-//     },
-
-//     /**
-//      * Node (V8) sometimes exhibits GC related issues  that significantly slow down the execution
-//      * (https://github.com/nodejs/node/issues/8670).
-//      *
-//      * Therefore an option is provided that automatically shuts down the server.
-//      * For this to work, the server must be run using a deamon (i.e. forever.js on Linux
-//      * or IISnode on Windows) so that the server is automatically restarted when the shutdown happens.
-//      */
-//     shutdownParams: {
-//         // 0 for off, server will shut down after this amount of minutes.
-//         timeoutMinutes: 24 * 60 /* a day */,
-
-//         // modifies the shutdown timer by +/- timeoutVarianceMinutes (to avoid multiple instances shutting at the same time)
-//         timeoutVarianceMinutes: 60
-//     },
-
-//     defaultPort: 1337,
-
-//     /**
-//      * Specify the prefix of the API, i.e.
-//      * <host>/<apiPrefix>/<API queries>
-//      */
-//     appPrefix: '/ModelServer',
-
-//     /**
-//      * The maximum time the server dedicates to executing a query.
-//      * Does not include the time it takes to read and export the data.
-//      */
-//     maxQueryTimeInMs: 5 * 1000,
-
-//     /** Maximum number of requests before "server busy" */
-//     maxQueueLength: 30,
-
-//     /**
-//      * Provide a property config or a path a JSON file with the config.
-//      */
-//     customProperties: <import('./property-provider').ModelPropertyProviderConfig | string>{
-//         sources: [
-//             // 'pdbe',
-//             // 'rcsb',
-//             // 'wwpdb'
-//         ],
-//         params: {
-//             PDBe: {
-//                 UseFileSource: false,
-//                 API: {
-//                     residuewise_outlier_summary: 'https://www.ebi.ac.uk/pdbe/api/validation/residuewise_outlier_summary/entry',
-//                     preferred_assembly: 'https://www.ebi.ac.uk/pdbe/api/pdb/entry/summary',
-//                     struct_ref_domain: 'https://www.ebi.ac.uk/pdbe/api/mappings/sequence_domains'
-//                 },
-//                 File: {
-//                     residuewise_outlier_summary: 'e:/test/mol-star/model/props/'
-//                 }
-//             },
-//             RCSB: {
-//                 API: {
-//                     assembly_symmetry: 'https://rest-staging.rcsb.org/graphql'
-//                 }
-//             },
-//             wwPDB: {
-//                 chemCompBondTablePath: ''
-//             }
-//         }
-//     },
-
-//     /**
-//      * Maps a request identifier to a filename.
-//      *
-//      * @param source
-//      *   Source of the data.
-//      * @param id
-//      *   Id provided in the request.
-//      */
-//     mapFile(source: string, id: string) {
-//         switch (source.toLowerCase()) {
-//             case 'pdb': return `e:/test/quick/${id}_updated.cif`;
-//             // case 'pdb': return `e:/test/mol-star/model/out/${id}_updated.bcif`;
-//             // 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;
+}

+ 23 - 8
src/servers/model/server.ts

@@ -1,11 +1,13 @@
 /**
- * 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>
  */
 
 import * as express from 'express'
 import * as compression from 'compression'
+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 { PerformanceMonitor } from '../../mol-util/performance-monitor';
@@ -39,24 +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() {
     let app = express();
     app.use(compression(<any>{ level: 6, memLevel: 9, chunkSize: 16 * 16384, filter: () => true }));
 
-    // TODO: read JSON file
-    setupConfig();
+    const cfg = cmdArgs.cfg ? JSON.parse(fs.readFileSync(cmdArgs.cfg, 'utf8')) : void 0;
+    setupConfig(cfg);
 
     initWebApi(app);
+
+    const port = process.env.port || ServerConfig.defaultPort;
     app.listen(port);
+
+    console.log(`Mol* ModelServer ${Version}`);
+    console.log(``);
+    console.log(`The server is running on port ${port}.`);
+    console.log(``);
 }
 
 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) {
     setupShutdown();