-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
95 lines (80 loc) · 2.36 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
const express = require("express");
const app = express();
const path = require("path");
const fs = require("fs");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const compression = require("compression");
const CronJob = require("cron").CronJob;
const { ApolloServer } = require("apollo-server-express");
const reactRoute = require("./src/server").default;
const typeDefs = require("./src/schema");
const resolvers = require("./src/resolvers");
app.use(compression());
app.use(
express.static("src/public", {
maxAge: 31536000000,
setHeaders: (res, path) => {
if (path.includes("uploaddata")) {
res.set("Cache-Control", "no-cache");
}
}
})
);
app.set("views", path.join(__dirname, "/src/views"));
app.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();
});
app.use(bodyParser());
app.use(cookieParser());
// for graphql
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true
});
server.applyMiddleware({ app });
// for react pages
app.get("/:locale(en|ja|zh|ko)", reactRoute);
app.get("/:locale(en|ja|zh|ko)/*", reactRoute);
// redirect if the first param is not language
app.get("*", (req, res) => {
const lang =
req.cookies.locale || req.acceptsLanguages("ja", "zh", "en", "ko") || "ja";
res.setHeader("Cache-Control", "public, max-age=0");
if (req.url === "/") {
res.redirect(301, `/${lang}`);
} else {
res.redirect(301, `/${lang}${req.url}`);
}
});
// for jobs
fs.readdirSync("./src/jobs").forEach(file => {
if (file.substr(-3) == ".js") {
let job = require(`./src/jobs/${file}`);
if (job.job && job.cronSchedule) {
const cronJob = new CronJob(job.cronSchedule, job.job);
cronJob.start();
console.log("Starting cron job", file);
if (process.env.RUN_CRON_ON_START_UP === "true") {
console.log("Run cron job once on deployment:", file);
job.job();
}
}
}
});
app.listen(process.env.PORT, function() {
console.log(`This app listening on port ${process.env.PORT}!`);
});
process.on("uncaughtException", function(err) {
console.log("uncaughtException => ", err);
});
process.on("SIGINT", () => {
console.log("Recieved signal SIGINT");
process.exit(0);
});