imlementation and cleanup in index.js
This commit is contained in:
80
index.js
80
index.js
@@ -6,53 +6,81 @@ const bodyParser = require('body-parser');
|
|||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const helmet = require('helmet');
|
const helmet = require('helmet');
|
||||||
const morgan = require('morgan');
|
const morgan = require('morgan');
|
||||||
|
const request = require('request');
|
||||||
|
const WebSocket = require('ws');
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var https = require('https');
|
|
||||||
var privateKey = fs.readFileSync('/etc/letsencrypt/live/zbor.eu.org/privkey.pem', 'utf8');
|
|
||||||
var certificate = fs.readFileSync('/etc/letsencrypt/live/zbor.eu.org/cert.pem', 'utf8');
|
|
||||||
|
|
||||||
var credentials = {key: privateKey, cert: certificate};
|
|
||||||
|
|
||||||
// defining the Express app
|
// defining the Express app
|
||||||
const app = express();
|
const app = express();
|
||||||
var httpServer = http.createServer(app);
|
var httpServer = http.createServer(app);
|
||||||
var httpsServer = https.createServer(credentials, app);
|
|
||||||
|
|
||||||
// defining an array to work as the database (temporary solution)
|
|
||||||
const ads = [
|
|
||||||
{title: 'Hello, world (again)!'}
|
|
||||||
];
|
|
||||||
|
|
||||||
// adding Helmet to enhance your API's security
|
// adding Helmet to enhance your API's security
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
|
|
||||||
// using bodyParser to parse JSON bodies into JS objects
|
// using bodyParser to parse JSON bodies into JS objects
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
// enabling CORS for all requests
|
// enabling CORS for all requests
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
// adding morgan to log HTTP requests
|
// adding morgan to log HTTP requests
|
||||||
app.use(morgan('combined'));
|
app.use(morgan('combined'));
|
||||||
|
|
||||||
// defining an endpoint to return all ads
|
//!UI & resources
|
||||||
|
app.use(express.static('public'));
|
||||||
|
app.set('view engine', 'ejs');
|
||||||
|
app.use(require('express-ejs-layouts'));//https://www.npmjs.com/package/express-ejs-layouts
|
||||||
|
//defining endpoints
|
||||||
|
|
||||||
|
//!other sources
|
||||||
|
var auth = require('./src/auth.js');
|
||||||
|
var db = require('./src/db');
|
||||||
|
var mqtt = require('./src/mqtt.js');
|
||||||
|
|
||||||
|
|
||||||
|
const env = process.env.NODE_ENV || 'development';
|
||||||
|
console.log("ENV: %s", env);
|
||||||
|
const config = require(__dirname + '/config/config.json')[env];
|
||||||
|
config.gardenIP = process.env.GARDEN_IP || config.gardenIP
|
||||||
|
|
||||||
|
// defining HTTP endpoints
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.send(ads);
|
res.send([
|
||||||
|
{title: 'Hello, world (again)!'}
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
app.get('/dht', (req, res) => {
|
|
||||||
res.send("DHT");
|
|
||||||
|
app.get('/garden/:days?', function(req, res){
|
||||||
|
res.render('chartGarden', {model:req.params});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get("/device/:field_name/:days?", function (req, res) {
|
||||||
|
db.devicemessages.findByName(req.params.field_name, req.params.days, function (err, data) {
|
||||||
|
if (!err) { res.send(data); }
|
||||||
|
else { console.log("error: ", err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
app.use('/water',function(req, res){
|
||||||
|
var time = req.query.t;
|
||||||
|
console.log("Watering cmd for: " + time);
|
||||||
|
request('http://'+config.gardenIP+'/tools?cmd=event,manualwatering='+ time, { json: true }, (err, res, body) => {
|
||||||
|
if (err) { return console.log("Problem watering: " + err); }
|
||||||
|
console.log("Watering cmd successfully sent!");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
app.use('/waterStop',function(req, res){
|
||||||
|
request('http://'+config.gardenIP+'/tools?cmd=event,stopwatering', { json: true }, (err, res, body) => {
|
||||||
|
if (err) { return console.log("Problem watering: " + err); }
|
||||||
|
console.log("STOP watering cmd sent!");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// starting the server
|
// starting the server
|
||||||
//app.listen(3001, () => {
|
//app.listen(3001, () => {
|
||||||
//console.log('listening on port 3001');
|
//console.log('listening on port 3001');
|
||||||
//});
|
//});
|
||||||
|
|
||||||
// httpServer.listen(8080, () => {
|
httpServer.listen(2080, () => {
|
||||||
// console.log('httpServer listening on port 8080');
|
console.log('index.js: httpServer listening on port 2080');
|
||||||
// });
|
|
||||||
httpsServer.listen(8443, () => {
|
|
||||||
console.log('httpsServer listening on port 8443');
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user