-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·107 lines (85 loc) · 2.51 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
"use strict";
const co = require("co");
co(function* co() {
const config = require("./config.json");
const env = config.env[process.env.NODE_ENV];
const hbs = require("koa-hbs");
const serve = require("koa-static-folder");
// for passport support
const session = require("koa-generic-session");
const bodyParser = require("koa-bodyparser");
const Koa = require("koa");
const app = new Koa();
// socket stuff
const KoaSocket = require("koa-socket");
const io = new KoaSocket();
const r = require("rethinkdbdash")(config.site.db);
io.attach(app);
exports.app = app;
const rss = require("./controllers/rss");
// for all socket interations
require("./controllers/sockets");
// misc handlebars helpers
require("./helpers/handlebars");
// trust proxy
app.proxy = true;
// sessions
app.keys = [config.site.secret];
app.use(session());
// body parser
app.use(bodyParser());
// statically serve assets
app.use(serve("./assets"));
// load up the handlebars middlewear
// app.use(hbs.middleware({
// viewPath: `${__dirname}/views`,
// layoutsPath: `${__dirname}/views/layouts`,
// partialsPath: `${__dirname}/views/partials`,
// defaultLayout: "main"
// }));
app.use(serve(`./${env.views_dir}`));
app.use(function* appUse(next) {
try {
yield next;
} catch (err) {
if (this.state.api === true) {
// if this was an API request, send the error back in a plain response
this.app.emit("error", err, this);
this.body = {error: true, message: String(err)};
} else {
// this wasn"t an API request, show the error page
this.app.emit("error", err, this);
yield this.render("error", {
dump: err
});
}
}
});
require("./routes");
console.log(`${config.site.name} is now listening on port ${config.site.port}`);
app.listen(config.site.port);
yield rss.init();
if (process.env.NODE_ENV === "local") {
// react stuff
const webpack = require("webpack");
const webpackConfig = require("./webpack.config.js");
const WebpackDevServer = require("webpack-dev-server");
// WEBPACK DEV SERVER
new WebpackDevServer(webpack(webpackConfig), {
"hot": true,
"historyApiFallback": true,
proxy: {
"*": `http://localhost:${config.site.port}`
},
stats: "errors-only"
}).listen(config.site.port + 1, "localhost", function webpackDevServer(err, result) {
if (err) {
console.log(err);
}
console.log(`Webpack Dev Server (Hot-Reload) listening on port ${config.site.port + 1}`);
});
}
process.on("SIGINT", function end() {
process.exit();
});
});