-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (80 loc) · 2.75 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
const globalVars = require("./globalVars");
const express = require("express");
const app = express();
const env = process.env.NODE_ENV || "dev";
const port = env == "production" ? 3001 : 3000;
const session = require("express-session");
const compression = require("compression");
const ghp = require("./helpers/gql");
// Set view location
app.set("views", "./views");
// Set template engine
app.set("view engine", "pug");
// Set publicly-accessible path
app.use("/public", express.static(__dirname + "/public"));
// Routes
const authenRouter = require("./routes/authen");
const manageRequestRouter = require("./routes/manageRequest");
const manageRoleRouter = require("./routes/manageRole");
const manageSpaceRouter = require("./routes/manageSpace");
const manageReportRouter = require("./routes/manageReport");
const manageMaterialRouter = require("./routes/manageMaterial");
const spaceRouter = require("./routes/space");
const myRequestRouter = require("./routes/myRequest");
const chooseDeptRouter = require("./routes/chooseDept");
app.use(session(globalVars.sessionOptions));
app.use(compression());
app.use("/authen", authenRouter);
app.use("/manage-request", manageRequestRouter);
app.use("/manage-role", manageRoleRouter);
app.use("/manage-space", manageSpaceRouter);
app.use("/manage-report", manageReportRouter);
app.use("/manage-material", manageMaterialRouter);
app.use("/space", spaceRouter);
app.use("/my-request", myRequestRouter);
app.use("/choose-dept", chooseDeptRouter);
// Apollo
const ApolloClient = require("apollo-client").ApolloClient;
const InMemoryCache = require("apollo-cache-inmemory").InMemoryCache;
const createHttpLink = require("apollo-link-http").createHttpLink;
const setContext = require("apollo-link-context").setContext;
const fetch = require("node-fetch");
const gql = require("graphql-tag");
const apollo = new ApolloClient({
link: createHttpLink({ uri: globalVars.gqlURL, fetch: fetch }),
cache: new InMemoryCache()
});
app.get("/", (req, res) => {
ghp.getDepartments(apollo)
.then(depts => {
res.render("index", {
member: req.session.member,
currentDept: req.session.currentDept,
faculty: depts.data.departments
});
})
.catch(err => {
if (env != "production") console.log(err);
res.redirect("/error/");
})
});
app.get("/error", (req, res) => {
res.render("error", {
member: req.session.member,
currentDept: req.session.currentDept
});
});
app.get("/graphiql", (req, res) => {
res.render("graphiql", {
session: req.session,
host: globalVars.apiHostname,
member: req.session.member,
currentDept: req.session.currentDept
});
});
// Start the server
app.listen(port, () => {
console.log("Server started\nListening on port " + port);
console.log("Using API: " + globalVars.apiHostname);
console.log("ENV: " + env);
});