2020-03-08 22:40:44 -05:00
|
|
|
const micro = require('micro');
|
2020-04-24 21:10:04 -05:00
|
|
|
const compress = require('micro-compress');
|
2020-03-09 10:49:19 -05:00
|
|
|
const queryString = require('querystring');
|
2020-03-08 22:40:44 -05:00
|
|
|
const Database = require('better-sqlite3');
|
2020-04-22 21:10:21 -05:00
|
|
|
const fs = require('fs');
|
2020-03-08 22:40:44 -05:00
|
|
|
|
2020-04-24 21:10:04 -05:00
|
|
|
module.exports = compress(async (req, res) => {
|
2020-03-08 22:40:44 -05:00
|
|
|
console.log(req.method, req.url);
|
2020-03-09 10:49:19 -05:00
|
|
|
res.setHeader("Access-Control-Allow-Origin","*");
|
|
|
|
|
//console.log(res);
|
2020-03-08 22:40:44 -05:00
|
|
|
if (req.method === "GET") {
|
|
|
|
|
console.log("Get Request coming in");
|
2020-04-22 21:10:21 -05:00
|
|
|
String.prototype.mySqlEsc = function(){return(this.replace(/'/g,"''"))};
|
|
|
|
|
let qsAttr;
|
2020-03-09 10:49:19 -05:00
|
|
|
let func=req.url;
|
|
|
|
|
if (req.url.indexOf("?") >=0){
|
|
|
|
|
func=req.url.substring(0,req.url.indexOf("?"))
|
|
|
|
|
qsAttr=queryString.parse(req.url.substring(req.url.indexOf("?")+1));
|
|
|
|
|
}
|
|
|
|
|
console.log(func,qsAttr);
|
|
|
|
|
if (func === "/getCombinations") {
|
|
|
|
|
console.log("Request for combinations");
|
2020-03-08 22:40:44 -05:00
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
const stmt = db.prepare("SELECT distinct Symbol,date(TimeStamp,'localtime') Date ,time(TimeStamp,'localtime') Time FROM OptionQuotes");
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
2020-04-22 21:10:21 -05:00
|
|
|
return(true);
|
2020-03-08 22:40:44 -05:00
|
|
|
}
|
2020-03-09 10:49:19 -05:00
|
|
|
if (func === "/getSymbols") {
|
|
|
|
|
console.log("Request for Symbols");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
const stmt = db.prepare("SELECT distinct Symbol FROM OptionQuotes order by 1");
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
2020-04-22 21:10:21 -05:00
|
|
|
return(true);
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
if (func === "/getDates") {
|
|
|
|
|
console.log("Request for Dates");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
2020-04-24 20:39:45 -05:00
|
|
|
let sql = "SELECT distinct date(TimeStamp,'localtime') Date FROM OptionQuotes where 1=1";
|
|
|
|
|
if (qsAttr.Symbol) {
|
|
|
|
|
sql +=" and Symbol='" + qsAttr.Symbol.mySqlEsc() + "'";
|
|
|
|
|
}
|
|
|
|
|
sql +=" order by 1";
|
|
|
|
|
const stmt = db.prepare(sql);
|
2020-03-09 10:49:19 -05:00
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
2020-04-22 21:10:21 -05:00
|
|
|
return(true);
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
if (func === "/getTimes") {
|
|
|
|
|
console.log("Request for Times");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
let sql = "SELECT distinct time(TimeStamp,'localtime') Time FROM OptionQuotes where 1=1"
|
|
|
|
|
if (qsAttr.Symbol) {
|
2020-04-22 21:10:21 -05:00
|
|
|
sql +=" and Symbol='" + qsAttr.Symbol.mySqlEsc() + "'";
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
if (qsAttr.Date){
|
2020-04-22 21:10:21 -05:00
|
|
|
sql +=" and date(TimeStamp,'localtime')='" + qsAttr.Date.mySqlEsc() + "'";
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
sql+=" order by 1";
|
|
|
|
|
const stmt = db.prepare(sql);
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
2020-04-22 21:10:21 -05:00
|
|
|
return(true);
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
if (func === "/getOptionChain") {
|
|
|
|
|
console.log("Request for OptionChain");
|
|
|
|
|
const db = new Database('./quoteDBs/consolidated.sqlite3', { verbose: console.log });
|
|
|
|
|
let sql = "SELECT Data FROM OptionQuotes where 1=1"
|
|
|
|
|
if (qsAttr.Symbol) {
|
2020-04-22 21:10:21 -05:00
|
|
|
sql +=" and Symbol='" + qsAttr.Symbol.mySqlEsc() + "'"
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
if (qsAttr.Date){
|
2020-04-22 21:10:21 -05:00
|
|
|
sql +=" and date(TimeStamp,'localtime')='" + qsAttr.Date.mySqlEsc() + "'";
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
if (qsAttr.Time){
|
2020-04-22 21:10:21 -05:00
|
|
|
sql +=" and time(TimeStamp,'localtime')='" + qsAttr.Time.mySqlEsc() + "'";
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stmt = db.prepare(sql);
|
|
|
|
|
let allDates = stmt.all();
|
|
|
|
|
//console.log(JSON.stringify(allDates));
|
|
|
|
|
db.close();
|
|
|
|
|
micro.send(res,200,allDates);
|
2020-04-22 21:10:21 -05:00
|
|
|
return(true);
|
2020-03-09 10:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-22 21:10:21 -05:00
|
|
|
console.log("Request for File");
|
|
|
|
|
// Anything else we deliver as file data
|
|
|
|
|
if (func === "/") { func += 'index.html';}
|
|
|
|
|
// this is very basic protection only...
|
|
|
|
|
if ((func.indexOf("..") === -1) && (func.indexOf("~") === -1) && (func.indexOf("favicon ") === -1) ){
|
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
2020-04-24 20:39:45 -05:00
|
|
|
fs.readFile("../client/"+func, (err,filecontent)=>{
|
2020-04-22 21:10:21 -05:00
|
|
|
if (err) {
|
2020-04-24 20:39:45 -05:00
|
|
|
console.log(err);
|
|
|
|
|
micro.send(res,500, "Error. check console for details.");
|
2020-04-22 21:10:21 -05:00
|
|
|
} else {
|
|
|
|
|
micro.send(res,200,filecontent);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
micro.send(res,401,"Invalid file to serve");
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 22:40:44 -05:00
|
|
|
}
|
2020-04-24 21:10:04 -05:00
|
|
|
});
|
2020-03-08 22:40:44 -05:00
|
|
|
|