-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
122 lines (98 loc) · 2.58 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
'use strict';
const express = require('express')
const gcloud = require('gcloud')
const uuid = require('node-uuid')
// App initialization
var app = express()
// Database initialization
var ds = gcloud.datastore({
projectId: 'node-sentry'
// keyFilename: 'service.json'
})
// App configuration
app.enable('trust proxy')
// Handling static contents
app.use('/public', express.static('public'))
// Exposing methods
app.get('/', (req, res) => {
res.send("Sistema de controle e monitoramento das VM do curso de Tecnologia em Informática para Internet.");
})
// Exposing methods to get utilization
app.get('/ping/:id', (req, res) => {
let now = new Date().toJSON()
let req_id = req.params.id
let req_headers = req.headers
console.log("Got request at: '" + now + "' of id: '" + req_id + "'")
var query = ds.createQuery('PingMachine')
.filter('uuid', '=', req_id)
.limit(1)
ds.runQuery(query, (err, results) => {
if (!err) {
if (results.length == 1) {
let result = results[0]
let entity_uuid = result.data.uuid
var key = ds.key('PingEntry')
var data = {
uuid: entity_uuid,
headers: req_headers,
created_at: now
}
ds.save({
key: key,
data: data
}, (err) => {
if (!err) {
res.json({
key: key,
data: data
})
console.log("This request was delivered.")
} else {
res.json({})
console.log("Failed with error: " + err)
}
})
} else {
res.json({})
console.log("PingMachine identity not found.")
}
}
})
})
app.get('/ping', (req, res) => {
let now = new Date().toJSON()
let random_id = uuid.v4()
console.log("Get request at '" + now + "' to generate id: '" + random_id + "'")
var key = ds.key(['PingMachine', random_id])
var data = {
uuid: random_id,
created_at: now
}
ds.save({
key: key,
data: data
}, (err) => {
if (!err) {
res.json({
uuid: random_id
})
console.log("This request was delivered.")
} else {
res.json({})
console.log("Failed with error: " + err)
}
})
})
// Handling server error (500)
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something broke!");
})
// Handling file not found error (404)
app.use((req, res, next) => {
res.status(404).send("Sorry can\'t find that!");
})
// Exposing service on port 8080
app.listen(8080, () => {
console.log("Server running at http://localhost:8080/");
})