diff --git a/attach.sh b/attach.sh index 8642c1c..1df6263 100755 --- a/attach.sh +++ b/attach.sh @@ -1,4 +1,4 @@ #! /bin/sh #Attach a shell to nginx -NGINX_PS=$(docker ps -f name=ocflmount -q) +NGINX_PS=$(docker ps -f name=ocfl-nginx_nginx-ocfl -q) docker exec -it $NGINX_PS /bin/bash -c 'exec "${SHELL:-sh}"' diff --git a/conf.d/ocfl.conf b/conf.d/ocfl.conf index c0c65e9..a75f66a 100644 --- a/conf.d/ocfl.conf +++ b/conf.d/ocfl.conf @@ -7,20 +7,15 @@ server { server_name 0.0.0.0; set $ocfl_root /etc/share/nginx/html; - set $ocfl_autoindex CATALOG.html; + set $ocfl_autoindex ro-crate-preview.html; set $ocfl_repo_index index.json; - location /staging/ { - set $ocfl_repo staging_ocfl; - js_content ocfl; + location /rocrate/ { + set $ocfl_repo rocrate_ocfl; + js_content ocfl; } - location /public/ { - set $ocfl_repo public_ocfl; - js_content ocfl; - } - - location /staging_ocfl/ { + location /rocrate_ocfl/ { root $ocfl_root; } @@ -28,8 +23,13 @@ server { root $ocfl_root; } + location /solr/ { + proxy_pass http://solr:8983; + } - + location /solr_bridge/ { + js_content solr_bridge; + } } diff --git a/docker-compose.yml b/docker-compose.yml index 168c161..b0932bf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,5 +26,6 @@ services: volumes: - "./test_solr/:/opt/solr/server/solr/ocflcore" command: solr-precreate ocflcore - + logging: + driver: none diff --git a/js/ocfl.js b/js/ocfl.js index 0614f5d..385978c 100644 --- a/js/ocfl.js +++ b/js/ocfl.js @@ -15,12 +15,15 @@ var MAX_INDEX_LENGTH = 120; // ie the REPO_NAME can't have '/' in it + function ocfl(req) { var ocfl_repo = req.variables.ocfl_repo; var ocfl_root = req.variables.ocfl_root; var index_file = req.variables.ocfl_autoindex; + req.error("Incoming uri: '" + req.uri + "'"); + var parts = req.uri.split('/'); var repo = parts[1]; var oidv = parts[2]; @@ -30,71 +33,68 @@ function ocfl(req) { req.error("Couldn't find match for " + req.uri); req.return(440, "Resource not found"); return; - } else if( !oidv ) { - repository_index(req, repo); - } else { - - var pattern = new RegExp('^([^/\\.]+)(\\.v\\d+)?$'); - var match = oidv.match(pattern); - if( !match ) { - req.error("Couldn't match oid " + oidv); - req.return(440, "Resource not found"); - return - } - var oid = match[1]; - var v = match[2]; - var object = pairtree(oid); - var opath = [ ocfl_repo ].concat(object).join('/'); - - if( v ) { - v = v.substr(1); - } - - if( !content ) { - content = index_file; - } + } - req.error("oid: " + oid + "; v: " + v + "; object: " + opath); - - var vpath = version(req, ocfl_root + '/' + opath, content, v); - if( vpath ) { - var newroute = '/' + opath + '/' + vpath; - req.warn("Remapped " + oid + " to " + newroute); - req.internalRedirect(newroute); + load_index(req, ( index ) => { + if( !oidv ) { + send_html(req, render_index(index, repo)); } else { - req.error("Version not found"); - req.return(440, "Version not found"); + + var pattern = new RegExp('^([^\\.]+)(\\.v\\d+)?$'); + var match = oidv.match(pattern); + if( !match ) { + req.error("Couldn't match oid " + oidv); + req.return(440, "Resource not found"); + return + } + var oid = decodeURIComponent(match[1]); + var v = match[2]; + + var indexo = index_lookup(index, oid); + + if( indexo ) { + var opath = [ ocfl_repo ].concat(indexo['path']).join('/'); + if( v ) { + v = v.substr(1); + } + + if( !content ) { + content = index_file; + } + + var vpath = version(req, ocfl_root + '/' + opath, content, v); + if( vpath ) { + var newroute = '/' + opath + '/' + vpath; + req.warn("Remapped " + oid + " to " + newroute); + req.internalRedirect(newroute); + } else { + req.error("Version not found"); + req.return(440, "Version not found"); + } + } else { + req.error("OID " + oid + " not found in index"); + req.return(440, "Object not found"); + } } - } + }); } -// see if this can return json or html - -function repository_index(req, url_path) { +// doing this with a callback so that it's compatible with +// what I'll have to do for the solr version even though it's +// readFileSync +function load_index(req, callback) { var ocfl_root = req.variables.ocfl_root; var ocfl_repo = req.variables.ocfl_repo; var repo_index = req.variables.ocfl_repo_index; var index_file = ocfl_root + '/' + ocfl_repo + '/' + repo_index; - // this could be a subrequest for an index.json which is - // generated from a database - try { var js = fs.readFileSync(index_file); var index = JSON.parse(js); - - var html = "
Hello, Docker
"; - - index.forEach((e) => { - var entry = index_map(e); - var url = '/' + url_path + '/' + entry[0] + '/'; - html += '' - }); - - html += ''; - send_html(req, html); + callback(index); + return; } catch(e) { req.error("Error reading " + index_file); req.error(e); @@ -103,15 +103,40 @@ function repository_index(req, url_path) { } -// indexmap takes an index entry and returns an id and a chunk of HTML -// to be rendered as the index +function index_lookup(index, id) { + var match = index.filter(i => i['uri_id'] === id); + if( match ) { + return match[0]; + } else { + return null; + } +} + +function render_index(index, url_path) { + + var html = "ocfl-nginx bridge v1.0.0
\n"; + + index.forEach((e) => { + var entry = index_map(e); + var url = '/' + url_path + '/' + entry[0] + '/'; + html += '\n' + }); + + html += '\n'; + + return html; +} + + +// indexmap takes an index entry and returns a URL path (based on +// the id) and HTML for the link function index_map(entry) { - var html = entry['name'] + ': ' + entry['description']; - if( html.length > MAX_INDEX_LENGTH ) { - html = html.slice(0, MAX_INDEX_LENGTH) + '...'; - } - return [ entry['@id'], html ]; + var html = entry['name']; + // if( html.length > MAX_INDEX_LENGTH ) { + // html = html.slice(0, MAX_INDEX_LENGTH) + '...'; + // } + return [ entry['uri_id'], html ]; } function send_html(req, html) { @@ -125,6 +150,7 @@ function send_html(req, html) { + function version(req, object, payload, version) { var inv = load_inventory(req, object); if( ! inv ) { @@ -150,7 +176,11 @@ function version(req, object, payload, version) { function load_inventory(req, object) { - var ifile = object + 'inventory.json'; + var ifile = object; + if( ifile.slice(-1) !== '/' ) { + ifile += '/'; + } + ifile += 'inventory.json'; try { req.log("Trying to read " + ifile); var contents = fs.readFileSync(ifile); @@ -216,3 +246,17 @@ function pairtree(id, separator) { + +// testbed for the solr handler + +var SOLR = "/solr/ocflcore/select"; + +// basic - just passes through + +function solr_bridge(req) { + req.warn("Routing subrequest to solr proxy via " + SOLR); + req.subrequest(SOLR, req.variables.args, (res) => { + req.return(res.status, res.responseBody); + }); +} +