Commit 57ba2513 authored by Martin Scholz's avatar Martin Scholz
Browse files

Add saving manifest to file, systemd service def

parent 0bb36b42
# This is a template for running g-imfdt as a systemd service
[Unit]
Description=g-imfdt: Generate IIIF Manifests from Digitool
After=network.target
[Service]
Type=simple
# uncomment next lines to support https; adjust paths!
#Environment=HTTPS=on
#Environment=CRED_PRIV=path/to/private.key
#Environment=CRED_CERT=/path/to/certificate
ExecStart=/usr/bin/node /path/to/g-imfdt/g-imfdt.js
Restart=on-failure
# change the user as necessary
User=nobody
[Install]
WantedBy=multi-user.target
......@@ -15,12 +15,12 @@
* These constants can be set be environment variables; but default is a reasonable value
*/
// if the env is not defined, we use https
const USE_HTTPS = (typeof process.env.USE_HTTPS === 'undefined') || !(/^(false|no|0)$/.test(process.env.USE_HTTPS));
const USE_HTTPS = (typeof process.env.USE_HTTPS !== 'undefined') && !(/^(false|no|0)$/.test(process.env.USE_HTTPS));
const PORT = process.env.PORT || 27666;
const PORT_HTTPS = process.env.PORT_HTTPS || 27661;
const CRED_PRIV = process.env.CRED_PRIV || '/etc/ssl/private/dhlab.ub.fau.de.key';
const CRED_CERT = process.env.CRED_CERT || '/etc/ssl/certs/dhlab.ub.fau.de.pem';
const CRED_PRIV = process.env.CRED_PRIV;
const CRED_CERT = process.env.CRED_CERT;
const DATA_DIR = process.env.DATA_DIR || '/var/www/html/iiif'; // no trailing slash!
/*
* URLs for fetching data; should normally not be changed
......@@ -30,6 +30,7 @@ const URL_INFO_JSON = "http://dhlab.ub.fau.de:8182/iiif/2/${pid}/info.j
const URL_MIDMAP_BY_PID = "http://dhlab.ub.fau.de:28777/pid/${pid}";
const URL_B3KAT_SRU = "http://bvbr.bib-bvb.de:5661/bvb01sru?version=1.1&recordSchema=marcxml&operation=searchRetrieve&maximumRecords=1&query=marcxml.idn%3D%22${bvnr}%22"
const DATA_SUBDIR_MANIFESTS = 'manifest'; // no leading and trailing slashes!
/*
* Constants for manifest files
......@@ -64,7 +65,6 @@ const app = express();
var http = require('http');
var serverHttp = http.createServer(app);
var serverHttps = null;
console.log('Using HTTP' + (USE_HTTPS ? 'S' : '') + ' env is ' + process.env.USE_HTTPS);
if (USE_HTTPS) {
var https = require('https');
var privateKey = fs.readFileSync(CRED_PRIV, 'utf8');
......@@ -72,6 +72,7 @@ if (USE_HTTPS) {
var credentials = {key: privateKey, cert: certificate};
serverHttps = https.createServer(credentials, app);
}
console.log('Using HTTP' + (USE_HTTPS ? 'S' : ''));
......@@ -107,14 +108,20 @@ app.use(function(req, res, next) {
*
*/
app.route('/:pid/manifest').get(function(req, res, next) {
app.generateManifest(req.params.pid)
.then(manifest => res.json(manifest))
.then(() => console.log("processed request for pid " + req.params.pid + " in " + (Date.now() - timestamp) + " millisecs"))
.catch(err => next(err));
var timestamp = Date.now();
app.checkAccessPid(req.params.pid)
.then(() => app.generateManifest(req.params.pid))
.then(manifest => Promise.all([
res.json(manifest),
app.saveToFile(manifest)
]))
.then(
() => console.log("processed request for pid " + req.params.pid + " in " + (Date.now() - timestamp) + " millisecs"),
err => next(err)
);
});
/* **
* HELPER FUNCTIONS
* **/
......@@ -128,6 +135,14 @@ app.substitutePlaceholders = function(str, map) {
};
/** Perform checks on Pid whether we allow access
*
*/
app.checkAccessPid = function(pid) {
return Promise.resolve(true);
};
/** Generate a iiif manifest for the given pid
*
*/
......@@ -302,6 +317,33 @@ app.insertImageMetadata = function(manifest, index) {
};
/** Write generated manifest file to a file as a cache
*
*/
app.saveToFile = function(manifest) {
// cf. https://stackoverflow.com/a/35211152
return new Promise(function(resolve, reject) {
fs.access(DATA_DIR + '/' + DATA_SUBDIR_MANIFESTS, fs.constants.W_OK, function (err1) {
if (err1) {
console.log("cannot access manifest data dir " + DATA_DIR + '/' + DATA_SUBDIR_MANIFESTS, err1);
reject(err1);
}
else {
fs.writeFile(DATA_DIR + '/' + DATA_SUBDIR_MANIFESTS + '/' + manifest._pid + '.json', JSON.stringify(manifest), function (err2) {
if (err2) {
console.log("cannot write manifest to file " + DATA_DIR + '/' + DATA_SUBDIR_MANIFESTS + '/' + manifest._pid + '.json:', err1);
reject(err2);
}
else {
console.log("wrote manifest to file " + DATA_DIR + '/' + DATA_SUBDIR_MANIFESTS + '/' + manifest._pid + '.json');
resolve();
}
});
}
});
});
};
/* **
* START SERVER
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment