This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
109 lines (95 loc) · 2.72 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict'
const fs = require('fs');
const mongoose = require('mongoose');
let mongoUri
// Get secret for MongoDB access
const fetchedPost = new mongoose.Schema(
{
title: 'string',
domain: 'string',
commentLink: 'string',
url: 'string',
thumbnail: 'string',
created_utc: 'number',
upvoteCount: 'number',
commentCount: 'number',
author: 'string',
fetchedAt: {
type: Date,
default: new Date(),
expires: 86400,
},
sub: 'string',
}, { collection: 'newposts' },
);
const post = mongoose.model('newPost', fetchedPost);
// Connect to database
fs.readFile('/var/openfaas/secrets/mongouri', 'utf8', function(err, mongoUri){
mongoose
.connect(mongoUri, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.catch(
err => console.warn(`MongoDB connect error: ${err}`) // eslint-disable-line no-console
);
});
// Database event handlers
mongoose.connection.on('connected', () => {
console.log('F5 is connected to MongoDB...'); // eslint-disable-line no-console
});
mongoose.connection.on('disconnected', (err) => {
console.warn(`MongoDB disconnected: ${err}`); // eslint-disable-line no-console
setTimeout(() => {
connectToDB();
}, 3000);
});
mongoose.connection.on('error', (err) => {
console.warn(`MongoDB error: ${err}`); // eslint-disable-line no-console
setTimeout(() => {
connectToDB();
}, 3000);
});
// main request handler
module.exports = async (event, context) => {
console.log(event.query); // eslint-disable-line no-console
const result = {
'body': JSON.stringify({extra: mongoUri}),
'content-type': event.headers["content-type"]
}
const utcDate = Math.floor(new Date().getTime() / 1000);
// Depending on time per day 30 minute and 60 minute searches in database
const timeAdjust = () => {
const today = new Date().getUTCHours();
if (today >= 11 && today <= 23) {
return '7200'; // 2 Hours
} else {
return '14400'; // 4 Hours
}
};
const searchTime = utcDate - timeAdjust();
// Search the db and return up to 20 docs
try {
const searchSub = event.query? event.query.sub? event.query.sub: "politics" : "politics";
const posts = await post
.find({ created_utc: { $gt: searchTime }, upvoteCount: { $gt: 5 }, sub: searchSub })
.sort({ upvoteCount: -1, created_utc: 1 })
.limit(20)
.exec()
return context
.headers(
{
'Content-type': 'application/json',
"Access-Control-Allow-Origin": "*"
}
)
.status(200)
.succeed({posts:posts})
} catch (error) {
return context
.status(503)
.succeed({error: "There was an error fetching posts"})
}
}