Skip to content
This repository has been archived by the owner on Oct 31, 2018. It is now read-only.

Commit

Permalink
Back to nginx, work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
warpr committed Mar 15, 2015
1 parent b83bfac commit 027d711
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 28 deletions.
42 changes: 25 additions & 17 deletions docker/Dockerfile
@@ -1,17 +1,33 @@
# -*- mode: shell-script -*-

# FROM ubuntu:14.04
FROM phusion/baseimage:0.9.16
# FROM phusion/passenger-full:0.9.15

EXPOSE 80

CMD ["/sbin/my_init"]

RUN apt-get -y update && apt-get -y upgrade
RUN apt-get -y update && apt-get -y install add-apt-key

# nginx repository and GPG key
RUN add-apt-key --keyserver pool.sks-keyservers.net 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62

RUN apt-get -y install apache2 build-essential curl git libkyotocabinet-dev libraptor2-dev \
python-virtualenv python-pip php5-cli
COPY nginx/sources.list /etc/apt/sources.list.d/nginx.list

RUN apt-get -y update && apt-get -y upgrade
RUN apt-get -y install build-essential curl git libkyotocabinet-dev libraptor2-dev \
nginx python-virtualenv python-pip php5-cli

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /root
RUN git clone https://github.com/rdfhdt/hdt-cpp

RUN cd /root/hdt-cpp/libcds-v1.0.12 \
&& make \
&& cd /root/hdt-cpp/hdt-lib \
&& make tools

# gpg keys listed at https://github.com/iojs/io.js
RUN gpg --keyserver pool.sks-keyservers.net --recv-keys 9554F04D7259F04124DE6B476D5A82AC7E37093B DD8F2338BAE7501E3DD5AC78C273792F7D83545D

Expand All @@ -24,14 +40,6 @@ RUN curl -SLO "https://iojs.org/dist/v$IOJS_VERSION/iojs-v$IOJS_VERSION-linux-x6
&& tar -xzf "iojs-v$IOJS_VERSION-linux-x64.tar.gz" -C /usr/local --strip-components=1 \
&& rm "iojs-v$IOJS_VERSION-linux-x64.tar.gz" SHASUMS256.txt.asc

WORKDIR /root
RUN git clone https://github.com/rdfhdt/hdt-cpp

RUN cd /root/hdt-cpp/libcds-v1.0.12 \
&& make \
&& cd /root/hdt-cpp/hdt-lib \
&& make tools

WORKDIR /srv
RUN git clone https://github.com/warpr/licensedb

Expand All @@ -42,10 +50,10 @@ RUN bin/bootstrap.sh
# the first run.
RUN make && make

RUN a2enmod rewrite
RUN apt-get -y update && apt-get -y install screen

COPY service/apache2.sh /etc/service/apache2/run
COPY apache2/licensedb.conf /etc/apache2/sites-available/licensedb.conf
RUN a2dissite 000-default \
&& a2ensite licensedb
RUN printf "\ndaemon off;\n" >> /etc/nginx/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/licensedb.org.conf /etc/nginx/conf.d/licensedb.org.conf
COPY service/nginx.sh /etc/service/nginx/run

33 changes: 33 additions & 0 deletions docker/nginx/licensedb.org.conf
@@ -0,0 +1,33 @@

upstream licensedb_nodejs {
server 127.0.0.1:26553 weight=1 max_fails=0 fail_timeout=10s;
}

server {
# listen [::]:80;
listen 80;
# listen [::]:80 ipv6only=on default_server;
# listen *:;
server_name licensedb.org;

root /srv/licensedb/www;
index index.html;

location / {
add_header Strict-Transport-Security max-age=2419200;

error_page 404 = @content_negotiation;
rewrite ^/ns/$ /ns permanent;
rewrite ^/license/$ /license permanent;
rewrite ^/id/(.*)/$ /id/$1 permanent;
}

location /dl {
autoindex on;
}

location @content_negotiation {
proxy_pass http://licensedb_nodejs;
}
}

3 changes: 3 additions & 0 deletions docker/nginx/sources.list
@@ -0,0 +1,3 @@

deb http://nginx.org/packages/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/ubuntu/ trusty nginx
2 changes: 1 addition & 1 deletion docker/run
Expand Up @@ -3,6 +3,6 @@
OPTIONS=""
OPTIONS="$OPTIONS --rm=true"
OPTIONS="$OPTIONS --interactive --tty"
OPTIONS="$OPTIONS --publish=80:80 --publish=443:443"
OPTIONS="$OPTIONS --publish=80:80"

docker run $OPTIONS "warp/licensedb" "$@"
8 changes: 0 additions & 8 deletions docker/service/apache2.sh

This file was deleted.

3 changes: 3 additions & 0 deletions docker/service/nginx.sh
@@ -0,0 +1,3 @@
#!/bin/sh

exec /usr/sbin/nginx
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -4,9 +4,10 @@
"clone": "0.1.x",
"jsonld": "^0.3.22",
"lazy": "1.0.x",
"negotiator": "^0.5.1",
"request": "2.12.x",
"turtle-to-jsonld": "warpr/turtle-to-jsonld",
"underscore": "1.4.x",
"underscore": "^1.4.4",
"underscore.string": "2.3.x"
},
"description": "LicenseDB, the License Database project.",
Expand Down
30 changes: 30 additions & 0 deletions src/server/app.js
@@ -0,0 +1,30 @@
/*
app.js -- this file is part of the licensedb.org server.
copyright 2012,2013 Kuno Woudt
Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
*/

var http = require ('http');
var main = require ('./main');

var base_url = process.argv[2] ? process.argv[2] : "";

exports.app = function (port, hostname) {
console.log ('Server running at http://127.0.0.1:' + port.toString () + '/');
http.createServer (main.listener (base_url)).listen (port, hostname);
}

exports.app(26553);
31 changes: 31 additions & 0 deletions src/server/main.js
@@ -0,0 +1,31 @@
/*
main.js -- this file is part of the licensedb.org server.
copyright 2012,2013 Kuno Woudt
Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
*/

var http = require ('http');
var negotiate = require ('./negotiate');

exports.listener = function(base_url) {
negotiate.init(base_url);
return function (request, response) {
negotiate.content(request, response);
}
}

exports.server = http.createServer(
exports.listener('https://licensedb.org'));
81 changes: 81 additions & 0 deletions src/server/negotiate.js
@@ -0,0 +1,81 @@
/*
negotiate.js -- this file is part of the licensedb.org server.
copyright 2012,2013 Kuno Woudt
Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
*/

var _ = require ('underscore');
var fs = require ('fs');
var url = require ('url');
var Negotiator = require ('negotiator');

_.mixin(require('underscore.string').exports());

var MimeType = function (type, suffix) {
return { 'type': type, 'suffix': suffix };
};

var _types = _([
MimeType("text/html", "html"),
MimeType("text/turtle", "ttl"),
// MimeType("application/rdf+xml", "rdf"),
// MimeType("application/json", "json"),
MimeType("application/ld+json", "jsonld"),
]);

var typemap = _types.reduce (function (memo, val) {
memo[val.type] = val.suffix;
return memo;
}, {});

var available_types = _types.map (function (val) { return val.type; });
var base_location = {};

var error404 = function (response) {
response.writeHead (404, { 'Content-Type': 'text/plain' });
response.end('Not Found\n');
};

exports.init = function (base_url) {
base_location = url.parse (base_url);
};

exports.content = function (request, response) {
var negotiator = new Negotiator (request);
var content_type = negotiator.preferredMediaType (available_types);

if (!content_type) {
return error404(response);
}

var location = url.parse(request.url);

location.pathname = location.pathname + '.' + typemap[content_type];

console.log('looking for', location.pathname);

if (!fs.existsSync('www' + location.pathname)) {
return error404(response);
}

var location_str = url.format(_(location).defaults(base_location));

response.writeHead (303, {
'Content-Type': 'text/plain',
'Location': location_str
});
response.end('see: ' + location_str + '\n');
};
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -13,7 +13,7 @@
var assert = require ('assert');
var rp = require ('request-promise').defaults({ resolveWithFullResponse: true });

var baseIRI = 'http://www.frob.mobi/';
var baseIRI = 'http://10.237.180.17';

var testRequest = function (path, conneg, expected) {
var options = {
Expand Down

0 comments on commit 027d711

Please sign in to comment.