-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
129 lines (106 loc) · 3.22 KB
/
server.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const mongoose = require('mongoose');
const uriUtil = require('mongodb-uri');
const config = require('./config');
const pythonShell = require('python-shell');
const fs = require('fs');
const authorize = require('./authorize').auth;
const googleapis = require('googleapis');
const { google } = googleapis;
// const apiRoutes = require('./routes/index');
const app = express();
const http = require('http').Server(app);
// Setup Server
app.use((req, res, next) => {
const responseHeaders = {
'Access-Control-Allow-Origin': 'http://localhost:3200',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, accept, Authorization, Content-Length, X-Requested-With, token, x-access-token',
'Access-Control-Allow-Credentials': 'true'
}
res.header({ ...responseHeaders });
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
});
app.use(bodyParser.json());
app.use(morgan('dev'));
app.set('superSecret', config.secret);
app.get('/', function(req, res){
res.send('server is running on http://localhost:8000')
});
app.post('/api/calendar/add', (req, res) => {
console.log(req.body)
const scheduleData = {
title: "Ganas – Mas Exitos con Ganas",
month: "April 2018",
link: "http://dublab.com/events/63539/live-broadcast-ganas/",
time: "12:00 PM - 02:00 PM",
date: "1",
day: "Sun",
user: "[email protected]"
// invites: [{
// email: '[email protected]'
// },
// {
// email: '[email protected]'
// }]
};
const event = {
summary: scheduleData.title,
location: scheduleData.link,
description: 'Daecent wibbly wobbly trouser-tech and sponge-bass-electracore',
start: {
dateTime: '2018-05-19T09:00:00-07:00',
timeZone: 'America/Los_Angeles',
},
end: {
dateTime: '2018-05-20T17:00:00-07:00',
timeZone: 'America/Los_Angeles',
},
recurrence: [
'RRULE:FREQ=DAILY;COUNT=2'
],
attendees: scheduleData.invites,
reminders: {
useDefault: false,
overrides: [
{method: 'email', minutes: 10},
{method: 'popup', minutes: 10},
]
}
};
const listEvents = (auth) => {
console.log('callback >>>>>>>>>>>>>>>>')
const calendar = google.calendar({version: 'v3', auth});
calendar.events.insert({
auth: auth,
calendarId: 'primary',
resource: event,
}, function(err, event) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
return;
}
console.log('Event created: %s', event.htmlLink);
});
}
fs.readFile('client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), listEvents);
});
res.send({
success: true,
message: 'Endpoint Hit'
})
})
const port = process.env.PORT || 8000;
http.listen(port);
console.log(`listening at port ${port}`)