forked from brewfactory/BrewCore
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
172 lines (126 loc) · 3.11 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Application root
*
* @module app
**/
'use strict';
// JSX support for React
require('node-jsx').install({
extension: '.jsx'
});
var config = require('./config');
var pkg = require('./package.json');
var koa = require('koa');
var serve = require('koa-static');
var router = require('koa-router')();
var body = require('koa-parse-json');
var cors = require('koa-cors');
var render = require('koa-ejs');
var thunkify = require('thunkify-wrap');
var mongoose = require('mongoose');
var Logger = require('./server/module/Logger');
var Socket = require('./server/module/Socket');
var routeHelper = require('./server/module/routeHelper');
var BrewUI = require('make-shift-brew-ui');
BrewUI.isomorphic();
var app = koa();
var server;
/**
* Configuration
*/
mongoose.connect(config.mongo.connect);
/**
* Configuring middleware
*/
render(app, {
root: config.clientDir,
layout: 'layout',
viewExt: 'html'
});
app.use(cors({
methods: ['GET', 'PUT', 'POST', 'PATCH']
}));
app.use(body());
app.use(router.routes());
Logger.init();
app.use(serve(config.clientDir));
// Register fetchers
BrewUI.Fetcher.register('brew', require('./server/fetcher/brew'));
BrewUI.Fetcher.register('log', require('./server/fetcher/log'));
/* *
* React page middleware
*/
app.use(function *(next) {
// Is react route?
if (!routeHelper.isReactRoute(BrewUI.routes, { path: this.path, method: this.method })) {
yield next;
return;
}
var application = new BrewUI.App({
fetcher: BrewUI.Fetcher
});
var actionContext = application.context.getActionContext();
var executeAction = thunkify(actionContext.executeAction);
var renderedHtml;
// Execute navigation action
try {
yield executeAction(BrewUI.actions.navigateAction, { path: this.url });
} catch (err) {
if(err.status === 404) {
this.throw(404);
}
this.throw(500, 'Error happened.');
}
yield application.init();
// React render
renderedHtml = BrewUI.React.renderToString(application.getComponent());
// Render layout with the application state
yield this.render('layout', {
html: renderedHtml,
state: routeHelper.shareState(application)
});
});
/**
* Router
*/
{
let brew = require('./server/route/brew');
let log = require('./server/route/log');
router.get('/api', function *(next) {
yield next;
this.body = {name: pkg.name, version: pkg.version};
});
router.get('/api/brew', brew.get);
router.post('/api/brew', brew.create);
router.patch('/api/brew/stop', brew.stop);
router.patch('/api/brew/pause', brew.pause);
// logs
router.get('/api/brew/log', log.find);
router.get('/api/brew/log/:id', log.findOne);
}
/**
* Fire up the server
*/
server = require('http').Server(app.callback());
server.listen(process.env.PORT || 5000, function () {
Logger.info('Server is listening on ' + process.env.PORT, 'app', {
name: pkg.name,
version: pkg.version,
env: config.env
});
});
/**
* Socket.io
*/
{
let io = require('socket.io')(server);
Socket.init(io);
}
/*
* Core (brewer)
*/
{
let core = require('./core');
Socket.setCoreEmitter(core.emitter);
core.init();
}