-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
111 lines (106 loc) · 3.54 KB
/
app.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
const express = require("express");
const logger = require("morgan");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const path = require("path");
const compression = require("compression");
const dotenv = require("dotenv");
dotenv.config("./env");
const PORT = process.env.PORT || 5000;
// const google_analytics = process.env.google_analytics;
const sslRedirect = require("heroku-ssl-redirect");
const MongoClient = require("mongodb").MongoClient;
const url = process.env.mongodb_altas_url;
const chinesePoetryQuery = async (expressRes, expressQuery) => {
const client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).catch((err) => {
console.log(err);
});
if (!client) {
return;
}
try {
const db = client.db("chinesePoetry");
let collection = db.collection("tang");
let dbStats = await collection.stats();
// let randomNumber = Math.floor(Math.random() * 97); // 97 poems in total
let randomNumber = Math.floor(Math.random() * dbStats.count);
let randomIndex = `1-00${
randomNumber < 10 ? "0" + String(randomNumber) : String(randomNumber)
}`;
let query = { index: randomIndex };
let res = await collection.findOne(query);
expressRes.send(res);
} catch (err) {
console.log(err);
} finally {
client.close();
}
};
const randomPatternQuery = async (expressRes, expressQuery) => {
const client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).catch((err) => {
console.log(err);
});
if (!client) {
return;
}
try {
const db = client.db("gameOfLife");
let collection = db.collection("gameOfLifePatterns");
let dbStats = await collection.stats();
// let randomIndex = Math.floor(Math.random() * 733); // 733 kinds of pattern in total
let randomIndex = Math.floor(Math.random() * dbStats.count);
let query = { index: randomIndex };
let res = await collection.findOne(query);
if (expressQuery && expressQuery.heightmax && expressQuery.widthmax) {
const heightmax = Number(expressQuery.heightmax);
const widthmax = Number(expressQuery.widthmax);
while (res.height > heightmax || res.width > widthmax) {
randomIndex = Math.floor(Math.random() * 733);
query = { index: randomIndex };
res = await collection.findOne(query);
}
expressRes.send(res);
} else {
expressRes.send(res);
}
} catch (err) {
console.log(err);
} finally {
client.close();
}
};
express()
.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();
})
// uncomment sslRedirect if build a container image and push to docker hub.
.use(sslRedirect())
.use(logger("short"))
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: false }))
.use(cookieParser())
.use(compression())
.use(express.static(path.join(__dirname, "dist")))
.set("views", path.join(__dirname, "views"))
.set("view engine", "ejs")
// .get("/", (req, res) =>
// res.render("index", { google_analytics: google_analytics })
// )
// .get("/", (req, res) => res.sendfile("dist/index.html"))
.get("/random.json", (req, res) => randomPatternQuery(res, req.query))
.get("/tang.json", (req, res) => chinesePoetryQuery(res, req.query))
// .get("*", (req, res) => {
// res.redirect("/");
// })
.listen(PORT, () => console.log(`Listening on ${PORT}`));