Explorar el Código

model-server: call for static input files, api versioning

David Sehnal hace 6 años
padre
commit
e6e5148694
Se han modificado 2 ficheros con 33 adiciones y 1 borrados
  1. 2 0
      src/servers/model/config.ts
  2. 31 1
      src/servers/model/server/api-web.ts

+ 2 - 0
src/servers/model/config.ts

@@ -67,6 +67,8 @@ const config = {
         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 `e:/test/mol-star/model/out/${id}_updated.bcif`;
+            case 'pdb-cif': return `e:/test/mol-star/model/out/${id}_updated.cif`;
             default: return void 0;
         }
     }

+ 31 - 1
src/servers/model/server/api-web.ts

@@ -4,6 +4,8 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
+import * as fs from 'fs';
+import * as path from 'path';
 import * as express from 'express';
 import Config from '../config';
 import { ConsoleLogger } from 'mol-util/console-logger';
@@ -97,7 +99,35 @@ async function processNextJob() {
 // }
 
 export function initWebApi(app: express.Express) {
-    app.get(makePath('query'), (req, res) => {
+    app.get(makePath('static/:format/:id'), async (req, res) => {
+        const binary = req.params.format === 'bcif';
+        const id = req.params.id;
+        const fn = Config.mapFile(binary ? 'pdb-bcif' : 'pdb-cif', id);
+        if (!fn || !fs.existsSync(fn)) {
+            res.status(404);
+            res.end();
+            return;
+        }
+        fs.readFile(fn, (err, data) => {
+            if (err) {
+                res.status(404);
+                res.end();
+                return;
+            }
+
+            const f = path.parse(fn);
+            res.writeHead(200, {
+                'Content-Type': binary ? 'application/octet-stream' : 'text/plain; charset=utf-8',
+                'Access-Control-Allow-Origin': '*',
+                'Access-Control-Allow-Headers': 'X-Requested-With',
+                'Content-Disposition': `inline; filename="${f.name}${f.ext}"`
+            });
+            res.write(data);
+            res.end();
+        });
+    })
+
+    app.get(makePath('api/v1'), (req, res) => {
         const query = /\?(.*)$/.exec(req.url)![1];
         const args = JSON.parse(decodeURIComponent(query));
         const name = args.name;