-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
305 lines (265 loc) · 8.16 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//importing fs
var fs = require("fs");
//setup express import
var express = require("express");
//setup express import var
var app = express();
//set port process env
var PORT = process.env.port || 4444;
//use public in static
app.use(express.static(__dirname + "/public"));
//import uuid
const { v4: uuidv4 } = require("uuid");
//import body parser
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
// const path = require('path');
const path = require('path');
// mongo-----------------------------------------------------------------------------------------------------
const { MongoClient } = require("mongodb");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
require("dotenv").config();
const donus = process.env.MONGO_THING;
const mongoDB = donus;
var NewFAQ = new Schema({
person: String,
message: String,
type: String,
vote: Number,
});
// var FlagModel = mongoose.model("NewPost", NewFlag);
var FAQsModel = mongoose.model("NewFAQPost", NewFAQ);
// mongo-----------------------------------------------------------------------------------------------------
app.use(express.static(__dirname + "./public/"));
app.use("/docs", express.static("public"));
// coooooooooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrssssssssssssssssssssssssssssss
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
// coooooooooooooooooorrrrrrrrrrrrrrrrrrrrrrrrrssssssssssssssssssssssssssssss
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
//get faqs route
app.get("/faqs", (request, response) => {
//ip tracking from header info
const ipp =
request.header("x-forwarded-for") || request.connection.remoteAddress;
const ip = ipp.slice(7);
console.log("ip1:" + ip);
//setting up faq to go to mongo to get faqs the slow way on request to illistrate difference in speed
const faqs = () => {
MongoClient.connect(
mongoDB,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (err, db) {
if (err) throw err;
var dbo = db.db("Flags");
var mysort = { vote: -1 };
dbo
.collection("faqs")
.find({})
.sort(mysort)
.toArray(function (err, result) {
if (err) throw err;
const results = result.map((fakews) => {
return fakews;
});
const faq = results;
console.log(faq);
db.close();
response.json({
faq: faq,
});
});
}
);
};
faqs();
});
let allFlags;
let flagInfoArr;
let links = [];
const getAll = () => {
MongoClient.connect(
mongoDB,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (err, db) {
if (err) throw err;
var dbo = db.db("Flags");
var mysort = { region: 1 };
dbo
.collection("flag")
.find({})
.sort(mysort)
.toArray(function (err, result) {
if (err) throw err;
const results = result.map((wall) => {
return wall;
});
allFlags = results;
for (let i = 0; i < 300; i++) {
let dinus = Math.floor(Math.random() * allFlags.length);
// console.log(dinus)
let link = allFlags[dinus].directLink;
// console.log(randoLink)
links.push(link);
};
db.close();
});
console.log('flags loaded')
}
);
};
getAll();
app.get('/api/online', (request, response)=>{
response.send('open flags online')
})
app.get("/all", (request, response) => {
const ipp =
request.header("x-forwarded-for") || request.connection.remoteAddress;
// console.log(allFlags)
const ip = ipp.slice(7);
console.log("ip1:" + ip);
response.json({allFlags});
});
const fillRando = ()=> {
for (let i = 0; i < 300; i++) {
let dinus = Math.floor(Math.random() * allFlags.length);
// console.log(dinus)
let link = allFlags[dinus].directLink;
// console.log(randoLink)
links.push(link);
};}
const buildCache = () => {
if (links.length < 200) {
links.pop();
console.log("low on rando");
fillRando()
}
};
app.get("/rando", (request, response) => {
topRando = links.pop();
// console.log(links)
buildCache();
response.json(topRando);
// console.log(ok)
});
app.post("/newfaq", (request, response) => {
// console.log(request.body)
const person = request.body.person;
const type = request.body.type;
const message = request.body.message;
const vote = 0;
const mongoModle = new FAQsModel({
person: person,
message: message,
type: type,
vote: vote,
});
MongoClient.connect(
mongoDB,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (err, db) {
if (err) throw err;
var dbo = db.db("Flags");
dbo.collection("faqs").insertOne(mongoModle, function (err, res) {
if (err) throw err;
console.log("\x1b[36m", "faq posted");
db.close();
});
}
);
setTimeout(() => {
response.redirect(`/ufaqs`);
}, 100);
});
// app.post('/rename', (request,response)=> {
// MongoClient.connect(
// mongoDB,
// { useNewUrlParser: true, useUnifiedTopology: true },
// function (err, db) {
// if (err) throw err;
// var dbo = db.db("Flags");
// // var myobj = mongoModle;
// dbo.collection("flag").updateMany({}, {$rename:{'ISO3166-2':'ISO3166'}})
// }
// )
// console.log('changed')
// response.send('all done')
// })
// let countryPosting = 'russia'
// let postCountryCode = 'RU-'
// app.post("/postflags", (request, response) => {
// var regionName = [];
// var arrayOfFiles = fs.readdirSync(`./public/${countryPosting}/region`);
// // var regionName =JSON.stringify(arrayOfFiles)
// const thing = arrayOfFiles.map((links) => {
// let region = links.slice(0, -4);
// let country = countryPosting;
// let ISO3166 = postCountryCode;
// let directLink =
// "https://openflags.net/" + country + "/region/" + region + ".svg";
// const quickLink = region + ".svg";
// // const ID = uuidv4()
// return { directLink, quickLink, region, country, ISO3166 };
// });
// console.log(thing)
// MongoClient.connect(
// mongoDB,
// { useNewUrlParser: true, useUnifiedTopology: true },
// function (err, db) {
// if (err) throw err;
// var dbo = db.db("Flags");
// // var myobj = mongoModle;
// dbo.collection("flag").insertMany(thing, function (err, res) {
// if (err) throw err;
// console.log("\x1b[36m", "flags posted!");
// db.close();
// });
// }
// );
// setTimeout(() => {
// response.redirect(`/`);
// }, 300);
// });
// routes--------------------------------------------------------------------------------------------------
//get route for country and region
app.get("/api/json/flagInfo/:country/:region?", function (request, response) {
let keyParam = request.params.region;
let flagInfo = allFlags.find((record) => record.region === keyParam);
response.json({ flagInfo });
});
//get route for region code search
app.get("/api/json/ISO3166/:regionCode?", (request, response) => {
let regionCode = request.params.regionCode;
console.log(regionCode)
// console.log(allFlags)
const ISO = allFlags.filter((record) => record.ISO3166 === regionCode)
console.log(ISO)
response.json({ flagInfo:ISO });
})
//route to list flags availible regions in country
app.get("/api/list/country/:country", function (request, response) {
let country = request.params.country;
console.log(country)
// console.log(allFlags)
const countryList = allFlags.filter((record) => record.country === country)
console.log(countryList)
response.json({ flagInfo:countryList });
});
// dns call to server
require("dns").lookup(require("os").hostname(), function (err, add, fam) {
console.log("addr: " + add);
});
// express app listen and console log on post started
app.listen(PORT);
console.log("server started on " + PORT);