-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (95 loc) · 2.67 KB
/
index.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 { createSecureServer } = require("http2");
const {
readFileSync,
stat
} = require("fs");
const { join } = require("path");
const Koa = require("koa");
const morgan = require("koa-morgan");
const serve = require("koa-static");
const koaViews = require("koa-views");
const session = require("koa-session");
const passport = require("koa-passport");
const rotatingFileStream = require("rotating-file-stream");
const config = require("./config");
console.info("Loaded config", config);
const PORT = process.env.PORT || 8443;
const accessLogStream = getLogStream(`access.log`);
const errorLogStream = getLogStream(`error.log`);
const app = new Koa();
const apiRouter = require("./apiRouter");
const staticRouter = require("./staticRouter");
const SESSION_CONFIG = config.session;
// app.use(session(SESSION_CONFIG, app));
apiRouter.use(session(SESSION_CONFIG, app));
staticRouter.use(session(SESSION_CONFIG, app));
// app.use(koaViews(config.koaViews.path, config.koaViews.options));
app.keys = [
"daily-coding-challenge-super-secret-0",
"dccsecret1",
"dccsecret2"
];
console.info(`Starting server`);
app
.use(session(SESSION_CONFIG, app))
.use(morgan(`dev`, {
skip: (req, res) => res.statusCode < 400
}))
.use(morgan(`common`, {
stream: accessLogStream
}))
.use(morgan(`combined`, {
stream: errorLogStream,
skip: (req, res) => res.statusCode < 400
}))
.use(koaViews(config.koaViews.path, config.koaViews.options))
.use(async (ctx, next) => {
// ctx.render provided by koaViews middleware
if (ctx.path === "/") {
// if root path, don't return index.html
// return homepage hbs template
return await ctx.render("default", {
title: "Home"
});
} else {
// if not the root path, move along
return next();
}
})
.use(serve(`public`, `${__dirname}/public`))
.use(serve(`dependency`, `${__dirname}/node_modules`))
.use(async (ctx, next) => {
// set ctx.state to set props
// then
// ctx.render(templateName);
// or
// ctx.render(templateName, { ... })
// console.log("ctx???", ctx.path);
if (ctx.path.startsWith("/api")) return next();
var layoutName = ctx.path
.replace("/", "")
.replace("-", " ");
if (layoutName === "") {
layoutName = "default";
}
return await ctx.render(layoutName, {
title: "Home"
});
})
.use(apiRouter.routes())
.use(staticRouter.routes())
.use(apiRouter.allowedMethods())
.use(staticRouter.allowedMethods());
createSecureServer(config.server, app.callback())
.listen(PORT, () => {
console.info("Server started at :%s", PORT);
});
function getLogStream (outputFilename) {
return rotatingFileStream(
outputFilename,
{
interval: `1d`, //rotate daily
path: join(__dirname, `log`)
}
);
}