Skip to content

Commit

Permalink
Pagination on solr index works, index has a stylesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
spikelynch committed Aug 28, 2019
1 parent 002b1ec commit 50cbc89
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx
COPY conf.d/ocfl.conf /etc/nginx/conf.d
RUN mkdir -p /etc/nginx/js
COPY js/ocfl.js /etc/nginx/js
COPY conf.d/ocfl.conf /etc/nginx/conf.d
RUN mkdir -p /etc/share/nginx/html/assets
COPY assets/* /etc/share/nginx/html/assets
51 changes: 51 additions & 0 deletions assets/ocfl.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* ocfl bridge */

body {
background-color: #eee;
padding: 0.5em;
}

#header {
font-size: 80%;
font-weight: bold;
background-color: #ccc;
padding: 1em;
height: 3em;
}

#title {
float:left;
margin: 1em;
}

#nav {
float: right;
margin: 1em;
}


#body {
background-color: #fff;
padding: 2em;
}

div.item {
padding: 0.25em;
}

div.item > a {
text-decoration: none;
}

div.item:nth-child(odd) {
background-color: #ddd;
}


#footer {
font-size: 80%;
font-weight: bold;
background-color: #ccc;
padding: 1em;
}

6 changes: 5 additions & 1 deletion conf.d/ocfl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ server {
listen 8080;
server_name 0.0.0.0;

set $ocfl_root /etc/share/nginx/html;
set $ocfl_root /etc/share/nginx/html/ocfl;
set $ocfl_autoindex ro-crate-preview.html;
set $ocfl_repo_index index.json;

location /assets/ {
root /etc/share/nginx/html;
}

location /repo/ {
set $ocfl_repo repo_ocfl;
set $ocfl_solr /solr/ocflcore;
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
networks:
- main
volumes:
- "./test_repos/:/etc/share/nginx/html"
- "./test_repos/:/etc/share/nginx/html/ocfl"

solr:
image: solr
Expand Down
71 changes: 61 additions & 10 deletions js/ocfl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var fs = require('fs');
// ie the REPO_NAME can't have '/' in it


var PAGE = 20;

// parse the URI and either serve the index or an ocfl_object

Expand All @@ -33,23 +34,37 @@ function ocfl(req) {
return;
}

req.error("in ocfl: repo = " + repo);

if( oidv ) {
ocfl_object(req, repo, oidv, content)
} else {

// get pagination from req.variables.args
var query = "fl=name,path,uri_id&q=record_type_s:Dataset"
var start = parse_pagination(req.variables.args);
var query = "q=record_type_s:Dataset&rows=" + String(PAGE) + "&start=" + start + "&fl=name,path,uri_id&"
req.error("solr query " + query);
req.subrequest(ocfl_solr + '/select', { args: query }, ( res ) => {
var solrJson = JSON.parse(res.responseBody);
var docs = solrJson['response']['docs'];
var start = solrJson['response']['start'];
send_html(req, render_index(repo, start, docs));
var numFound = solrJson['response']['numFound'];
send_html(req, render_index(repo, numFound, start, PAGE, docs));
});
}
}


function parse_pagination(args) {
if( ! args ) {
return '0';
}
var start_re = new RegExp('^start=(\\d+)');
var match = args.match(start_re);
if( match ) {
return match[1];
}
return '0';
}


// parse a versioned url_id, look it up in solr to find the path
// and then return the versioned ocfl content

Expand Down Expand Up @@ -111,23 +126,59 @@ function ocfl_object(req, repo, oidv, content) {
// with the properties url_id and name (name is an array)


function render_index(repo, start, links) {
function render_index(repo, numFound, start, page, links) {

var html = "<html><body><p>ocfl-nginx bridge v1.0.3</p>\n";
html += "<p>Start: " + start + "</p>";
var html = '<html><head><link rel="stylesheet" type="text/css" href="/assets/ocfl.css"></head>\n' +
'<body>\n' +
'<div id="header">\n' +
'<div id="title">OCFL Repository: ' + repo + '</div>\n' +
'<div id="nav">' + nav_links(repo, numFound, start, page) + '</div>\n' +
'</div>' +
'<div id="body">\n';

links.forEach((l) => {
var url = '/' + repo + '/' + l['uri_id'] + '/';
html += '<p><a href="' + url + '">' + l['name'][0] + '</a></p>\n'
html += '<div class="item"><a href="' + url + '">' + l['name'][0] + '</a></div>\n'
});

html += '</body>\n';
html += '</div>\n' +
'<div id="footer"><a href="https://github.com/UTS-eResearch/ocfl-nginx">ocfl-nginx bridge v1.0.3</a></div>\n' +
'</body>\n</html>\n';

return html;

}


function nav_links(repo, numFound, start, page) {
var html = '';
var url = '/' + repo + '/'
var last = start + page - 1;
var next = undefined;
if( last > numFound - 1 ) {
last = numFound - 1;
} else {
next = start + page;
}
if( start > 0 ) {
var prev = start - page;
if( prev < 0 ) {
prev = 0;
}
if( prev > 0 ) {
html += '<a href="' + url + '?start=' + String(prev) + '">&lt;--</a> ';
} else {
html += '<a href="' + url + '">&lt;--</a> ';
}
}
html += String(start + 1) + '-' + String(last + 1) + ' of ' + String(numFound);
if( next ) {
html += ' <a href="' + url + '?start=' + String(next) + '">--&gt;</a>'
}
return html;
}


function send_html(req, html) {
req.status = 200;
req.headersOut['Content-Type'] = "text/html; charset=utf-8";
Expand Down

0 comments on commit 50cbc89

Please sign in to comment.