added youtube util and cors middleware

This commit is contained in:
Joe-Dowd 2020-03-19 23:32:18 +00:00
parent 5c3d18b0e2
commit 8543647cbc
5 changed files with 17 additions and 22 deletions

View file

@ -4,14 +4,10 @@ var http = require('http');
// Create a service (the app object is just a callback).
var app = express();
let config = JSON.parse(fs.readFileSync('config.json'));
// Utils
var getHash = require('./src/utils/getHash.js');
var getIP = require('./src/utils/getIP.js');
var getFormattedTime = require('./src/utils/getFormattedTime.js');
// Routes
var corsMiddleware = require('./src/routes/corsMiddleware.js');
// Routes
var getVideoSponsorTimes = require('./src/routes/getVideoSponsorTimes.js');
@ -35,26 +31,11 @@ YouTubeAPI.authenticate({
key: config.youtubeAPIKey
});
var Sqlite3 = require('better-sqlite3');
let options = {
readonly: config.readOnly
};
//load database
var db = new Sqlite3(config.db, options);
//where the more sensitive data such as IP addresses are stored
var privateDB = new Sqlite3(config.privateDB, options);
// Create an HTTP service.
http.createServer(app).listen(config.port);
//setup CORS correctly
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(corsMiddleware);
//add the get function
app.get('/api/getVideoSponsorTimes', getVideoSponsorTimes);

5
src/middleware/cors.js Normal file
View file

@ -0,0 +1,5 @@
module.exports = function corsMiddleware(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
}

View file

@ -4,6 +4,7 @@ var config = JSON.parse(fs.readFileSync('config.json'));
var databases = require('../databases/databases.js');
var db = databases.db;
var privateDB = databases.privateDB;
var YouTubeAPI = require('../utils/youtubeAPI.js');
var getHash = require('../utils/getHash.js');
var getIP = require('../utils/getIP.js');

View file

@ -7,6 +7,7 @@ var getIP = require('../utils/getIP.js');
var databases = require('../databases/databases.js');
var db = databases.db;
var privateDB = databases.privateDB;
var YouTubeAPI = require('../utils/youtubeAPI.js');
module.exports = async function voteOnSponsorTime(req, res) {
let UUID = req.query.UUID;

7
src/utils/youtubeAPI.js Normal file
View file

@ -0,0 +1,7 @@
// YouTube API
const YouTubeAPI = require("youtube-api");
YouTubeAPI.authenticate({
type: "key",
key: config.youtubeAPIKey
});
module.exports = YouTubeAPI;