From c0dac6b5083212490aa418f12bf7baab66f7eb85 Mon Sep 17 00:00:00 2001 From: Timothy M Preble Date: Mon, 25 Nov 2024 12:51:41 -0500 Subject: [PATCH] move to 3ks --- .gitignore | 67 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 52 ++++++++++++++++++++++++++++++++++ config.js.dist | 19 +++++++++++++ dockerfile | 14 ++++++++++ main.js.backup | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 16 +++++++++++ run.txt | 4 +++ server.js | 66 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 314 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 config.js.dist create mode 100644 dockerfile create mode 100644 main.js.backup create mode 100755 package.json create mode 100644 run.txt create mode 100755 server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2352227 --- /dev/null +++ b/.gitignore @@ -0,0 +1,67 @@ +#Visual Studio Code Settings +.vscode/ + +#Ignore Real Config Files a distributed one is included for structure.s +config.js + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# next.js build output +.next \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba70fec --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +#tsAPI3 -- Twitter API +This project uses a local database to run stats on specified hash-tags. + + +#Notes +https://medium.com/@rdsubhas/docker-for-development-common-problems-and-solutions-95b25cae41eb +#SCP +http://www.hypexr.org/linux_scp_help.php +https://www.howtoforge.com/tutorial/ssh-and-scp-with-public-key-authentication/ + + + + +#Package & Ship app to Pi + +#Move to parent (Code) Directory +cd /users/timpreble/Documents/code + +#Zip Main Folder + Files only. +https://www.gnu.org/software/tar/manual/tar.html#SEC35 + +tar +-c : create +-z : read or write archives through gzip, allowing tar to directly operate on several kinds of compressed archives transparently +-f : new file name +-v : verbose + +tar -czf folder_name.tar.gz folder_name/ +tar -czf tsAPI3.tar.gz tsAPI3/ + + + +#Ship Zip File to TSAPI3 +scp scp tsAPI3.tar.gz timp@RPi3(IPADDRESS):/docker-data + +#ssh to RPi3 +ssh RPi3 + +#move into correct directory & uncompress archive +cd /docker-data +tar -zxvf tsAPI3.tar.gz + +#Remove original archive +rm tar tsAPI3.tar.gz + +#Move into api dir & build docker image +cd /docker-data/tsAPI3 +docker build -t timpreble/tsapi:3.0 . + +#Run the container +docker run -d --name tsapi3 --restart=always -p 3001:3001 timpreble/tsapi:3.1 + diff --git a/config.js.dist b/config.js.dist new file mode 100755 index 0000000..82c124f --- /dev/null +++ b/config.js.dist @@ -0,0 +1,19 @@ +'use strict' + +var mysql = require('mysql'); + +module.exports = { + name: 'TS-api3', + hostname : 'http://localhost', + version: '3.0.1', + env: process.env.NODE_ENV || 'development', + port: process.env.PORT || 3000, + db: { + get : mysql.createConnection({ + host : 'ip or host name here', + user : 'yOuR_UsEr', + password : 'S0mePa$$w0rd', + database : 'YourDBs' + }) + } +} \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..f44d498 --- /dev/null +++ b/dockerfile @@ -0,0 +1,14 @@ +FROM arm32v7/node:alpine + +#Create App Directory +WORKDIR /usr/src/api + +#Install Dependencies +COPY package.json . +RUN npm install + +#Bundle ap source +COPY . . + +EXPOSE 3001 +CMD ["npm", "start"] \ No newline at end of file diff --git a/main.js.backup b/main.js.backup new file mode 100644 index 0000000..9749a52 --- /dev/null +++ b/main.js.backup @@ -0,0 +1,76 @@ +/** + * Module Dependencies + */ +const config = require('./config'), + restify = require('restify'), + mysql = require('mysql') + + +/** + * Initialize Server + */ +const server = restify.createServer({ + name : config.name, + version : config.version, + url : config.hostname +}); + +var connection = config.db.get; +server.use(restify.plugins.acceptParser(server.acceptable)); +server.use(restify.plugins.queryParser()); +server.use(restify.plugins.bodyParser()); + +/*server.get('/echo/:name', function (req, res, next) { + res.send(req.params); + return next(); +});*/ + +//rest api to get all results +server.get('/employees', function (req, res) { + //connection.query('select * from tracking limit 10', function (error, results, fields) { + connection.query('CALL CP_RPT_TodaysCounts();', function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to get a single employee data +server.get('/employees/:id', function (req, res) { + connection.query('select * from employee where id=?', [req.params.id], function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to create a new record into mysql database +server.post('/employees', function (req, res) { + var postData = req.body; + connection.query('INSERT INTO employee SET ?', postData, function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to update record into mysql database +server.put('/employees', function (req, res) { + connection.query('UPDATE `employee` SET `employee_name`=?,`employee_salary`=?,`employee_age`=? where `id`=?', [req.body.employee_name,req.body.employee_salary, req.body.employee_age, req.body.id], function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + }); +}); + +//rest api to delete record from mysql database +/*server.delete('/employees/:id', function (req, res) { + connection.query('DELETE FROM `employee` WHERE `id`=?', [req.params.id], function (error, results, fields) { + if (error) throw error; + res.end('Record has been deleted!'); + }); +});*/ + +server.get('/', function(req, res){ + console.log('Welcome Nodejs restify'); +}); + +server.listen(3001, function () { + console.log('%s listening at %s', server.name, server.url); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100755 index 0000000..0ab955d --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "nodejs-restapi-example", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "mysql": "^2.14.1", + "restify": "^5.2.0" + } +} diff --git a/run.txt b/run.txt new file mode 100644 index 0000000..c2b82be --- /dev/null +++ b/run.txt @@ -0,0 +1,4 @@ +sudo docker build -t timpreble/tsapi3:1.0.0 . + + +sudo docker run -d -p 3001:3001 --name tsapi3.1 --restart=always timpreble/tsapi3:1.0.0 diff --git a/server.js b/server.js new file mode 100755 index 0000000..5715f7c --- /dev/null +++ b/server.js @@ -0,0 +1,66 @@ +/** + * Module Dependencies + */ +const config = require('./config'), + restify = require('restify'), + mysql = require('mysql') + + +/** + * Initialize Server + */ +const server = restify.createServer({ + name : config.name, + version : config.version, + url : config.hostname +}); + +var connection = config.db.get; +server.use(restify.plugins.acceptParser(server.acceptable)); +server.use(restify.plugins.queryParser()); +server.use(restify.plugins.bodyParser()); + +/*server.get('/echo/:name', function (req, res, next) { + res.send(req.params); + return next(); +});*/ + +//rest api to get all results +server.get('/api/today', function (req, res) { + //connection.query('select * from tracking limit 10', function (error, results, fields) { + connection.query('CALL CP_RPT_TodaysCounts();', function (error, results, fields) { + if (error) throw error; + res.end(JSON.stringify(results)); + //console.log(results); + //res.end(); + }); +}); + +server.get('/api/campaign',function(req, res){ + connection.query('CALL CP_RPT_TweetsByScreenName ();',function(error, results, fields){ + if(error) throw error; + //https://www.sitepoint.com/using-node-mysql-javascript-client/ + res.end(JSON.stringify(results)); + //res.end(results); + }); +}); + +/* +server.get('',function(){ + connection.query('',function(){}); +}); +*/ + +//rest api to get a single employee data +//rest api to create a new record into mysql database +//rest api to update record into mysql database +//rest api to delete record from mysql database + + +server.get('/', function(req, res){ + console.log('Welcome Nodejs restify'); +}); + +server.listen(3001, function () { + console.log('%s listening at %s', server.name, server.url); +}); \ No newline at end of file