Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs upgrade #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:12

RUN apt-get update && apt-get install -y libfreetype6-dev ghostscript imagemagick

WORKDIR /code

COPY serverless.yml /code/serverless.yml
COPY package.json /code/package.json
COPY yarn.lock /code/yarn.lock

RUN npm -g install serverless

RUN yarn install

COPY . /code

EXPOSE 4500

CMD sls offline start --host 0.0.0.0 --port 4500 --dontPrintOutput
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
run:
sls offline start --port 4500 --dontPrintOutput
sls offline start --httpPort 4500 --dontPrintOutput

up:
docker-compose up

test:
perl test.pl

deps:
yarn install
npm -g install serverless
sls plugin install -n serverless-pseudo-parameters
sls plugin install -n serverless-webpack
sls plugin install -n serverless-offline

deploy-dev:
sls deploy --stage dev

deploy-prod:
sls deploy --stage prod

.PHONY: run test deps
cleanup-test:
rm -f test-*.json-out.json
rm -f test-*payload.png

.PHONY: run test deps cleanup-test
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

National Voting Registration Imaging Service

### Setup

Local setup:

```
% make deps
% make run # for local install
% make up # for docker install
```

### Deployment

1. Set up Serverless framework. https://serverless.com/framework/docs/providers/aws/guide/quick-start/
2. Verify that your ~/.aws/credentials profile is named "nvris-dev" for your AWS IAM creds.
3. From within project folder run `sls deploy --stage dev` (replace stage name as necessary)
1. Set up Serverless framework. https://serverless.com/framework/docs/providers/aws/guide/quick-start/
1. Verify that your ~/.aws/credentials profile is named "nvris-dev" for your AWS IAM creds.
1. From within project folder run `sls deploy --stage dev` (replace stage name as necessary)

### Usage

Expand All @@ -16,11 +26,25 @@ curl -XPOST -H 'Content-Type: application/json' --data @exampleFlexPayload.json

## On OSX

Ensure you are using the latest freetype and imagemagick.
Ensure you are using the latest freetype and imagemagick.

```
brew update
brew upgrade freetype
brew upgrade imagemagick
```


## Local Docker setup

NodeJS and its dependancies can be a bit of a pain on OSX. To help reduce
future development friction, we setup a Docker environment.

To get nvris running locally you can just run:

```shell
$ docker-compose up
```

Which will run nvris and expose it on `http://localhost:4500/`.

9 changes: 9 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'

services:

nvris:
build: .
ports:
- "4500:4500"
stop_signal: SIGKILL
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-loader": "^7.1.4",
"babel-plugin-source-map-support": "^2.0.1",
"babel-plugin-source-map-support": "^2.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-3": "^6.24.1",
Expand All @@ -17,13 +17,14 @@
"eslint-config-prettier": "^2.9.0",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-jsx-a11y": "^6.1.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-react": "^7.10.0",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-standard": "^3.1.0",
"jest": "^23.1.0",
"serverless-offline": "^3.25.4",
"serverless-offline": "^3.33.0",
"serverless-pseudo-parameters": "^2.5.0",
"serverless-webpack": "^5.1.5",
"supertest": "^3.1.0",
"webpack": "^4.12.0",
Expand All @@ -34,14 +35,14 @@
"test": "jest"
},
"dependencies": {
"bluebird": "^3.5.1",
"body-parser": "^1.18.3",
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",
"dotenv": "^6.0.0",
"express": "^4.16.3",
"gm": "^1.23.1",
"gm-base64": "^1.1.1",
"lodash": "^4.17.10",
"newrelic": "^4.8.0",
"newrelic": "6.10.0",
"request": "^2.87.0",
"serverless-http": "^1.5.5",
"sls": "^0.1.0"
Expand Down
12 changes: 8 additions & 4 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
## express / test stuff from https://github.com/FrontendMasters/serverless-with-aws

service: NVRIS
app: nvris-dev

provider:
name: aws
runtime: nodejs8.10
runtime: nodejs12.x
profile: nvris-dev
region: us-east-1
timeout: 10 # optional, in seconds, default is 6
environment:
NEW_RELIC_NO_CONFIG_FILE: true
# iamRoleStatements:
# - Effect: Allow
# Action:
Expand All @@ -18,9 +21,7 @@ provider:
plugins:
- serverless-offline
- serverless-webpack

# serverless-webpack configuration
# Enable auto-packing of external modules
- serverless-pseudo-parameters
custom:
webpack:
webpackConfig: ./webpack.config.js
Expand All @@ -29,6 +30,9 @@ custom:
functions:
hello:
handler: src/index.handler
layers:
# version 2 is intentional. later versions are broken.
- arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:layer:image-magick:2
timeout: 10 # optional, in seconds, default is 6
events:
- http: ANY /
Expand Down
17 changes: 15 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ import KSAV2route from "./routes/KSAV2";
import FLEXroute from "./routes/FLEX";

const app = express();
const jsonParser = bodyParser.json({limit: '50mb', extended: true});
const jsonParser = bodyParser.json({ limit: '50mb', extended: true });

app.locals.newrelic = newrelic;

app.get("/", (req, res) => {
res.json({ message: "ok" });
const { exec } = require("child_process");
exec("convert -list font -debug configure", (error, stdout, stderr) => {
let err = error ? error.message : "";
console.log(`error:${err} stdout:${stdout} stderr:${stderr}`);
res.json({ message: "ok", error: err, stderr: stderr, stdout: stdout });
});
});
app.get("/im", (req, res) => {
const { exec } = require("child_process");
exec("convert -list configure", (error, stdout, stderr) => {
let err = error ? error.message : "";
console.log(`error:${err} stdout:${stdout} stderr:${stderr}`);
res.json({ message: "ok", error: err, stderr: stderr, stdout: stdout });
});
});

app.post("/vr/en", jsonParser, VRENroute);
Expand Down
9 changes: 5 additions & 4 deletions src/routes/FLEX.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import request from "request";

// import VRES from "../formDefinitions/VRES";
import fillForm from "../helpers/fillForm";
import renderImage from "../helpers/renderImage";
import base64RenderToFile from "../helpers/base64RenderToFile";
Expand All @@ -10,19 +9,21 @@ import overlayImagesThenRender from "../helpers/overlayImagesThenRender";
import imageToBase64 from "../helpers/imageToBase64";
import deleteTmpFiles from "../helpers/deleteTmpFiles";

// const path = require("path");

const Promise = require("bluebird");
// const fs = require('fs');
const gm = require("gm").subClass({ imageMagick: true });
require("gm-base64");
const fs = require("fs");

Promise.promisifyAll(gm.prototype);

export default async (req, res) => {
const { formData, formDefinition } = req.body;

const base = gm(request(formDefinition.baseImg));
const fontFile = '/tmp/helvetica.ttf';
request('https://ksvotes-v2.s3.amazonaws.com/helvetica.ttf').pipe(fs.createWriteStream(fontFile));

const base = gm(request(formDefinition.baseImg)).font(fontFile);
// fill form
const filledForm = await fillForm(base, formDefinition.fields, formData);
// write filled form to tmp
Expand Down
10 changes: 6 additions & 4 deletions src/routes/KSAV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import overlayImagesThenRender from "../helpers/overlayImagesThenRender";
import imageToBase64 from "../helpers/imageToBase64";
import deleteTmpFiles from "../helpers/deleteTmpFiles";

// const path = require("path");

const Promise = require("bluebird");
// const fs = require('fs');
const gm = require("gm").subClass({ imageMagick: true });
require("gm-base64");
const fs = require("fs");

Promise.promisifyAll(gm.prototype);

export default async (req, res) => {
const formPayload = req.body;

const base = gm(request("https://s3.amazonaws.com/ksvotes/AV1NVRIS.png"));
const fontFile = '/tmp/helvetica.ttf';
request('https://ksvotes-v2.s3.amazonaws.com/helvetica.ttf').pipe(fs.createWriteStream(fontFile));

const base = gm(request("https://s3.amazonaws.com/ksvotes/AV1NVRIS.png")).font(fontFile);
// fill form
const filledForm = await fillForm(base, KSAV1, formPayload);
// write filled form to tmp
Expand Down Expand Up @@ -55,7 +57,7 @@ export default async (req, res) => {

// now we are ready to send response
let imgB64 = await imageToBase64(imgPath, "png");
console.log(formPayload["uuid"]+": imgPath="+imgPath+" hasSignature:"+hasSignature+" img:"+imgB64.length+" bytes");
console.log(formPayload["uuid"] + ": imgPath=" + imgPath + " hasSignature:" + hasSignature + " img:" + imgB64.length + " bytes");
let response = {
KSAV1: "form Generated",
img: imgB64
Expand Down
11 changes: 6 additions & 5 deletions src/routes/KSAV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import overlayImagesThenRender from "../helpers/overlayImagesThenRender";
import imageToBase64 from "../helpers/imageToBase64";
import deleteTmpFiles from "../helpers/deleteTmpFiles";

// const path = require("path");

const Promise = require("bluebird");
// const fs = require('fs');
const gm = require("gm").subClass({ imageMagick: true });
require("gm-base64");
const fs = require("fs");

Promise.promisifyAll(gm.prototype);

export default async (req, res) => {
const formPayload = req.body;

const fontFile = '/tmp/helvetica.ttf';
request('https://ksvotes-v2.s3.amazonaws.com/helvetica.ttf').pipe(fs.createWriteStream(fontFile));

const base = gm(
request("https://s3.amazonaws.com/ksvotes/PERMVOTINGSTATUS.png")
);
).font(fontFile);
// fill form
const filledForm = await fillForm(base, KSAV2, formPayload);
// write filled form to tmp
Expand Down Expand Up @@ -57,7 +58,7 @@ export default async (req, res) => {

// now we are ready to send response
let imgB64 = await imageToBase64(imgPath, "png");
console.log(formPayload["uuid"]+": imgPath="+imgPath+" hasSignature:"+hasSignature+" img:"+imgB64.length+" bytes");
console.log(formPayload["uuid"] + ": imgPath=" + imgPath + " hasSignature:" + hasSignature + " img:" + imgB64.length + " bytes");
let response = {
KSAV2: "form Generated",
img: imgB64
Expand Down
11 changes: 6 additions & 5 deletions src/routes/VREN.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import overlayImagesThenRender from "../helpers/overlayImagesThenRender";
import imageToBase64 from "../helpers/imageToBase64";
import deleteTmpFiles from "../helpers/deleteTmpFiles";

// const path = require("path");

const Promise = require("bluebird");
// const fs = require('fs');
const gm = require("gm").subClass({ imageMagick: true });
require("gm-base64");
const fs = require("fs");

Promise.promisifyAll(gm.prototype);

export default async (req, res) => {
const formPayload = req.body;

const base = gm(request("https://s3.amazonaws.com/ksvotes/FEDVRENNVRIS.png"));
const fontFile = '/tmp/helvetica.ttf';
request('https://ksvotes-v2.s3.amazonaws.com/helvetica.ttf').pipe(fs.createWriteStream(fontFile));

const base = gm(request("https://s3.amazonaws.com/ksvotes/FEDVRENNVRIS.png")).font(fontFile);
// fill form
const filledForm = await fillForm(base, VREN, formPayload);
// write filled form to tmp
Expand Down Expand Up @@ -55,7 +56,7 @@ export default async (req, res) => {

// now we are ready to send response
let imgB64 = await imageToBase64(imgPath, "png");
console.log(formPayload["uuid"]+": imgPath="+imgPath+" hasSignature:"+hasSignature+" img:"+imgB64.length+" bytes");
console.log(formPayload["uuid"] + ": imgPath=" + imgPath + " hasSignature:" + hasSignature + " img:" + imgB64.length + " bytes");
let response = {
VR_ENG: "form Generated",
img: imgB64
Expand Down
11 changes: 6 additions & 5 deletions src/routes/VRES.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import overlayImagesThenRender from "../helpers/overlayImagesThenRender";
import imageToBase64 from "../helpers/imageToBase64";
import deleteTmpFiles from "../helpers/deleteTmpFiles";

// const path = require("path");

const Promise = require("bluebird");
// const fs = require('fs');
const gm = require("gm").subClass({ imageMagick: true });
require("gm-base64");
const fs = require("fs");

Promise.promisifyAll(gm.prototype);

export default async (req, res) => {
const formPayload = req.body;

const fontFile = '/tmp/helvetica.ttf';
request('https://ksvotes-v2.s3.amazonaws.com/helvetica.ttf').pipe(fs.createWriteStream(fontFile));

const base = gm(
request("https://s3.amazonaws.com/ksvotes/FEDVRENNVRIS_SP.png")
);
).font(fontFile);
// fill form
const filledForm = await fillForm(base, VRES, formPayload);
// write filled form to tmp
Expand Down Expand Up @@ -57,7 +58,7 @@ export default async (req, res) => {

// now we are ready to send response
let imgB64 = await imageToBase64(imgPath, "png");
console.log(formPayload["uuid"]+": imgPath="+imgPath+" hasSignature:"+hasSignature+" img:"+imgB64.length+" bytes");
console.log(formPayload["uuid"] + ": imgPath=" + imgPath + " hasSignature:" + hasSignature + " img:" + imgB64.length + " bytes");
let response = {
VR_ES: "form Generated",
img: imgB64
Expand Down
Loading