Huh Jin-Ho

add server code

server_code @ bca4d754
1 -Subproject commit bca4d754b2f01d09d2c9eec4522a79ae6cb7537c
No preview for this file type
1 +node_modules
2 +./dff
...\ No newline at end of file ...\ No newline at end of file
1 +version: 0.0
2 +os: linux
3 +files:
4 + - source: /
5 + destination: /opt/food_test
6 +hooks:
7 + ApplicationStop:
8 + - location: deployment_scripts/stop.sh
9 + timeout: 180
10 + AfterInstall:
11 + - location: deployment_scripts/deploy.sh
12 + timeout: 180
13 + ApplicationStart:
14 + - location: deployment_scripts/start.sh
15 + timeout: 180
1 +#!/bin/bash
2 +cd /opt/food_test
3 +sudo npm install
4 +sudo node /opt/food_test/src/create_database.js
5 +sudo node /opt/food_test/src/insert_db.js
1 +#!/usr/bin/env bash
2 +#sudo pm2 stop node-app
3 +# actually start the server
4 +sudo pm2 start /opt/food_test/src/bin/www -i 0 --name "node-app"
1 +#!/usr/bin/env bash
2 +sudo pm2 stop node-app
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "food-waste",
3 + "version": "0.0.0",
4 + "private": true,
5 + "scripts": {
6 + "start": "node ./src/bin/www"
7 + },
8 + "dependencies": {
9 + "acron": "^1.0.5",
10 + "aws-sdk": "^2.587.0",
11 + "cookie-parser": "~1.4.4",
12 + "debug": "~2.6.9",
13 + "express": "~4.16.1",
14 + "http-errors": "~1.6.3",
15 + "jade": "~1.11.0",
16 + "morgan": "~1.9.1"
17 + }
18 +}
1 +var createError = require('http-errors');
2 +var express = require('express');
3 +var path = require('path');
4 +var cookieParser = require('cookie-parser');
5 +var logger = require('morgan');
6 +
7 +var indexRouter = require('./router.js');
8 +// var usersRouter = require('./routes/users');
9 +
10 +var app = express();
11 +
12 +// view engine setup
13 +app.set('views', path.join(__dirname, 'views'));
14 +app.set('view engine', 'jade');
15 +
16 +app.use(logger('dev'));
17 +app.use(express.json());
18 +app.use(express.urlencoded({ extended: false }));
19 +app.use(cookieParser());
20 +app.use(express.static(path.join(__dirname, 'public')));
21 +
22 +app.use('/', indexRouter);
23 +
24 +// catch 404 and forward to error handler
25 +app.use(function(req, res, next) {
26 + next(createError(200));
27 +});
28 +
29 +// error handler
30 +app.use(function(err, req, res, next) {
31 + // set locals, only providing error in development
32 + res.locals.message = err.message;
33 + res.locals.error = req.app.get('env') === 'development' ? err : {};
34 +
35 + // render the error page
36 + res.status(err.status || 500);
37 + res.render('error');
38 +});
39 +
40 +module.exports = app;
1 +#!/usr/bin/env node
2 +
3 +/**
4 + * Module dependencies.
5 + */
6 +
7 +var app = require('../app');
8 +var debug = require('debug')('food-waste:server');
9 +var http = require('http');
10 +
11 +/**
12 + * Get port from environment and store in Express.
13 + */
14 +
15 +var port = normalizePort(process.env.PORT || '3000');
16 +app.set('port', port);
17 +
18 +/**
19 + * Create HTTP server.
20 + */
21 +
22 +var server = http.createServer(app);
23 +
24 +/**
25 + * Listen on provided port, on all network interfaces.
26 + */
27 +
28 +server.listen(port);
29 +server.on('error', onError);
30 +server.on('listening', onListening);
31 +
32 +/**
33 + * Normalize a port into a number, string, or false.
34 + */
35 +
36 +function normalizePort(val) {
37 + var port = parseInt(val, 10);
38 +
39 + if (isNaN(port)) {
40 + // named pipe
41 + return val;
42 + }
43 +
44 + if (port >= 0) {
45 + // port number
46 + return port;
47 + }
48 +
49 + return false;
50 +}
51 +
52 +/**
53 + * Event listener for HTTP server "error" event.
54 + */
55 +
56 +function onError(error) {
57 + if (error.syscall !== 'listen') {
58 + throw error;
59 + }
60 +
61 + var bind = typeof port === 'string'
62 + ? 'Pipe ' + port
63 + : 'Port ' + port;
64 +
65 + // handle specific listen errors with friendly messages
66 + switch (error.code) {
67 + case 'EACCES':
68 + console.error(bind + ' requires elevated privileges');
69 + process.exit(1);
70 + break;
71 + case 'EADDRINUSE':
72 + console.error(bind + ' is already in use');
73 + process.exit(1);
74 + break;
75 + default:
76 + throw error;
77 + }
78 +}
79 +
80 +/**
81 + * Event listener for HTTP server "listening" event.
82 + */
83 +
84 +function onListening() {
85 + var addr = server.address();
86 + var bind = typeof addr === 'string'
87 + ? 'pipe ' + addr
88 + : 'port ' + addr.port;
89 + debug('Listening on ' + bind);
90 +}
1 +var AWS = require('aws-sdk');
2 +
3 +AWS.config.update({
4 + region: "ap-northeast-2",
5 +})
6 +
7 +var dynamodb = new AWS.DynamoDB();
8 +
9 +var pi_data =
10 +{
11 + TableName: 'pi_data',
12 + KeySchema: [
13 + { // Required
14 + AttributeName: 'ras_id',
15 + KeyType: 'HASH',
16 + },
17 + { // Optional
18 + AttributeName: 'save_date',
19 + KeyType: 'RANGE',
20 + }
21 + ],
22 + AttributeDefinitions: [
23 + {
24 + AttributeName: 'ras_id',
25 + AttributeType: 'S', // (S | N | B) for string, number, binary
26 + },
27 + {
28 + AttributeName: 'save_date',
29 + AttributeType: 'S', // (S | N | B) for string, number, binary
30 + }
31 + ],
32 + ProvisionedThroughput: { // required provisioned throughput for the table
33 + ReadCapacityUnits: 1,
34 + WriteCapacityUnits: 1,
35 + }
36 +};
37 +
38 +// var user_data =
39 +// {
40 +// TableName: 'user_data',
41 +// KeySchema: [
42 +// { // Required
43 +// AttributeName: 'user_id',
44 +// KeyType: 'HASH',
45 +// }
46 +// ],
47 +// AttributeDefinitions: [
48 +// {
49 +// AttributeName: 'user_id',
50 +// AttributeType: 'S', // (S | N | B) for string, number, binary
51 +// }
52 +// ],
53 +// ProvisionedThroughput: { // required provisioned throughput for the table
54 +// ReadCapacityUnits: 1,
55 +// WriteCapacityUnits: 1,
56 +// }
57 +// };
58 +
59 +
60 +dynamodb.createTable(pi_data, function(err, data) {
61 + if (err) {
62 + console.log(err); // an error occurred
63 + } else {
64 + console.log(data); // successful response
65 + }
66 +});
67 +
68 +dynamodb.createTable(user_data, function(err, data) {
69 + if (err) {
70 + console.log(err); // an error occurred
71 + } else {
72 + console.log(data); // successful response
73 + }
74 +});
1 +var AWS = require("aws-sdk");
2 +var fs = require('fs');
3 +
4 +AWS.config.update({
5 + region: "ap-northeast-2"
6 +});
7 +
8 +var docClient = new AWS.DynamoDB.DocumentClient();
9 +var pi_table = "pi_data";
10 +var user_table = "user_data";
11 +var dynamodb = new AWS.DynamoDB();
12 +
13 +function get_last_weight(ras_id,cb){
14 + params = {
15 + TableName : pi_table,
16 + ExpressionAttributeValues: {
17 + ":ras_id": {
18 + S: ras_id
19 + }
20 + },
21 + KeyConditionExpression : "ras_id = :ras_id",
22 + "ScanIndexForward":false,
23 + "Limit" : 1,
24 + }
25 + console.log(ras_id)
26 + dynamodb.query(params, function(err, data) {
27 + if (err) {
28 + console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
29 + } else {
30 + console.log(data.Items[0])
31 + var last_weight = data.Items[0].weight.N
32 + console.log("Last Weight : ",last_weight)
33 + cb(last_weight)
34 + }
35 + });
36 +}
37 +
38 +module.exports = {
39 + save_pi_data: function(pi_data,cb){
40 + console.log(pi_data)
41 + get_last_weight(pi_data.ras_id,function(last_weight){
42 + console.log(last_weight)
43 + var diff_weight = pi_data.weight - last_weight;
44 + if(diff_weight < 0) diff_weight = 0;
45 + var date = new Date(pi_data.date)
46 + date = date.getTime().toString()
47 + var params = {
48 + TableName : pi_table,
49 + Item : {
50 + "ras_id" : pi_data.ras_id,
51 + "save_date" : pi_data,
52 + "weight" : Number(pi_data.weight),
53 + "diff" : diff_weight
54 + }
55 + }
56 + docClient.put(params, function(err, data) {
57 + if (err) {
58 + console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
59 + cb(false)
60 + } else {
61 + console.log("Added item:", params);
62 + cb(true)
63 + }
64 + });
65 + })
66 + },
67 +
68 + save_app_data: function(app_data,cb){
69 + // var app_data = JSON.parse(app_data);
70 + var params = {
71 + TableName : user_table,
72 + Item : {
73 + "ras_id" : app_data.ras_id,
74 + "user_id" : app_data.user_id
75 + }
76 + }
77 + docClient.put(params, function(err, data) {
78 + if (err) {
79 + console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
80 + cb(false)
81 + } else {
82 + console.log("Added item:", params);
83 + cb(true)
84 + }
85 + });
86 + },
87 +
88 + get_user_data: function(ras_id,cb){
89 + // var params = {
90 + // TableName : user_table,
91 + // Key : {
92 + // "user_id" : user_id
93 + // }
94 + // }
95 + // console.log(user_id)
96 + // docClient.get(params, function(err, data) {
97 + // if (err) {
98 + // console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
99 + // } else {
100 + // console.log("GetItem succeeded:", data);
101 + // console.log(data.Item)
102 + // if(data.Item){
103 + params = {
104 + TableName : pi_table,
105 + ExpressionAttributeValues: {
106 + ":ras_id": { S: ras_id }
107 + },
108 + KeyConditionExpression : "ras_id = :ras_id"
109 + }
110 + dynamodb.query(params, function(err, data) {
111 + if (err) {
112 + console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
113 + } else {
114 + JSON.stringify(data, null, 2)
115 + // console.log(data.Items)
116 + cb(data.Items)
117 + }
118 + });
119 + // }
120 + // else{
121 + // cb("No User")
122 + // }
123 + // }
124 + // });
125 + },
126 +
127 + get_diff_data: function(user_info,cb){
128 + console.log(user_info)
129 + params = {
130 + TableName : pi_table,
131 + ExpressionAttributeValues: {
132 + ":ras_id": { S: user_info.ras_id},
133 + ":save_date" : {S : user_info.date}
134 + },
135 + KeyConditionExpression : "ras_id = :ras_id and save_date > :save_date",
136 + }
137 + dynamodb.query(params, function(err, data) {
138 + if (err) {
139 + console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
140 + } else {
141 + JSON.stringify(data, null, 2)
142 + console.log(data.Items)
143 + cb(data.Items)
144 + }
145 + });
146 + }
147 +}
...\ No newline at end of file ...\ No newline at end of file
1 +var AWS = require('aws-sdk');
2 +
3 +AWS.config.update({
4 + region: "ap-northeast-2"
5 +})
6 +
7 +var docClient = new AWS.DynamoDB.DocumentClient();
8 +
9 +function add_data(params){
10 + docClient.put(params, function(err, data) {
11 + if (err) {
12 + console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
13 + } else {
14 + console.log("Added item:", params);
15 + }
16 + });
17 +}
18 +async function save_random_data(epoch,start,end){
19 + var start_date = start;
20 + var start_weight = 0;
21 + for(var i =0; i<epoch; i++){
22 + console.log(start_date)
23 + var date = await start_date.getTime() + Math.random() * (end.getTime() - start_date.getTime());
24 + start_date = new Date(date)
25 + if(start_weight == 2000)
26 + start_weight = 0
27 + var weight = await Math.floor(Math.random() * (2000-start_weight))+start_weight+1;
28 + var diff = weight - start_weight
29 + if(start_weight == 0) diff = 0
30 + var params = {
31 + TableName : "pi_data",
32 + Item : {
33 + "ras_id" : "pi1",
34 + "save_date" : date.toString(),
35 + "weight" : weight,
36 + "diff" : diff
37 + }
38 + }
39 + await add_data(params);
40 + start_weight = await weight
41 +
42 + }
43 +}
44 +
45 +save_random_data(10, new Date("2019-12-10"), new Date())
...\ No newline at end of file ...\ No newline at end of file
1 +body {
2 + padding: 50px;
3 + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4 +}
5 +
6 +a {
7 + color: #00B7FF;
8 +}
1 +var express = require('express');
2 +var router = express.Router();
3 +var dynamo_access = require('./dynamodb.js')
4 +
5 +
6 +// router.post('/app/save_data',function(req,res,next){
7 +// var body = req.body;
8 +// dynamo_access.save_app_data(body,function(response){
9 +// console.log(response)
10 +// if(response == true) res.status(200).json("Success")
11 +// else res.status(500).json("Fail")
12 +// });
13 +// })
14 +
15 +router.get('/app/get_data/:id',function(req,res,next){
16 + dynamo_access.get_user_data(req.params.id,function(response){
17 + console.log(response)
18 + res.status(200).json(response)
19 + })
20 +})
21 +
22 +router.post('/app/get_diff_data',function(req,res,next){
23 + var body = req.body;
24 + console.log(body);
25 + dynamo_access.get_diff_data(body,function(response){
26 + console.log(response)
27 + res.status(200).json(response)
28 + })
29 +})
30 +
31 +
32 +router.post('/pi/save_data',function(req,res,next){
33 + var body = req.body;
34 + console.log(body);
35 + dynamo_access.save_pi_data(body,function(response){
36 + console.log(response)
37 + if(response == true) res.status(200).json("Success")
38 + else res.status(500).json("Fail")
39 + });
40 +})
41 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 +extends layout
2 +
3 +block content
4 + h1= message
5 + h2= error.status
6 + pre #{error.stack}
1 +extends layout
2 +
3 +block content
4 + h1= title
5 + p Welcome to #{title}
1 +doctype html
2 +html
3 + head
4 + title= title
5 + link(rel='stylesheet', href='/stylesheets/style.css')
6 + body
7 + block content