Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbam committed Aug 17, 2019
1 parent 97b12ce commit 75d136a
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 6 deletions.
16 changes: 10 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ $RECYCLE.BIN/
# Mac
.DS_Store

# java
*.class
*.jar
*.war
*.ear
# nodejs
node_modules/
package-lock.json
nohup.out

# maven
# python
__pycache__
.pytest_cache

# build
target/
build/
dist/

# eclipse
.settings/
Expand Down
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "deepracer-league",
"version": "0.0.1",
"description": "Node.js deepracer-league",
"main": "server.js",
"dependencies": {
"ejs": "^2.6.1",
"express": "^4.16.4",
"redis": "^2.8.0",
"request": "^2.88.0",
"socket.io": "^2.1.1"
},
"engine": {
"node": "*",
"npm": "*"
},
"scripts": {
"build": "npm install -s",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "http://github.com/nalbam/deepracer-league.git"
},
"author": "Jungyoul Yu <[email protected]>",
"bugs": {
"url": "http://github.com/nalbam/deepracer-league/issues"
},
"homepage": "http://github.com/nalbam/deepracer-league"
}
24 changes: 24 additions & 0 deletions report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ _message() {
fi
}

_json() {
_command "_json"

JSON=${SHELL_DIR}/cache/points.json

echo "{\"deepracer\":[" > ${JSON}

IDX=1
while read LINE; do
ARR=(${LINE})

if [ "${IDX}" != "1" ]; then
echo "," >> ${JSON}
fi

echo "{\"no\":${IDX},\"name\":\"${ARR[1]}\",\"point\":${ARR[0]}}" >> ${JSON}

IDX=$(( ${IDX} + 1 ))
done < ${SHELL_DIR}/build/points.log

echo "]}" >> ${JSON}
}

_git_push() {
_command "_git_push"

Expand Down Expand Up @@ -268,6 +291,7 @@ __main__() {

_build
_message
_json

if [ ! -z ${CHANGED} ]; then
_git_push
Expand Down
151 changes: 151 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
'use strict';

const TRACER = process.env.TRACER || 'none';

// datadog tracer
if (TRACER === 'all' || TRACER === 'datadog') {
require('dd-trace').init({
hostname: process.env.DD_AGENT_HOST,
port: process.env.DD_AGENT_PORT,
analytics: true
})
}

// newrelic tracer
if (TRACER === 'all' || TRACER === 'newrelic') {
require('newrelic');
}

const os = require('os'),
cors = require('cors'),
express = require('express'),
moment = require('moment-timezone'),
redis = require('redis');

// express
const app = express();
app.set('view engine', 'ejs');

app.use(cors());
app.use(express.json());
app.use('/favicon.ico', express.static('views/favicon.ico'));
app.use('/counter.js', express.static('views/counter.js'));

// env
const PORT = process.env.PORT || 3000;
const PROFILE = process.env.PROFILE || 'default';
const REDIS_URL = process.env.REDIS_URL || `redis://sample-node-redis:6379`;
const MESSAGE = process.env.MESSAGE || PROFILE;
const IMAGE_TAG = process.env.IMAGE_TAG || 'v0.0.0';

// redis
const retry_strategy = function(options) {
if (options.error && (options.error.code === 'ECONNREFUSED' || options.error.code === 'NR_CLOSED')) {
// Try reconnecting after 5 seconds
console.error('The server refused the connection. Retrying connection...');
return 5000;
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands with an individual error
return new Error('Retry time exhausted');
}
if (options.attempt > 50) {
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.min(options.attempt * 100, 5000);
};
const client = redis.createClient(REDIS_URL, {retry_strategy: retry_strategy});
client.on('connect', () => {
console.log(`connected to redis: ${REDIS_URL}`);
});
client.on('error', err => {
console.error(`${err}`);
});

app.get('/', function (req, res) {
// console.log(`${req.method} ${req.path}`);
let host = os.hostname();
let date = moment().tz('Asia/Seoul').format();
res.render('index.ejs', {host: host, date: date, message: MESSAGE, version: IMAGE_TAG});
});

app.get('/stress', function (req, res) {
// console.log(`${req.method} ${req.path}`);
let sum = 0;
for (let i = 0; i < 1000000; i++) {
sum += Math.sqrt(i);
}
return res.status(200).json({sum : sum});
});

app.get('/cache/:name', function (req, res) {
// console.log(`${req.method} ${req.path}`);
const name = req.params.name;
return client.get(`cache:${name}`, (err, result) => {
if (err) {
console.error(`${err}`);
return res.status(500).json({status:500, message:err.message,});
}
return res.status(200).json(result == null ? {} : JSON.parse(result));
});
});

app.post('/cache/:name', function (req, res) {
// console.log(`${req.method} ${req.path}`);
const name = req.params.name;
const json = JSON.stringify(req.body);
//console.log(`req.body: ${json}`);
return client.set(`cache:${name}`, json, (err, result) => {
if (err) {
console.error(`${err}`);
return res.status(500).json({status:500, message:err.message,});
}
return res.status(200).json(result == null ? {} : result);
});
});

app.get('/counter/:name', function (req, res) {
// console.log(`${req.method} ${req.path}`);
const name = req.params.name;
return client.get(`counter:${name}`, (err, result) => {
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
if (err) {
console.error(`${err}`);
return res.status(500).send(err.message);
}
return res.send(result == null ? '0' : result.toString());
});
});

app.post('/counter/:name', function (req, res) {
// console.log(`${req.method} ${req.path}`);
const name = req.params.name;
return client.incr(`counter:${name}`, (err, result) => {
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
if (err) {
console.error(`${err}`);
return res.status(500).send(err.message);
}
return res.send(result == null ? '0' : result.toString());
});
});

app.delete('/counter/:name', function (req, res) {
// console.log(`${req.method} ${req.path}`);
const name = req.params.name;
return client.decr(`counter:${name}`, (err, result) => {
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
if (err) {
console.error(`${err}`);
return res.status(500).send(err.message);
}
return res.send(result == null ? '0' : result.toString());
});
});

app.listen(PORT, function () {
console.log(`[${PROFILE}] Listening on port ${PORT}!`);
console.log(`connecting to redis: ${REDIS_URL}`);
});
Binary file added views/favicon.ico
Binary file not shown.
19 changes: 19 additions & 0 deletions views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width,minimal-ui" />
<title>AWS DeepRacer League</title>
</head>

<body>
<script src="lib/aws-sdk.min.js"></script>
<script src="lib/mqtt.min.js"></script>
<script src="run.js"></script>
<script>
run('main')
</script>
</body>

</html>

0 comments on commit 75d136a

Please sign in to comment.