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

Related to issue #2 - AWS setup (SNS) + dotenv config updates + SNS integration #3

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AWS_REGION=
AWS_ACCOUNT=
SNS_TOPIC=
9 changes: 3 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
"parser": "babel-eslint",
"plugins": [
""
],
"extends": [
"plugin:react/recommended"
"airbnb"
],
"env": {
"browser": true,
"node": true,
"jest": true
},
"rules": {
Expand Down Expand Up @@ -56,4 +53,4 @@
}]
}
}


5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ typings/
# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

Expand Down Expand Up @@ -88,3 +85,5 @@ typings/
SOURCES/
SPECS/
*.service

.vscode
43 changes: 43 additions & 0 deletions bake-scripts/20-cosmos-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! /usr/bin/env python

import json
import pipes
import sys
import pwd
import grp
import os

NAME = "digital-paper-edit-api"
UID = pwd.getpwnam(NAME).pw_uid
GID = grp.getgrnam(NAME).gr_gid


def write_env(content, path):
f = open(path, "w")
for key, value in content.items():
f.write("%s=%s\n" % (key, pipes.quote(value)))
f.close()


def merge_two_dicts(x, y):
z = x.copy()
z.update(y)
return z


def main(argv):
out_dir = os.path.join("/usr/lib/", NAME)
path = os.path.join(out_dir, ".env.override")

raw = json.load(open(argv[1], "r"))
configs = merge_two_dicts(
raw["configuration"],
raw["secure_configuration"]
)
write_env(configs, path)
os.chown(path, UID, GID)
return 0


if __name__ == "__main__":
sys.exit(main(sys.argv))
16 changes: 0 additions & 16 deletions bake-scripts/create-service-config.sh

This file was deleted.

13 changes: 13 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const dotenv = require('dotenv');
const fs = require('fs');

const result = dotenv.config();

if (result.error) {
throw result.error;
}
const envConfig = dotenv.parse(fs.readFileSync('.env.override'));

envConfig.forEach((k) => {
process.env[k] = envConfig[k];
});
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
const express = require('express');
const bodyParser = require('body-parser');
const url = require('url');

const app = express();
const router = express.Router();

require('./config');

// https://stackoverflow.com/questions/24543847/req-body-empty-on-posts
// https://github.com/expressjs/body-parser#limit
// > Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb'.
app.use(bodyParser.json( { limit: '50MB' } ));

app.use(bodyParser.urlencoded({
extended: true
extended: true,
}));
const port = process.env.PORT || 8080;

app.use(function (req, res, next) {
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
Expand All @@ -26,15 +29,15 @@ app.use(function (req, res, next) {

// list all available rotues
app.get('/', (req, res) => {
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
const fullUrl = `${ req.protocol }://${ req.get('host') }${ req.originalUrl }`;
const results = [];
// https://stackoverflow.com/questions/14934452/how-to-get-all-registered-routes-in-express/14934933
app._router.stack.forEach(function(r) {
app._router.stack.forEach((r) => {
if (r.route && r.route.path) {
results.push({
path: r.route.path,
url: url.resolve(fullUrl, r.route.path),
methods: r.route.methods
methods: r.route.methods,
});
}
});
Expand All @@ -50,6 +53,10 @@ require('./routes/annotations.js')(app);
// TODO: status should probably not always return ok?
// eg if server is failing/crashed should return something else
// so that the instance can be terminated? or is not necessary to do this explicitly?
require('./routes/status.js')(app);

const status = require('./routes/status');

app.get('/status', status.healthCheck);
app.get('/queue', status.sendMessage);

app.listen(port, () => console.log(`App listening on port ${ port }!`));
33 changes: 33 additions & 0 deletions lib/messageBroker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const AWS = require('aws-sdk');

const getTopicArn = () => [
'arn:aws:sns',
process.env.AWS_REGION,
process.env.AWS_ACCOUNT,
process.env.SNS_TOPIC,
].join(':');

const publish = (msg) => {
const sns = new AWS.SNS({
apiVersion: '2010-03-31',
region: process.env.AWS_REGION,
});

const params = {
Message: msg,
TopicArn: getTopicArn(),
};

const promisePublish = new Promise((resolve, reject) => {
sns.publish(params, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});

return promisePublish;
};

module.exports = { publish };
Loading