Skip to content

Commit

Permalink
Merge pull request #2 from uci-soe/docker
Browse files Browse the repository at this point in the history
Docker
  • Loading branch information
rhettl committed Jun 29, 2016
2 parents eee959c + 234fdeb commit b2b76bf
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 13 deletions.
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/coverage
/node_modules
/test
/tmp

.dockerid
.editorconfig
.gitattributes
.gitignore
.travis.yml
docker-rebuild
Dockerfile
LICENSE
README.md
/tmp*
/scratch.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
config.json
pm2.json
/tmp
.dockerid
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js
node_js:
- v4
- v5
- v6
env:
- CXX=g++-4.8
before_install:
Expand Down
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM ubuntu:14.04

MAINTAINER Rhett Lowe <rng2ml@gmail.com>

ENV DEBIAN_FRONTEND noninteractive

RUN sed 's/main$/main universe/' -i /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y curl build-essential wget

# Download and install wkhtmltopdf
RUN apt-get install -y xorg libssl-dev libxrender-dev gdebi xvfb libicu52 xfonts-75dpi
RUN wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
RUN tar -xvJf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz -C /usr/local/
ENV PATH /usr/local/wkhtmltox/bin:$PATH

# Install Nodejs
RUN curl -sL https://deb.nodesource.com/setup_4.x | bash
RUN apt-get install -y nodejs
RUN npm install --global npm

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
run npm install

copy . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]
34 changes: 24 additions & 10 deletions bin/www.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,41 @@ try {

var serviceName = process.env.NAME || pkg.name;
var toPdf = pdfFac({
command: process.env.CMD_PATH || '~/bin/wkhtmltopdf'
command: process.env.CMD_PATH || 'wkhtmltopdf'
});

var server = restify.createServer({
name: serviceName,
version: '1.0.0'
});

server.use(restify.queryParser());
server.use(restify.gzipResponse());

server.post('/', restify.bodyParser(), function (req, res, next) {
var html = req.body && req.body.html;

server.post('/', restify.bodyParser(), function (req, res) {
var opts = req.body || req.params;
var html = opts.html;
if (html) {
Object.getOwnPropertyNames(req.query).forEach(i => {
let t = req.query[i].toLowerCase();
if (t === 'true' || t === 'false') {
req.query[i] = t === 'true';
}
});

if (opts.html) {
delete opts.html;
}
toPdf(html, req.query, (err, out) => {
if (err) {
next(err);
} else {

res.setHeader('content-type', 'application/pdf');
res.send(out);
}
});

toPdf.stream(html, opts)
.pipe(res)
;
} else {
next(new Error('No HTML body given.'));
}
});


Expand Down
6 changes: 6 additions & 0 deletions docker-clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

IDS=$(docker ps --all --no-trunc | grep Exited | awk '{print $1}')
for ID in ${IDS}; do
docker rm ${ID}
done
12 changes: 12 additions & 0 deletions docker-kill
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

ID_FILE='./.dockerid'
ID=''

if [ -s ${ID_FILE} ]; then
ID=`cat ${ID_FILE}`
if [[ $(docker ps --no-trunc --quiet | grep ${ID}) != '' ]]; then
docker stop ${ID}
docker rm ${ID}
fi
fi
26 changes: 26 additions & 0 deletions docker-rebuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

USER='rhettl'
NAME='toPdf'
TAG='node-wkhtmltopdf'
ID_FILE='./.dockerid'

PORT=8080
if [[ ${1} != '' ]]; then
if [[ ${1} =~ ^[0-9]+$ ]] && [ ${1} -gt 0 ]; then
PORT=${1}
else
(>&2 echo "port is not a number > 0")
echo
echo "USAGE: docker-rebuild [port]"
echo "port defaults to 8080"
exit 1
fi
fi

./docker-kill

docker build -t "${USER}/${TAG}" .
ID=`docker run -d -p "${PORT}":8080 -e PORT=8080 --name "${NAME}" "${USER}/${TAG}"`

echo ${ID} > ${ID_FILE}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@
"deep-extend": "^0.4.1",
"if-err": "^0.1.1",
"restify": "^4.0.3",
"wkhtmltopdf": "^0.1.5"
"wkhtmltopdf": "^0.3.1"
},
"scripts": {
"start": "node ./bin/www.js",
"prepublish": "gulp prepublish",
"test": "gulp"
"test": "gulp",
"docker-rebuild": "./docker-rebuild",
"docker-clean": "./docker-clean",
"docker-kil": "./docker-kill"
},
"eslintConfig": {
"extends": "rhett",
Expand All @@ -57,4 +60,4 @@
}
},
"repository": "uci-soe/to-pdf"
}
}

0 comments on commit b2b76bf

Please sign in to comment.